How to implement Generic Repository Design Pattern with Dapper?

Sure, a function to create and dispose your Connection will work great. protected void Execute(Action<IDbConnection> query) { using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings[“myDB”].ConnectionString)) { query.Invoke(db); } } And your simplified call site: public void SaveCustomer(CustomerDTO custDTO) { Execute(db => db.Execute(saveCustSp, custDTO, CommandType.StoredProcedure)); } With Return Values: public T Get<T>(Func<IDbConnection, T> query) { using (IDbConnection db … Read more

Is it ok for entities to access repositories?

it’s not a horrible violation of DDD it’s a horrible violation of… well… it’s just plain horrible (i say this tongue in cheek) :). First off, your entity becomes dependent on having a repository… that’s not ideal. Ideally you’d want to have your repository create the Person and then assign it everything it needs to … Read more

using stored procedure in entity framework

You need to create a model class that contains all stored procedure properties like below. Also because Entity Framework model class needs primary key, you can create a fake key by using Guid. public class GetFunctionByID { [Key] public Guid? GetFunctionByID { get; set; } // All the other properties. } then register the GetFunctionByID … Read more

What is the difference between DAO and Repository patterns?

DAO is an abstraction of data persistence. Repository is an abstraction of a collection of objects. DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain, dealing only in Aggregate Roots. Repository could be implemented using DAO‘s, but you wouldn’t do the opposite. Also, a Repository is … Read more

Difference between Repository and Service Layer?

Repository Layer gives you additional level of abstraction over data access. Instead of writing var context = new DatabaseContext(); return CreateObjectQuery<Type>().Where(t => t.ID == param).First(); to get a single item from database, you use repository interface public interface IRepository<T> { IQueryable<T> List(); bool Create(T item); bool Delete(int id); T Get(int id); bool SaveChanges(); } and … Read more

EF Including Other Entities (Generic Repository pattern)

Use just the Include extension on IQueryable. It is available in EF 4.1 assembly. If you don’t want to reference that assembly in your upper layers create wrapper extension method in your data access assembly. Here you have example: public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params Expression<Func<T, object>>[] includes) where T : class { if … Read more

Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable

Start with you DbContext, create a new file called Database.cs: Database.cs public class Database : DbContext { private IDbSet<Post> _posts; public IDbSet<Post> Posts { get { return _posts ?? (_posts = DbSet<Post>()); } } public virtual IDbSet<T> DbSet<T>() where T : class { return Set<T>(); } public virtual void Commit() { base.SaveChanges(); } } Define … Read more

How to implement Unit Of Work pattern with Dapper?

This Git project is very helpful. I started from the same and did some changes as per my need. public sealed class DalSession : IDisposable { public DalSession() { _connection = new OleDbConnection(DalCommon.ConnectionString); _connection.Open(); _unitOfWork = new UnitOfWork(_connection); } IDbConnection _connection = null; UnitOfWork _unitOfWork = null; public UnitOfWork UnitOfWork { get { return _unitOfWork; … Read more