Skip to content

Latest commit

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

DataAccess

Repository and Unit Of Work implementation over a relational database.

Overview

This library provides an abstraction and implementation of data access to a relational database. It targets two main goals:

  • support for consistent development model for reading or modifying data in the entire application
  • enforce good separation of concerns by separating data access from business logic

The implementation is done with Entity Framework, but the client code should not have any direct dependencies (reference assemblies) to it. The client code depends only on the abstract interface provided by the library and does not know details about the underlying ORM.

Usage Patterns

IRepository is a generic interface for querying data for read-only purposes:

privatereadonlyIRepositoryrep;// injected w/ Dependency InjectionpublicIEnumerable<Order>GetAllLargeOrders(intamount){varorders=rep.GetEntities<Order>().Where(o =>o.OrderLines.Any(ol =>ol.Amount>amount)
return orders.ToList();}

Queries may be returned to be composed and executed by caller code:

privatereadonlyIRepositoryrep;// injected w/ Dependency InjectionprivateIQueriable<Order>GetAllLargeOrders(intamount){varorders=rep.GetEntities<Order>().Where(o =>o.OrderLines.Any(ol =>ol.Amount>amount)
return orders;}
public IEnumerable<OrderSummary>GetRecentLargeOrders(intamount){intthisYear= DateTime.UtcNow.Year;var orders =GetAllLargeOrders(amount).Where(o.Year==thisYear).Select(o =>newOrderSummary{
...});returnorders;}

IUnitOfWork is a generic interface for creating a well defined scope for adding, modifying or deleting data:

publicvoidReviewLargeAmountOrders(intamount,ReviewDatadata){using(IUnitOfWorkuof=rep.CreateUnitOfWork()){IQueryable<Order>orders=uof.GetEntities<Order>().Where(o =>o.OrderLines.Any(ol =>ol.Ammount>amount);foreach(varorderinorders){order.Status=Status.Reviewed;order.Cutomer.Name=data.CustomerNameUpdate;
...}ReviewEventre=newReviewEvent{...}uof.Add(re)
uof.SaveCanges();}}

Getting Started

Create a data model project which contains the POCOs that are mapped to the database model. This will also contain the Entity Framework DbContext.

Register into your Dependency Injection container the following mappings:

ContractImplementation
IDbContextFactoryDbContextFactory<MyEntities>
IRepositoryRepository
IInterceptorsResolverInterceptorsResolver

Related Repository

iQuarc.AppBoot

About

Repository and Unit Of Work implementation

Resources

Stars

19 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages