Transactional data access layer using UnitOfWork and Repository patterns against Entity Framework 6 DbContext and IDbSet.
Key features:
  • completely loose coupled from the Entity Framework, query against an interface, not against the DbContext.
  • IDbContext gives you full unit test and mocking support.
  • nested unit of works
  • generic repositories
  • generates plain pocos with DataAnnotations and WCF support


1. Add Nuget package NQueries to your project

2. Download the T4 Templates for NQueries

3. Replace the templates from your model with the NQueries templates

4. Setup your Service Locator to provide your context as IDataContext

5. Look at the sample in the source

Code Samples

Apart from the sample here there is a running (yet incomplete) sample project within the solution, that demonstrates basic setup and different use cases.

Update an entity

public void UpdateProdukt(Guid productId, string productName)
    using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
        Connector product = unitOfWork.DataContext.Set().Single(x => x.Id == productId);
        product.Name = productName;

Unit test for update

public void UpdateProduktTest()
    Product product1 = new Product { Id = Guid.NewGuid() };
    Product product2 = new Product { Id = Guid.NewGuid() };

    MockTools.StubRepository(new[] { product1, product2 });

    ProductService target = new ProductService();

    target.UpdateProdukt(product2.Id, "newName");

    Assert.AreNotEqual("newName", product1.Name);
    Assert.AreEqual("newName", product2.Name);

Last edited Oct 5, 2015 at 2:43 PM by Ymris, version 37