Skip to content

A set of extensions to ease the use of Dapper in a .NET project

Notifications You must be signed in to change notification settings

sirtopas/DapperProxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DapperProxy

A set of extensions to ease the use of Dapper in a .NET project

Installing

Simply either clone the repo or just copy the DapperProxy.cs and IDapperProxy.cs files into your project.

Usage

  1. Install Dapper using NuGet
  2. Create a BaseRepository:
    public abstract class BaseRepository
    {
        protected BaseRepository(IDapperProxy dapper)
        {
            Dappter = dapper;
        }
    
        protected IDapperProxy Dapper { get; }
    }
  3. Use this Base Repo in any other repository:
    public class MyRepository : BaseRepository
    {
        public MyRepository(IDapperProxy dapper) : base(dapper)
        {
        }
    
        public IEnumerable<MyObject> Search(int objectId)
        {
            var dict = new Dictionary<int, MyObject>();
            var myObjects = Dapper
                                .ClearParameters()
                                .WithStoredProcedure("SearchObjects")
                                .AddParameter("@ObjectId", objectId, DbType.Int32)
                                .Query<MyObject>((myObject) =>
                                    {
                                        if (!dict.TryGetValue(myObject.id, out var item))
                                        {
                                            item = myObject;
                                            dict.Add(item);
                                        }
                                        return item;
                                    },
                                    splitOn: "ObjectId")
                                .Distinct()
                                .ToList();
            return myObjects;
        }
    }

About

A set of extensions to ease the use of Dapper in a .NET project

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages