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:
- Sleep for an hour
- List all the files (plain txt files) in an FTP server.
- Discard the ones that are already parsed into the db.
- Parse the new files into Order objects.
- Commit the objects into db.
- Start all over and over till the power goes out or something :)
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.