Generic Repository with Dapper

2

I'm doing a test application where I want to see the performance and learn how to create a generic repository with Dapper, well I have some experience when I created a generic repository, but using EF6, trying to use the same logic made the construction almost the same form the main classes, are as follows:

IRepository Interface

public interface IRepositorio<TEntity> where TEntity : class
{
    dynamic Adicionar(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null);
    bool Atualizar(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null);
    bool Deletar(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null);
    TEntity ObterPorId(int id, IDbTransaction transaction = null, int? commandTimeout = null);
    IEnumerable<TEntity> ObterTodos(IDbTransaction transaction = null, int? commandTimeout = null);
    IEnumerable<TEntity> ObterPor(object where = null, object order = null, IDbTransaction transaction = null, int? commandTimeout = null);

}

The Repository class that implements my interface:

public class Repository<TEntity> : IRepositorio<TEntity>, IDisposable where TEntity : class
{
    public IDbConnection Conn { get; set; }

    public Repository(IDapperContexto context)
    {
        Conn = context.Connection;
    }

    public dynamic Adicionar(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return entity == null ? null : Conn.Insert(entity, transaction, commandTimeout);
    }

    public bool Atualizar(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return entity != null && Conn.Update(entity, transaction, commandTimeout);
    }

    public bool Deletar(TEntity entity, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return entity != null && Conn.Delete(entity, transaction, commandTimeout);
    }

    public TEntity ObterPorId(int id, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return Conn.Get<TEntity>(id, transaction, commandTimeout);
    }

    public IEnumerable<TEntity> ObterTodos(IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return Conn.GetList<TEntity>(null, null, transaction, commandTimeout);
    }

    public IEnumerable<TEntity> ObterPor(object where = null, object order = null, IDbTransaction transaction = null, int? commandTimeout = null)
    {
        return Conn.GetList<TEntity>(@where);
    }

    public void Dispose()
    {
        GC.SuppressFinalize(this);
    }
}

The IDapperContext interface

 public interface IDapperContexto : IDisposable
{
    IDbConnection Connection { get; }
}

DapperContext class that implements my interface

public class DapperContexto : IDapperContexto
{
    private readonly string _connectionString;
    private IDbConnection _connection;


    public DapperContexto()
    {
        _connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    }

    public IDbConnection Connection
    {
        get
        {
            if (_connection == null)
            {
                _connection = new SqlConnection(_connectionString);

            }
            if (_connection.State != ConnectionState.Open)
            {
                _connection.Open();
            }
            return _connection;
        }
    }

    public void Dispose()
    {
        if (_connection != null && _connection.State == ConnectionState.Open)
            _connection.Close();
    }
}

Well, done that I thought that it would be enough to instantiate the class Repository and pass on it the model that should persist the data, as follows:

private void btnLoad_Click(object sender, EventArgs e)
    {
        var rec = new Repository<Customer>().ObterTodos();

        dgvData.DataSource = rec;
    }

At this point the questions began, remembering that some characteristics are important: Existing SqlServer Database with Customer Sample Table I used Model Generator T4 to create the classes based on the database, in this case only the Customer table

When instantiating the Repository class I get an error:

Then finally the doubt is that I am not able to pass expected parameter, since the class is expecting a type IDapperContext, does anyone have any suggestions?

    
asked by anonymous 13.12.2018 / 21:34

0 answers