10
I’m putting together a project that I use Uow to facilitate the issue of transactions. In some scenarios, I use several repositories, persisting the data in memory to save at once in the BD using a Commit() method of the class Unitofwork. I’m also using Entity Framework, to which I receive an Interface Idbcontext in the builder of Unitofwork and use a set of Dbset and the very Savechanges() framework to join the Commit().
Now let’s get to the question, I’m not using the interface Idisposable in Unitofwork and wanted to know if there is need since I am using Ioc (with Simpleinjector). Another point is that I have seen many articles using Idisposable in the repositories, but I am using a single instance per request, if I close the database connection and in the same request call another repository maybe release a Exception. I think what I lack is knowledge compared to this.
Iunitofwork interface:
public interface IUnitOfWork
{
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
void Commit();
}
Unitofwork class
public class UnitOfWork : IUnitOfWork //Deveria implementar IDisposable aqui ?
{
private readonly IDbContext _dbContext;
public UnitOfWork(IDbContext dbContext)
{
_dbContext = dbContext;
}
public IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
return _dbContext.Set<TEntity>();
}
public void Commit()
{
_dbContext.SaveChanges();
}
}
Idbcontext interface:
public interface IDbContext
{
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
}
Dbcontext:
public class MeuDbContext : IDbContext
{
public MeuDbContext() : base("MeuDbContextConnectionString")
{
//Configurações ...
}
public DbSet<ClasseA> ClasseA { get; set; }
public DbSet<ClasseB> ClasseB { get; set; }
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (DbEntityValidationException exception)
{
var ex = HandleDbEntityValidationException(exception);
throw ex;
}
catch (DbUpdateException exception)
{
var ex = HandleDbUpdateException(exception);
throw ex;
}
}
}
Ioc:
public class MeuProjetoBootstrap
{
public static void RegisterServices(Container container)
{
container.Register<IDbContext, QueensberryDbContext (Lifestyle.Scoped);
container.Register<IUnitOfWork, UnitOfWork>(Lifestyle.Scoped);
}
}
Setting:
public class MeuExemploApplication()
{
private readonly IUnitOfWork _uow;
private readonly IClasseARepositorio _classeARepositorio;
private readonly IClasseBRepositorio _classeBRepositorio;
public void MeuExemploApplication(IUnitOfWork uow, IClasseARepositorio classeARepositorio, IClasseBRepositorio classeBRepositorio)
{
//Utilizacao de IoC
_uow = uow;
_classeARepositorio = classeARepositorio;
_classeBRepositorio = classeBRepositorio;
}
public void Adicionar(DadosViewModel model)
{
// Deveria utilizar *using* aqui ? Algo como:
// using(_uow) { ... código citado abaixo ... }
var classeA = new ClasseA()
{
PropriedadeUm = model.PropriedadeUmA,
PropriedadeDois = model.PropriedadeDoisA
}
_classeARepositorio.Adicionar(classeA);
var classeB = new ClasseB()
{
PropriedadeUm = model.PropriedadeUmB,
PropriedadeDois = model.PropriedadeDoisB
}
_classeBRepositorio.Adicionar(classeB);
_uow.Commit();
}
}
I’m not using the interface Idisposable nowhere. Where should I close the connection to the Database ? In the Unit Of Work class ? And if so, how to do this using Ioc and without the need to call a Dispose() method for each operation.
OBS: I don’t think it’s in a repository, because if I call two repositories in a single operation, maybe the first repository called closes the connection to the database and how I’m using an instance for request this would give a problem to the following repository.
http://answall.com/questions/51536/quando-usar-entity-framework-com-repository-pattern/80696#80696
– Leonel Sanches da Silva
Nice... but there’s a question. In a scenario like DDD (where Eric Evans himself teaches about repositories), you think it’s bad practice even if it’s used in this question ? Is it best practice to replace the repositories with Extesions Methods ? I believe that the book was created even before C# offered resources like Extesions Methods and it is based on a tool like Java that I do not know if I have an alternative. If you could clear that up, I’d appreciate it.
– Wilson Santos
"you think it’s bad practice even being used in this question?". Yes. You read the answer?
– Leonel Sanches da Silva
"Is it best practice to replace the repositories with Extesions Methods ? I believe the book was created even before C# offered features like Extesions Methods and it is based on a tool like Java that I don’t know if I have an alternative." The question is not about Java: it is about C# using Entity Framework. There is no Extensions for Java. There is no Entity Framework for Java. If we were talking about Java and Java repositories, there would be no problem of practices in your question. I ask you once again to re-read the answer linked to your question.
– Leonel Sanches da Silva