CRM Development

Solutionist has designed the architecture for a new CRM system for use in the medical imaging industry.

Wednesday, 12 January 2011

.Net IoC - part 2 (StructureMap)

It's been some time since I've last posted anything up, but I thought I'd put up a bit more about my
working with IoC in .NET.

In a previous post I mentioned that I'd come across Spring.NET, Unity and StructureMap
I wanted something that I could pick up easily and which focused on IoC. 


Starting to using it fairly simple. 
  1. You reference the structuremap dll
  2. Add something looking like this into your code... I wrapped it up in a Singleton to give me more flexibility.. 
public sealed class Locator     {
        private static volatile Locator _instance = null;         private static object syncRoot = new Object();


        private static Container objectFactory = null; 
        private Locator()
        {
            objectFactory = new Container(
                 x =>
                 {

                    x.For<myservice>().Use<myserviceclient>();                        </myserviceclient></myservice>
 
                     x.For<ihelper>().Use<realhelper>();
  
                     x.For<iinterface1>().Use<implementation1>()
                         .Setter().IsTheDefault();
                 });
        }
 
        public static Locator Instance
        {
            get 
            {         
                if (_instance == null) 
                {            
                    lock (syncRoot)             
                    {               
                        if (_instance == null)                   
                            _instance = new Locator();            
                    }         
                }         
                return _instance;      
            }
        }
 
        public object getObjectInstance()
        {

            return objectFactory.GetInstance();
        }
 

        public object getObjectInstance(Dictionary items)
        {

            var args = new ExplicitArguments();
 
            foreach (var item in items)
            {
                //args.Set(item);
                args.SetArg(item.Key, item.Value);
            }
            return objectFactory.GetInstance(args);
        }


In code I'm calling:
Locator.Instance.getObjectInstance()


The second method allow you to pass in constructor parameters.


All this means I can remove references to StructureMap from my Code and, if need be,
swap out IoC containters without affecting the rest of the code. In fact, this happened eventually.

StructureMap looked a good candidate, the fluent interface is nice and there's no XML if you don't want it! But, I did find the that the lack of decent documentation was an issue.
There was a lot of information - but scattered around. I didn't want to spent ages hunting stuff down.


Unity, I'm afraid, seemed over complicated. I'm still getting my head round other .Net stuff and I wanted to minimise the amount to learn. 


That left Spring.... which, all being well, I'll cover in part 3.

1 comment:

  1. Merkur 15c Safety Razor - Barber Pole - Deccasino
    Merkur 15C Safety Razor - 바카라사이트 Merkur - apr casino 15C poormansguidetocasinogambling.com for https://deccasino.com/review/merit-casino/ Barber Pole 바카라 사이트 is the perfect introduction to the Merkur Safety Razor.

    ReplyDelete