lundi 25 novembre 2019

Proper Separation of Concerns When Using UnitOfWork?

I followed an online tutorial of the UnitOfWork pattern with Entity Framework as it has been awhile since I have used it. I am confused as to why in the tutorial the DataContext is a parameter of the public UnitOfWork constructor. This means if I use the UnitOfWork in another layer of the application, the other layer must be aware of the DataContext. This does not seem like a good separation of concerns. Am I missing something?

UnitOfWork:

 public class UnitOfWork : IUnitOfWork
    {
        private readonly PurchasingDataContext _context;

        public UnitOfWork(PurchasingDataContext context)
        {
            _context = context;
            Items = new ItemRepository(_context);
            Manufacturers = new LabelerRepository(_context);
            Quotes = new QuoteRepository(_context);
            Vendors = new VendorRepository(_context);
            Contacts = new ContactRepository(_context);
        }



        public IItemRepository Items { get; private set; }
        public ILabelerRepository Manufacturers { get; private set; }
        public IQuoteRepository Quotes { get; private set; }
        public IVendorRepository Vendors { get; private set; }
        public IContactRepository Contacts { get; private set; }

        public int Complete()
        {
            return _context.SaveChanges();
        }

        public void Dispose()
        {
            _context.Dispose();
        }
    }

Interface:

public interface IUnitOfWork : IDisposable
    {
        IContactRepository Contacts { get; }
        IItemRepository Items { get; }
        ILabelerRepository Manufacturers { get; }
        IQuoteRepository Quotes { get; }
        IVendorRepository Vendors { get; }
        int Complete();
    }

Aucun commentaire:

Enregistrer un commentaire