2
I’m doing a test application where I want to see performance and learn about creating a generic repository with Dapper, well I have some experience when creating a generic repository, but using EF6, trying to use the same logic I made the construction in almost the same way the main classes, are the following:
Irepositorio 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 Repositorio 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 Idappercontexto interface
public interface IDapperContexto : IDisposable
{
IDbConnection Connection { get; }
}
Class Dappercontexto implementing 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 I thought it would be enough to instantiate the Repositorio class and pass 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:
So finally the doubt is that I’m not able to pass the expected parameter, since the class is expecting an Idappercontexto type, does anyone have any suggestions?
failed to inform
public Repository(IDapperContexto context)
Dappercontexto in the constructor something like that:new Repositorio<Customer>(new DapperContexto())
but even so your layer seems to have some problems– novic
Honestly I would abandon this code, there are so many problems in it that I don’t even know where to start. I’m sorry, but there are conceptual errors, even fixing this will still have problems
– Maniero