GithubHelp home page GithubHelp logo

ans-ashkan / dapper.datarepositories Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elninjagaiden/dapper.datarepositories

0.0 1.0 0.0 527 KB

Base structures to implement Data Repository pattern upon Dapper as MicroOrm.

C# 100.00%

dapper.datarepositories's Introduction

Dapper.DataRepositories

There is some pretty cool stuff happening out there: Micro ORMs like Dapper, Repository Pattern as nice and usefull option to implement data layers and async programming as a way to improve the performance of our code. This package combines them and lets you to implement the repository pattern easily and fast.

This package contains 3 components:

  • DataConnection: It's just a database connection wrapper. It could be useful for your no conventional data repositories, especially for those that does not needs to implement all CRUD operations. It will be the base class for all the data repositories directly or indirectly.
  • IDataRepository: the contract base for the data repositories that needs to implement CRUD operations (and more).
  • DataRepository: the base class for your data repositories that needs to implement CRUD operations (and more).

How do I use those things?

Lets say we have this POCO definition:

[StoredAs("Users")]
public class User
{
	[KeyProperty(Identity = true)]
	public int Id { get; set; }
	
	public string Login { get; set;}
	
	[StoredAs("FName")]
	public string FirstName { get; set; }
	
	[StoredAs("LName")]
	public string LastName { get; set; }
	
	public string Email { get; set; }
	
	public DateTime DateOfBirth { get; set; }
	
	[StatusProperty]
	public UserStatus Status { get; set; }
	
	[NonStored]
	public string FullName
	{
		get
		{
			return string.Format("{0} {1}", FirstName, LastName);
		}
	}
}

public enum UserStatus : byte
{
	Registered = 1,
	
	Active = 2,
	
	[Deleted]
	Inactive = 3
}

NOTE: for more information about how to define the metadata of your POCO's check the MicroOrm.Pocos.SqlGenerator package.

Using the elements of this package, you only need to do a couple of things in order to create a data repository for this "User" POCO.

First, create the repository contract inheriting from IDataRepository:

public interface IUsersRepository : IDataRepository<User>
{
    //IUsersRepository is inheriting all CRUD operations 
}

Then, implements the repository:

public class UsersRepository : DataRepository<User>, IUsersRepository
{
    //NOTE: Because this is a "Dependency Injection Oriented Package"
    //we need to pass the database connection and the SQL Generator as parameters
    public UsersRepository(IDbConnection connection, ISqlGenerator<User> sqlGenerator)
        : base(connection, sqlGenerator)
    {
    }
}

Simple as that, we have defined a fully functional data repository for the "User" POCO. Because the inheritance pattern we are doing here, both repository contract and repository implementation contains this functions:

IEnumerable<User> GetAll();

Task<IEnumerable<User>> GetAllAsync();

IEnumerable<User> GetWhere(object filters);

Task<IEnumerable<User>> GetWhereAsync(object filters);

User GetFirst(object filters);

Task<User> GetFirstAsync(object filters);

bool Insert(User instance);

Task<bool> InsertAsync(User instance);

bool Update(User instance);

Task<bool> UpdateAsync(User instance);

bool Delete(object key);

Task<bool> DeleteAsync(object key);

No SQL. No repeated code. Three pretty basic steps:

  • Decorate your POCO's
  • Define your repository contracts
  • Implement your repositories.

All those functions are virtual at DataRepository level so you can easily override them. Lets say we have a heavy custom stored procedure to insert users called "sp_AddUser". If we want to use that stored procedure, we only need to overwrite the "Insert" function at repository level, like this:

public class UsersRepository : DataRepository<User>, IUsersRepository
{
    public UsersRepository(IDbConnection connection, ISqlGenerator<User> sqlGenerator)
        : base(connection, sqlGenerator)
    {
    }
    
    //Custom insert implementation
    public bool override Insert(User instance)
    {
        var newId = Connection.Query<int>("sp_AddUser", instance, commandType: CommandType.StoredProcedure).Single();
        instance.Id = newId;
        return newId > 0;
    }
}

And what if I want to handle more functions rather than CRUD operations? Easy, define them at repository contract level and implement them at repository level, like this:

//Repository contract
public interface IUsersRepository : IDataRepository<User>
{
    //Totally custom function
    User GetOlderUser();
}

//Repository implementation
public class UsersRepository : DataRepository<User>, IUsersRepository
{
    public UsersRepository(IDbConnection connection, ISqlGenerator<User> sqlGenerator)
        : base(connection, sqlGenerator)
    {
    }
    
    //Totally custom function
    public User GetOlderUser()
    {
        return Connection.Query<User>("sp_GetOlderUser", commandType: CommandType.StoredProcedure).FirstOrDefault();
    }
}

Don't want/need all CRUD operations?

There are some situations when we don't need all CRUD operations. If that' the case, we only need to do a couple of variants:

  • Don't inherit from IDataRepository at repository contract level and include in it only the operations that you need
  • Inherit from DataConnection instead of DataRepository at repository implementation level

Example:

//POCO definition
[StoredAs("EventLogs")]
public class EventLog
{
	[KeyProperty(Identity = true)]
	public long Id { get; set; }
	
	public string Description { get; set;}
	
	public DateTime AddedOn { get; set; }
}

//Repository contract definition
public interface IEventLogsRepository
{
	void Insert(EventLog instance);
}

//Repository implementation
public class EventLogsRepository : DataConnection
{
	public EventLogsRepository(IDbConnection connection)
		: base(connection)
	{
	}
	
	public void Insert(EventLog instance)
	{
		...
	}
}

dapper.datarepositories's People

Contributors

ans-ashkan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.