Answer the question
In order to leave comments, you need to log in
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;
}
}
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();
}
}
}
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();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question