0

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

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Dominik Kirschenhofer
  • 1,185
  • 1
  • 12
  • 26

2 Answers2

1

You probably must configure your web application with a context per request. Do a google and you'll get something like this Caching Entity Framework DbContexts per request and One DbContext per request in ASP.NET MVC (without IOC container)

Community
  • 1
  • 1
Jone Polvora
  • 1,728
  • 1
  • 19
  • 27
0

I'm pretty sure that if the web service client has cookies enabled and you are storing session data in process then you should be able to use the Session object to store it. I'm not sure how sensible or secure that is though.

wizzardmr42
  • 1,589
  • 9
  • 22