In a simple application, I have the code below that works to some extent, very well. However, for each Service that is instantiated for the Form, a new unit of work is created, consequently a new Context. So while the user is working on form, he does not see other updates that other users have done in other instances. Even updating the lists.
I have already tested to do a Refresh / Reaload at the moment of obtaining the data, however, it only updates with the data of the bank in the main entity, and not of the lists contained in that entity. I have also tried using the context or unit of work as Singleton, but, in addition to not solving the problem, in my point of view it is a bad practice.
The way it is, when I update the List entity it generates the error:
An entity object can not be referenced by multiple instances of IEntityChangeTracker
As I understand it when the Form is instantiated, it creates 2 instances of the work unit, 1 for each service.
I also understand that it should create a unit of work, with its context, and use it for all services instantiated to the Form. As well as performing the Dispose of these services on leaving it (which does not occur).
Has anyone gone through the implementation of something with these concepts and give a light?
program.cs
static void Main(string[] args)
{
Container container = new Container();
container.Options.DefaultScopedLifestyle = new LifetimeScopeLifestyle();
container.Register(typeof(IServiceBase<>), typeof(ServiceBase<>));
container.Register<IVisaoService, VisaoService>();
container.Register<IListaService, ListaService>();
container.Register<ITipoListaService, TipoListaService>();
container.Register<IProcedimentoService, ProcecdimentoService>();
container.Register(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
container.Register<IVisaoRepository, VisaoRepository>();
container.Register<IListaRepository, ListaRepository>();
container.Register<ITipoListaRepository, TipoListaRepository>();
container.Register<IProcedimentoRepository, ProcedimentoRepository>();
container.Register<IUnitOfWork, UnitOfWork>();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(Global._container.GetInstance<Principal>());
}
Form
private IVisaoService _visaoService;
private IListaService _listaService;
public Form(IVisaoService visaoService, IListaService listaService)
{
_visaoService = visaoService;
_listaService = listaService;
InitializeComponent();
}
private void CarregaDados()
{
List<Visao> visoes = _visaoService.ObtemTodos().OrderByDescending(c => c.Numero_Visao).ToList();
//Carrega os dados
}
private void Renomear()
{
lista.Descrisao = txtDescicao.Text;
_listaService.Atualizar(lista);
}
Service
public class ListaService : ServiceBase<Lista>, IListaService
{
private readonly IUnitOfWork _uow;
public ListaService(IUnitOfWork uow)
: base(uow, uow.ListaRepository)
{
_uow = uow;
}
}
Repository
public class ListaRepository : RepositoryBase<Lista>, IListaRepository
{
private readonly Contexto Db;
public ListaRepository(Contexto db) : base(db)
{
Db = db;
}
}
Unit of work
public class UnitOfWork : IUnitOfWork
{
private bool _disposed;
private Contexto _db;
private IVisaoRepository _visaoRepository;
private IListaRepository _listaRepository;
public UnitOfWork(Contexto db)
{
_db = db;
}
public IVisaoRepository VisaoRepository
{
get { return _visaoRepository = _visaoRepository ?? new VisaoRepository(_db); }
}
public IListaRepository ListaRepository
{
get { return _listaRepository = _listaRepository ?? new ListaRepository(_db); }
}
public void Commit()
{
_db.SaveChanges();
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_db.Dispose();
}
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}