Canada Online Pharmacy Viagra

12Mar/091

Overnight Canadian Viagra

Overnight canadian viagra 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):

Overnight canadian viagra Which DI container/framework should I choose?

Overnight canadian viagra His answer:

Overnight canadian viagra None; just roll your own and use simple Service Location

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

Overnight canadian viagra What are the reasons I use to decide whether or not to use a DI container?

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

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

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

  • Published/declared dependencies
  • Autowiring

Published dependencies vs implicit dependencies

Overnight canadian viagra Below is an illustration of two constructors.  The first uses published dependencies, overnight canadian viagra and the latter uses implicit.

public MovieLister( IMovieFinder finder )

{

    // DI style

    _finder = finder;

}

 

public MovieLister()

{

    // service locator style

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

}

Overnight canadian viagra  

Overnight canadian viagra The first is more easily testable and usable, overnight canadian viagra 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, overnight canadian viagra with this example, overnight canadian viagra you're tied to the implementation of the service locator, overnight canadian viagra and also to using the CsvMovieFinder key (I suppose this key could be in AppConfig or some other configuration mechanism, overnight canadian viagra so it could actually be configurable).

Overnight canadian viagra I agree it can be just as simple to inject mocks into your service locator for testing the implicit example, overnight canadian viagra 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, overnight canadian viagra because then the client developer knows exactly what is expected.

Autowiring

Overnight canadian viagra When dependencies are published, overnight canadian viagra 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, overnight canadian viagra but it truly is one of the best features that comes from using a DI container.

Overnight canadian viagra 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, overnight canadian viagra CsvMovieFinder>("movies.txt");

    }

 

    public void Run()

    {

        // uses autowiring to inject CsvMovieFinder

        // into MovieLister

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

 

        lister.List( Console.Out );

    }

}

Overnight canadian viagra  

Overnight canadian viagra The container builds up a dependency graph for the requested object, overnight canadian viagra and walks it bottom-up, overnight canadian viagra supplying each parent with the required dependencies.  In this example, overnight canadian viagra the container sees the published dependency that the MovieLister has on the IMovieFinder, overnight canadian viagra 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, overnight canadian viagra but I know you've ran into code before where this would be useful.

Service Location with a DI container

Overnight canadian viagra Sometimes it is useful to use the service locator pattern, overnight canadian viagra I'm certainly not refuting that.  Its definitely possible, overnight canadian viagra and is usually the easiest way to get introduced to using a DI container actually.  In fact, overnight canadian viagra 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, overnight canadian viagra 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.

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

Overnight canadian viagra Hopefully this clears something up for someone, overnight canadian viagra and doesn't just muddy the waters.

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.