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();
}
}
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
}
}
class bootstrapper
{
Initialize()
{
//Build unity container..
//set resolver..
var parser = new OrderParser(container.Resolve<IOrderRepository>, container.Resolve<IContext>)
//can getAll, cant commit.
}
}
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.
To answer your question, there is no way. by definition, private constructors are inaccessible by other classes.
Frameworks that apply the Constrained Construction anti-pattern can make using Constructor Injection difficult. The main disadvantage to Constructor Injection is that if the class you're building is called by your current application framework, you might need to customize that framework to support it.
Types of Dependency Injection The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.
A view component class: Supports constructor dependency injection. Doesn't take part in the controller lifecycle, therefore filters can't be used in a view component.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With