Normally the code for using the dbContext
from Entity Framework looks something like this:
using (var context = new SMDbContext())
{
context.StartTransaction();
var userCrudLogic = new UserEntityCrudLogic(context);
userCrudLogic.CreateUser(...);
context.SaveChanges();
context.Commit();
}
In my case I want to use this in a web service, and instead of calling my CRUD logic I would like to call a business logic to do some validation and other things. The business logic afterwards will call the CRUD logic.
My problem is that I don't know how to store or pass the context to the CRUD logic. The easiest way would be to use a parameter but I don't want to have a unneeded parameter in all my business logic functions.
I think it would be better to have something like an "context manager" which is responsible for storing and providing the context for the current request. Than I could use IoC or something like that to load the context wherever its needed.
But how should such a "manager" look like and how/where I have to store it?
If I register it in IoC as singleton (in global.asax
), will it be shared over all requests or is it unique for each?
Sorry if my question seems stupid or something like that, but I have never done such things before and want to learn them now! So I don't want to just make it working, I want to know what the best way to do it. If something is unclear, please just ask!
Greetings