W
W
WhiteNinja2017-02-27 13:48:38
ASP.NET
WhiteNinja, 2017-02-27 13:48:38

C#: UnitOfWork factory implementation?

Good afternoon!
In the process of refactoring the current approaches and system architecture (N-Layer = DAL (Entities, Repositories, UnitOfWork) <-> BLL (DTO, Services) <-> Presentation (ASP.NET MVC / ASP.NET Web API 2)) it was decided move to the next implementation of the business logic layer (Services
)

public abstract class BaseService : IService
{
  private readonly ILogger _logger;
  private readonly IUnitOfWorkFactory _uowFactory;	

  public BaseUnitOfWorkService(ILogger logger, IUnitOfWorkFactory uowFactory)
  {
    _logger = logger;
    _uowFactory = uowFactory;
  }
}

Notification Service
public class NotificationService : BaseService, INotificationService
{
  public NotificationService(ILogger logger, 
    IUnitOfWorkFactory uowFactory, 
    IRepository<Notification> notificationRepository) : base(logger, uowFactory)  
  { 
    _notificationRepository = notificationRepository;
  }
  
  public void CreateNotification(NotificationDTO notification)
  {			
    using(IUnitOfWork uow = uowFactory.Create())
    {
      _notificationRepository.Insert(notification);
      uow.Commit();
    }		
  }	
}

Ticket service
public class TicketService : BaseService, ITicketService
{
  private readonly ITicketRepository _ticketRepository;
  private readonly INotificationService _notificationService;
  
  public TicketService(ILogger logger, 
    IUnitOfWorkFactory uowFactory, 
    ITicketRepository ticketRepository, 
    INotificationService notificationService) : base(logger, uowFactory) 
  {
    _notificationService = notificationService;
    _ticketRepository = ticketRepository;
  }
  
  public TicketDTO CreateTicket(TicketDTO ticket)
  {			
    using(IUnitOfWork uow = _uowFactory.Create())
    {
      try
      {
        _ticketRepository.Insert(ticketEntity);					
        _notificationService.CreateNotification(new NotificationDTO());
        // uow.Commit() внутри метода CreateNotification не должен выполнить SaveChanges(),	
        // так как всё выполняется в рамках одной транзакции
        
        // А строчкой ниже выполнится общая транзакция
        uow.Commit();
      }			
      catch(Exception ex)
      {
        uow.Rollback();
      }
    }			
  }
}

But the question arose - how to implement UnitOfWorkFactory in relation to Entity Framework 6 along with Repository and UnitOfWork?
That is, how to get the current UnitOfWork (the one that is currently used by the factory) in the repositories?
Please show a brief implementation example, or send a link to such an example.
A similar approach is described in Alexander Bindu 's blog , but, unfortunately, it is not possible to download the source code there (
Thanks in advance for any help in this matter!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrDywar Pichugin, 2017-02-27
@Dywar

1) https://habrahabr.ru/post/321050/
2) https://habrahabr.ru/post/276593/
3) View code samples at https://code.msdn.microsoft.com/
- Unit of Work & Repositories Framework
- KiksApp Enterprise application architecture Onion
4) Pluralsight
- Become a Full-stack .NET Developer - Advanced Topics
- Become a Full-stack .NET Developer - Architecture and Testing
- Become a Full-stack .NET Developer - Fundamentals

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question