seed_87
seed_87

Reputation: 179

Using Dependency Injection outside of a Controller's constructor

So here's the issue, my mvc3 project uses Dependency Injection and has a base Generic IRepository class from which other repositories derive.

So I can co ahead and do this in a controller:

public class SomethingController
{
    IOrderRepository repository;

    public SomethingController(IOrderRepository repo)
    {
        this.repository = repo;
    }

    public ActionResult SaveOrder(Order order)
    {
        repository.add(order)
        unitOfWork.CommitChanges();  // THIS works!
    }
}

But now i need to use one of those repositories in a custom static non-controller like this:

static class OrderParser
{
    private IOrderRepository repo;

    public static DoWork()
    {
        repo = DependencyResolver.Current.GetService<IOrderRepository>();

        var ordersInDB = repo.GetAllOrders(); //THIS works!

        //But!
        var ordersForInsertion = new List<Order>();


        //do some backgroundworker magic                     
        //fetch txt files from an ftp server
        var ordersForInsertion = ParseTextFilesIntoOrders();

        foreach order in ordersForInsertion 
             repo.add(order)
        unitOfWork.CommitChanges();
        // THIS doesnt commit anything into the database
        // It also doesnt throw any exceptions
        // and repo isnt null or any of that
    }
}

So, as a test, i tried doing:

repo = DependencyResolver.Current.GetService<IOrderRepository>();

inside a controller class like in the first example to see if it also didnt commit stuff, and it doesn't. (Doing it the right way [injecting repositories and the unitOfWork trough the constructors] works!)

So it has to be something to do with the DependencyResolver, right?

Note: if there is any more code you need me to post, ask away and I'll edit it in here in a flash!

Note2: Thanx!

EDIT1:

Regarding w0lf's super fast answer Here's some more info:

My OrderParser class implments a backgroundWorker which is supposed to:

All that has to happen without any user action, meaning, the action is not originated from a controller, hence all I do is:

in my bootstrapper class

Initialise()
{
    //Unrelated stuff
    OrderParser.DoWork()
}

And that's also why I implemented it as a static class ( easily changable to a non-static )

EDIT2:

It would be something like:

class OrderParser
{
    private IOrderRepository repo;

    public OrderParser(IOrderRepository foo)
    {
        this.repo = foo;
    }
    public static DoWork()
    {
        //use repo var!
    }
}

But then when i instance it in the bootstrapper Initialize() method, how would i do that, e.g.:

class bootstrapper
{
    Initialize()
    {
        var parser = new OrderParser(/*how do i pass the dependency here?*/)
        parser.DoWork();
    }
}

EDIT3:

Here's some more testing, please bear with me!

Here's my OrderParser again:

class OrderParser
{
    public OrderParser(IOrderRepository foo, IContext unitOfWork)
    {
        foo.getall(); 

        foo.add(some_order);
        unitOfWork.commit(); 

    }
}

Test1:

public class SomeController
{
    IOrderRepository repository;

    public SomeController(IOrderRepository repo)
    {
        this.repository = repo;
    }

    public ActionResult SomeMethod(Order order)
    {
        repository.GetAll();    //WORKS

        repository.add(order)
        unitOfWork.CommitChanges();  // WORKS
    }
}

TEST2:

class bootstrapper
{
    Initialize()
    {
        //Build unity container..
        //set resolver..

        var parser = new OrderParser(container.Resolve<IOrderRepository>, container.Resolve<IContext>)
        //can getAll, cant commit.
    }
}

TEST3:

public class SomeController
{
    IOrderRepository controllers_repository;

    public SomeController(IOrderRepository repo)
    {
        this.controllers_repository = repo;
    }

    public ActionResult SomeMethod(Order order)
    {
        var parser = new OrderParser(DependencyResolver.Current.GetService<IOrderRepository>,
        DependencyResolver.Current.GetService<IContext>)   
        //can do getall, no commits


        var parser = new OrderParser(controllers_repository, controllers_icontext)
        // obviously works (can getall and commit)
    }
}

By the way, when i say "can't commit" it's not that i get an exception or the repositories are null, nope. the code runs as if it were okay, only the DB won't change.

Upvotes: 8

Views: 10873

Answers (3)

Adam Tuliper
Adam Tuliper

Reputation: 30162

Since this is a background task, don't run this in a web application. Instead use a service or scheduled application in windows. Youc an then resolve your reference during application initialization or using a [Dependency] attribute

http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.100).aspx http://stackoverflow.com/questions/888479/using-unity-framework-inject-into-system-windows-forms-form-page

Upvotes: 1

Cristian Lupascu
Cristian Lupascu

Reputation: 40576

One possible solution is to make the OrderParser class non-static and inject an instance of it in the constructor of the Controller that triggers the action (DoWork).

Then make OrderParser's constructor take an IOrderRepository parameter and the IoC container will gladly take care of it.

Also, beware of things like:

DependencyResolver.Current.GetService<ISomeInterface>();

This is called Service Locator and it's considered to be an anti-pattern. Avoid it if possible.

Basically, the only place where you should reference DependencyResolver.Current.GetService is your implementation of IControllerFactory that enables DI in the first place.

Update:

It would be best if you did this in another application than your MVC website. Two alternatives would be:

  • a Windows Service that performs that action based on a timer
  • a Console application that is run using Windows Task Scheduler every hour

These, being separate applications would have their own Composition roots that would deal with the object instantiation / dependency injection issue.

If, however, you are constrained to do this from your web app (for example - you have a hosting that only allows web apps), then you may find it acceptable to make an exception to the "Don't use the Dependencey Resolver directly" rule and do somehing like this on the application startup:

var runner = DependencyResolver.Current.GetService<OrderParsingRunner>();
runner.StartWorking();

Of course, the OrderParsingRunner class would look something like this:

public class OrderParsingRunner
{
    private readonly OrderParser orderParser;

    public OrderParsingRunner(OrderParser orderParser)
    {
        this.orderParser = orderParser;
    }

    public StartWorking()
    {
        TaskFactory.StartNew(() => 
            { 
                DoWorkHourly();
            });
    }

    private DoWorkHourly()
    {
        while(true)
        {
            Thread.Sleep(TimeSpan.FromHours(1));

            orderParser.DoWork();
        }
    }
}

Disclaimer: I haven't actually compiled/run this code, I just wrote it to illustrate the concept.

Please note that this is a workaround rather than an actual solution. It's recommended that you use another application for the background tasks if possible.

Upvotes: 6

Antony Scott
Antony Scott

Reputation: 22006

You shouldn't need static helper classes when using DI. You can treat everything as a "service" and declare your dependencies in your constructor. That's how I think about it. Then everything just gets created for you as you need it.

So I would change your static class to a non-static and inject it where needed via the constructor.

Answer for Edit 2

Pass your container in to the bootstrap class.

class bootstrapper
{
    Initialize(DependencyResolver container)
    {
        var parser = new OrderParser(container.Resolve<IOrderRepository>());
        parser.DoWork();
    }
}

Edit

I would actually do this ...

var parser = container.Resolve<OrderParser>();

and let the dependency resolver figure everything out!

Upvotes: 2

Related Questions