So here is the problem: my mvc3 project uses Injection Dependency and has a base common class IRepository from which other repositories are derived.
So, I can go ahead and do it in the controller:
public class SomethingController { IOrderRepository repository; public SomethingController(IOrderRepository repo) { this.repository = repo; } public ActionResult SaveOrder(Order order) { repository.add(order) unitOfWork.CommitChanges();
But now I need to use one of these repositories in a custom static non-controller:
static class OrderParser { private IOrderRepository repo; public static DoWork() { repo = DependencyResolver.Current.GetService<IOrderRepository>(); var ordersInDB = repo.GetAllOrders();
So, as a test, I tried to do:
repo = DependencyResolver.Current.GetService<IOrderRepository>();
inside the controller class, as in the first example, to see if it also didn’t pass the material, and it doesn’t. (Doing this the right way [injecting repositories and unitOfWork through constructors] works!)
So this should be due to DependencyResolver, right?
Note: if there is still some code that I need to publish, ask away, and I will edit it here instantly!
Note2: Thanx!
EDIT1:
Regarding quick quick response w0lf Here is some more info:
In my OrderParser class, backgroundWorker is applied, which should:
- Sleep for an hour
- List all the files (plain text files) on the FTP server.
- Discard those already parsed in db.
- Parse the new files into Order objects.
- Transfer objects to db.
- Start all over and over until the power goes out or something :)
All that should happen without any user action, i.e. the action does not come from the controller, so all I do is:
in bootstrapper class
Initialise() {
And that's why I implemented it as a static class (easily modifiable for non-static)
EDIT2:
It will be something like:
class OrderParser { private IOrderRepository repo; public OrderParser(IOrderRepository foo) { this.repo = foo; } public static DoWork() {
But then, when I instantiate it in the bootstrapper initialization method, how would I do it, for example:
class bootstrapper { Initialize() { var parser = new OrderParser() parser.DoWork(); } }
EDIT3:
Here are some more tests, please, with me.
Here is 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();
TEST2:
class bootstrapper { Initialize() {
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>)
By the way, when I say "can not commit", it is not that I am getting an exception, or the repositories are null, nope. the code works as if everything is in order, only the database will not change.