Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. public abstract class BaseService<TUnitOfWork> : IService where TUnitOfWork : IUnitOfWork, new()
  2. {
  3. protected readonly IUnitOfWork _uow;
  4. protected readonly ILogger _log;
  5.  
  6. public BaseService()
  7. {
  8. _uow = new TUnitOfWork();
  9. _log = new Logger(GetType().FullName);
  10. //...
  11. }
  12.  
  13. // Other base protected & private methods
  14. }
  15.  
  16. public class FooService : BaseService, IFooService
  17. {
  18. public void CreateFoo(FooDTO foo)
  19. {
  20. FooEntity fooEntity = Mapper.Map<FooDTO, FooEntity>(foo);
  21.  
  22. // Insert Foo entity
  23. _uow.FooRepository.Insert(fooEntity);
  24. _uow.Commit();
  25. }
  26. }
  27.  
  28. public class BarService : BaseService, IBarService
  29. {
  30. public void CreateBar(BarDTO bar)
  31. {
  32. // Insert Bar entity
  33. BarEntity barEntity = Mapper.Map<BarDTO, BarEntity>(bar);
  34. _uow.BarRepository.Insert(barEntity);
  35.  
  36.  
  37.  
  38. // После чего необходимо вызвать FooService.CreateFoo(fooDTO)
  39. IFooService fooService = ???
  40. fooService.CreateFoo(new FooDTO()); // только для примера создается пустой FooDTO-объект
  41. // При этом действие должно выполниться одной транзакцией
  42. // т.е. полчается, что _uow.Commit() в методе FooService.CreateFoo не должен сработать,
  43. // а должен сработать _uow.Commit для всех сущностей (и Bar и Foo) тот что ниже??
  44.  
  45. _uow.Commit();
  46. }
  47. }
  48.  
  49. public interface IExampleUnitOfWork
  50. {
  51. IRepository<Foo> FooRepository { get; }
  52. IBarRepository BarRepository { get; }
  53. }
  54.  
  55. public class ExampleUnitOfWork : BaseUnitOfWork<ExampleContext>, IExampleUnitOfWork
  56. {
  57. public IRepository<Foo> FooRepository { get { return GetGenericRepository<Foo>(); } }
  58. public IBarRepository BarRepository { get { return GetCustomRepository<BarRepository>(); } }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement