At the beginning, you need to prepare your environment and set up a test Northwind database https://northwinddatabase.codeplex.com.
Let's use a console application to demonstrate the example. Target framework for all projects should be 4.5.1.
  • Create a new Console Application project.
CreateNewConsoleApp.PNG
  • Add a new “Domain Model” class library project that represents layer that contain your domain types.
AddDomainModel.PNG
  • Add “dcp.DDD.DomainModel” nugget package to Domain Model project.
addNugetDomainModel.PNG
  • Add a new “Infrastructure.Data” class library project that will contain your type for accessing database (base on EntityFramework). Add reference to “Domain Model” project.
AddInfrastructure.PNG
  • Add “dcp.DDD.DataInfrastructure” nugget package to “Infrastructure.Data” project.
AddNugetInfrastructure.PNG
  • Add a data model of Northwind database to “Infrastructure.Data” project.
AddModel.PNG
  • Correctly move Northwind.tt template that generates domain entities to Domain Model project.
ModelEntities.PNG
  • Extend generated NorthwindEntities class with IUnitOfWork contract.
    public partial class NorthwindEntities : IUnitOfWork
    {
        public void Commit()
        {
            SaveChanges();
        }
    }
  • Add IOrderRepository contract to Domain layer and inherit it from IRepository<Order> that placed in dcp.DDD.DomainModel.dll.
public interface IOrderRepository : IRepository<Order>    {    }
  • Implement this IOrderRepository contract. Create OrderRepository class ant inherit it from RepositoryBase<Order> that placed in dcp.DDD.DataInfrastructure.dll.
    public class OrderRepository : RepositoryBase<Order>, IOrderRepository
    {
        public OrderRepository(IUnitOfWork unitOfWork) : base(unitOfWork, x => x.OrderID)
        {
        }
    }
Note, that you need specify primary key properties of entity when invoke base constructor.

That’s all. Your data access layer is completed.

In next example you can see examples how to use the repository.
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new NorthwindEntities())
            {
                 context.Configuration.LazyLoadingEnabled = false;                
                 IOrderRepository orderRepository = new OrderRepository(context);
 
                //find single result by key
                var order = orderRepository.Find(10248);
 
                //find single result projection by key
                var orderProjection = orderRepository.Find(10248, projection: x => new
                {
                    OrderId = x.OrderID,
                    CustomerName = x.Customer.ContactName
                });
 
                //find single result by key with eager loaded Customer association
                order = orderRepository.Find(10248, includePaths: x => x.Customer);
 
                //find single result projection by key with eager loaded Customer association
                var orderProjection2 = orderRepository.Find(10248, projection: x => new
                {
                    OrderId = x.OrderID,
                    Order = x,
                    CustomerName = x.Customer.ContactName
                }, includePaths: x => x.Customer);
 
                //find results by predicate
                var orders = orderRepository.FindBy(x => x.ShipCountry == "Russia");
 
                //find result projections by predicate
                var orderProjections = orderRepository.FindBy(x => x.ShipCountry == "Russia", projection: x => new
                {
                    OrderId = x.OrderID,
                    CustomerName = x.Customer.ContactName
                });
 
                //find result projections by predicate
                var orderProjections2 = orderRepository.FindBy(x => x.ShipCountry == "Russia", projection: x => new
                {
                    OrderId = x.OrderID,
                    CustomerName = x.Customer.ContactName
                }, includePaths: x => x.Customer);
            }
        }
    }
Description of all repository’s methods see in documentation.

Of course you can use a dependency injection container and inject your IOrderRepository contract to your services, controllers and etc.

You can download example by https://dcpddd.codeplex.com/downloads/get/1437395.

Last edited Mar 10, 2015 at 1:06 PM by ziic, version 3