Women Viagra

12Mar/091

Viagra Gay

Viagra gay I was having a discussion with a colleague the other day about DI and Service Location in the context of the question (posed by a third person):

Viagra gay Which DI container/framework should I choose?

Viagra gay His answer:

Viagra gay None; just roll your own and use simple Service Location

Viagra gay I'm not meaning to ruffle any feathers, viagra gay but I couldn't come up with a good case on the spot, viagra gay but as I've thought about it some more, viagra gay I completely disagree, viagra gay respectfully (at least for the reasons that I choose to use a DI container).

Viagra gay What are the reasons I use to decide whether or not to use a DI container?

  • Reduce Coupling
  • Testability
  • Declarative Configuration
  • Testability
  • Rapid Development

Viagra gay Using a simple service locator pattern, viagra gay with a hand-rolled instantiation mechanism, viagra gay can get you the first two, viagra gay but not the last (unless you spend many many hours on your hand-rolled solution, viagra gay which would most likely turn out just like one of the already existing containers).  If you don't care about declarative configuration, viagra gay does that mean the using a container vs not using a container is roughly the same?  No.

Viagra gay There are two other major benefits that I get from using a container that are not in the list above, viagra gay but I feel are important, viagra gay and help testability.

  • Published/declared dependencies
  • Autowiring

Published dependencies vs implicit dependencies

Viagra gay Below is an illustration of two constructors.  The first uses published dependencies, viagra gay and the latter uses implicit.

public MovieLister( IMovieFinder finder )

{

    // DI style

    _finder = finder;

}

 

public MovieLister()

{

    // service locator style

    _finder = ServiceLocator.Resolve<IMovieFinder>("CsvMovieFinder", viagra gay "movies.txt");

}

Viagra gay  

Viagra gay The first is more easily testable and usable, viagra gay because you know your dependencies up front (published by the constructor signature).  Whereas you don't know unless you look at the implementation which services are needed with the implicit dependency example.  Additionally, viagra gay with this example, viagra gay you're tied to the implementation of the service locator, viagra gay and also to using the CsvMovieFinder key (I suppose this key could be in AppConfig or some other configuration mechanism, viagra gay so it could actually be configurable).

Viagra gay I agree it can be just as simple to inject mocks into your service locator for testing the implicit example, viagra gay but I'd rather not have to think about it.  Publishing those dependencies up front makes it easier for the client consuming that object to use and extend, viagra gay because then the client developer knows exactly what is expected.

Autowiring

Viagra gay When dependencies are published, viagra gay it allows the container to use autowiring to build up your application instance.  This may sound like hand-waving magic to those of you that haven't seen it in action, viagra gay but it truly is one of the best features that comes from using a DI container.

Viagra gay The following example shows a simple example of how autowiring can work.

public class Program

{

    private IContainer _container;

 

    public Program()

    {

        // configure container

        _container = new Container();

        _container.Register<MovieLister>();

        _container.Register<IMovieFinder, viagra gay CsvMovieFinder>("movies.txt");

    }

 

    public void Run()

    {

        // uses autowiring to inject CsvMovieFinder

        // into MovieLister

        MovieLister lister = _container.Resolve<MovieLister>();

 

        lister.List( Console.Out );

    }

}

Viagra gay  

Viagra gay The container builds up a dependency graph for the requested object, viagra gay and walks it bottom-up, viagra gay supplying each parent with the required dependencies.  In this example, viagra gay the container sees the published dependency that the MovieLister has on the IMovieFinder, viagra gay and automatically instantiates the IMovieFinder it knows about and injects it as it is created.  This case is somewhat trivial since the graph is only one or two levels deep, viagra gay but I know you've ran into code before where this would be useful.

Service Location with a DI container

Viagra gay Sometimes it is useful to use the service locator pattern, viagra gay I'm certainly not refuting that.  Its definitely possible, viagra gay and is usually the easiest way to get introduced to using a DI container actually.  In fact, viagra gay the last example uses the container in exactly that fashion when pulling the MovieLister out of the container.  The program is using the container as the service location facility.  The key being though, viagra gay that we're leveraging the robustness of the container to autowire and instantiate anything else it needs rather than requiring each dependency to wire themselves up.

Viagra gay It is also worth mentioning that Microsoft has released a common container interface called the CommonServiceLocator, viagra gay for helping framework developers abstract their container choice away from client developers, viagra gay so they can pick whichever container (or roll their own) that they want.

Viagra gay Hopefully this clears something up for someone, viagra gay and doesn't just muddy the waters.

Average Rating: 4.9 out of 5 based on 182 user reviews.

Comments (1) Trackbacks (0)
  1. I agree, I don’t really like the service locator pattern. It looks like your example code is using Autofac.

    We’ve been using autofac for a few weeks now with a lot of success. It’s really helped to point our dependencies and to move us away from using statics and singletons. It also makes testing much easier. We were using Typemock which can do things like mock out statics and new’s, but now we are using Rhino mocks more (the Rhino AAA syntax is really nice).


Leave a comment


No trackbacks yet.