This project is read-only.
To use Unit of Work pattern in your project you need write only sth like this:
IUnitOfWork UoW = new EFUnitOfWork<YourDbContext>();

Now, you can work with you UoW what is also very simple e.g. to get all Employee entities from DB (I assume you have Employee class in your model) you have to type:
var employees = UoW.Get<Employee>().ToList();

All methods from UnitOfWork library are generic where generic type is type of classes from your model. Passing model class to generic param you say UoW that it should use Repository for this type (UoW consist of repository of every type from your model).

It's recommended to keep classes like EFUnitOfWork in central place(it is class which we leverage in many places in out project) e.g. in base controller in ASP.NET MVc, or as item in collection of items from HttpContext. The same to creating and disposing this class e.g. create it in constructor of base controller and dispose it inside Dispose method of base controller.

Every know that two principles are very important when it comes to access to DB:
  1. you should open connection as late as possible
  2. you should close connection as soon as possible
UnitOfWork library concern about first principle for you it mean when you create new EFUnitOfWork class it doesn't open connection yet. Connection is opened when you execute any with access to db like insert, update, get etc.
What about second principle? In this case you can chose one of two way:
  • You can concern about it manually. IUnitOfWork include Dispose method so you can execute it where you think is right e.g. in Dispose method of base controller or when response is ending but you must remember about it!.
  • Second way is recomended by me because it ensure of fulfilling second principle. In this way UnifOfWork library provide special method -> Query. Inside this method you can execute other methods and connection is open before first method and close after last method. Query method take one parameter - Action delegate so you can create anonymous method using lambda with few method fom UoW e.g.:

UoW.Query(() => var emp = UoW.GetById<Employee>(1);
emp.Name = "Adam";

As I mentioned in this way you have 100% sure that 2 above principles is fulfilled. You didn't execute Dispose method in this approach.

For knowing with all method of my library you can download documentation in DOWNLOAD section.

Last edited Dec 4, 2011 at 3:04 PM by krzysiekfonal, version 3


No comments yet.