Skip to content

Commit 519f593

Browse files
committed
Show one of the possible ways to use UoW with repository pattern
1 parent 3f02024 commit 519f593

File tree

6 files changed

+83
-59
lines changed

6 files changed

+83
-59
lines changed

src/AdditionalPatterns/Repository/OrderManagement/Controllers/OrderController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ namespace OrderManagement.Controllers
77
public class OrderController
88
{
99
private readonly IOrderRepository _orderRepository;
10+
private readonly IUnitOfWork _unitOfWork;
1011

11-
public OrderController(IOrderRepository orderRepository)
12+
public OrderController(IOrderRepository orderRepository, IUnitOfWork unitOfWork)
1213
{
1314
_orderRepository = orderRepository;
15+
_unitOfWork = unitOfWork;
1416
}
1517

1618
public Order Create(int id, string description, string deliveryAddress, decimal price)
@@ -25,7 +27,7 @@ public Order Create(int id, string description, string deliveryAddress, decimal
2527
};
2628

2729
newOrder = _orderRepository.Add(newOrder);
28-
_orderRepository.SaveChanges();
30+
_unitOfWork.Commit();
2931

3032
return newOrder;
3133
}

src/AdditionalPatterns/Repository/OrderManagement/Executor.cs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,72 +9,80 @@ namespace OrderManagement
99
{
1010
public class Executor : PatternExecutor
1111
{
12+
private OrderManagementContext _context;
13+
private CustomerRepository _customerRepository;
14+
private OrderRepository _orderRepository;
15+
private UnitOfWork _unitOfWork;
16+
17+
private CustomerController _customerController;
18+
private OrderController _orderController;
19+
1220
public override string Name => "Repository - Data Access Pattern";
1321

1422
public override void Execute()
1523
{
24+
ConfigureDependencies();
1625
InitializeDatabase();
1726

18-
using var context = new OrderManagementContext();
19-
var customerRepository = new CustomerRepository(context);
20-
var orderRepository = new OrderRepository(context);
21-
22-
var customerController = new CustomerController(customerRepository);
23-
var orderController = new OrderController(orderRepository);
24-
2527
ShowAllCustomers();
2628
ShowAllOrders();
2729
CreateNewOrder();
2830
ShowAllOrders();
31+
}
2932

30-
void ShowAllCustomers()
31-
{
32-
Console.WriteLine("Showing all customers...");
33-
foreach (var customer in customerController.GetAll())
34-
{
35-
Console.WriteLine($"{customer.FirstName} {customer.LastName}");
36-
}
37-
}
38-
39-
void ShowAllOrders()
40-
{
41-
Console.WriteLine("\nShowing all orders...");
42-
foreach (var order in orderController.GetAll())
43-
{
44-
Console.WriteLine($"{order.Description} with price of {order.Price:C}");
45-
}
46-
}
33+
private void ConfigureDependencies()
34+
{
35+
_context = new OrderManagementContext();
36+
_customerRepository = new CustomerRepository(_context);
37+
_orderRepository = new OrderRepository(_context);
38+
_unitOfWork = new UnitOfWork(_context);
4739

48-
void CreateNewOrder()
49-
{
50-
Console.WriteLine("\nCreating new order...");
51-
orderController.Create(3, "PlayStation 5", "Address 3", 600);
52-
}
40+
_customerController = new CustomerController(_customerRepository);
41+
_orderController = new OrderController(_orderRepository, _unitOfWork);
5342
}
5443

5544
private void InitializeDatabase()
5645
{
57-
using (var context = new OrderManagementContext())
58-
{
59-
context.Database.EnsureDeleted();
60-
context.Database.EnsureCreated();
46+
_context.Database.EnsureDeleted();
47+
_context.Database.EnsureCreated();
48+
49+
var emma = new Customer { Id = 1, FirstName = "Emma", LastName = "Johnson" };
50+
var marc = new Customer { Id = 2, FirstName = "Marc", LastName = "Milston" };
6151

62-
var emma = new Customer { Id = 1, FirstName = "Emma", LastName = "Johnson" };
63-
var marc = new Customer { Id = 2, FirstName = "Marc", LastName = "Milston" };
52+
_customerRepository.Add(emma);
53+
_customerRepository.Add(marc);
6454

65-
var camera = new Order { Id = 1, Description = "Camera", DeliveryAddress = "Address 1", Price = 550 };
66-
var wallet = new Order { Id = 2, Description = "Wallet", DeliveryAddress = "Address 2", Price = 10 };
55+
var camera = new Order { Id = 1, Description = "Camera", DeliveryAddress = "Address 1", Price = 550 };
56+
var wallet = new Order { Id = 2, Description = "Wallet", DeliveryAddress = "Address 2", Price = 10 };
6757

68-
var customerRepository = new CustomerRepository(context);
69-
customerRepository.Add(emma);
70-
customerRepository.Add(marc);
71-
customerRepository.SaveChanges();
58+
_orderRepository.Add(camera);
59+
_orderRepository.Add(wallet);
60+
61+
_unitOfWork.Commit();
62+
}
7263

73-
var orderRepository = new OrderRepository(context);
74-
orderRepository.Add(camera);
75-
orderRepository.Add(wallet);
76-
orderRepository.SaveChanges();
64+
private void ShowAllCustomers()
65+
{
66+
Console.WriteLine("Showing all customers...");
67+
foreach (var customer in _customerController.GetAll())
68+
{
69+
Console.WriteLine($"{customer.FirstName} {customer.LastName}");
7770
}
7871
}
72+
73+
private void ShowAllOrders()
74+
{
75+
Console.WriteLine("\nShowing all orders...");
76+
foreach (var order in _orderController.GetAll())
77+
{
78+
Console.WriteLine($"{order.Description} with price of {order.Price:C}");
79+
}
80+
}
81+
82+
private void CreateNewOrder()
83+
{
84+
Console.WriteLine("\nCreating new order...");
85+
_orderController.Create(3, "PlayStation 5", "Address 3", 600);
86+
}
7987
}
8088
}

src/AdditionalPatterns/Repository/OrderManagement/Infrastructure/Repositories/Contracts/IRepository.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,5 @@ public interface IRepository<T>
1717
T GetById(int id);
1818

1919
void Delete(T entity);
20-
21-
void SaveChanges();
2220
}
2321
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace OrderManagement.Infrastructure.Repositories.Contracts
2+
{
3+
public interface IUnitOfWork
4+
{
5+
void Commit();
6+
}
7+
}

src/AdditionalPatterns/Repository/OrderManagement/Infrastructure/Repositories/Repository.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,5 @@ public virtual void Delete(T entity)
5555
{
5656
_context.Remove(entity);
5757
}
58-
59-
/// <summary>
60-
/// Save all changes to the database.
61-
/// Additionaly, we can implement UnitOfWork design pattern
62-
/// to enhance this example and coordinate database changes from one place.
63-
/// </summary>
64-
public void SaveChanges()
65-
{
66-
_context.SaveChanges();
67-
}
6858
}
6959
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using OrderManagement.Infrastructure.Repositories.Contracts;
2+
3+
namespace OrderManagement.Infrastructure.Repositories
4+
{
5+
public class UnitOfWork : IUnitOfWork
6+
{
7+
private readonly OrderManagementContext _context;
8+
9+
public UnitOfWork(OrderManagementContext context)
10+
{
11+
_context = context;
12+
}
13+
14+
public void Commit()
15+
{
16+
_context.SaveChanges();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)