Skip to content

Commit efbe361

Browse files
committed
Update UoW pattern examples
1 parent d1aa3ea commit efbe361

File tree

18 files changed

+62
-78
lines changed

18 files changed

+62
-78
lines changed

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Domain/Customer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
public class Customer : Entity
44
{
5-
public string FirstName { get; set; }
6-
7-
public string LastName { get; set; }
8-
}
5+
public string FirstName { get; set; } = string.Empty;
6+
public string LastName { get; set; } = string.Empty;
7+
}

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Domain/Entity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public abstract class Entity
44
{
5-
public int Id { get; set; }
6-
}
5+
public int Id { get; init; }
6+
}

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Domain/Order.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
public class Order : Entity
44
{
5-
public string Description { get; set; }
6-
7-
public string DeliveryAddress { get; set; }
8-
5+
public string Description { get; set; } = string.Empty;
6+
public string DeliveryAddress { get; set; } = string.Empty;
97
public decimal Price { get; set; }
10-
}
8+
}

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example1/Controllers/OrderController1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public OrderController1(IUnitOfWork1 unitOfWork)
1414

1515
public Order Create(int id, string description, string deliveryAddress, decimal price)
1616
{
17-
// ID is usually auto-genereated by database.
18-
var newOrder = new Order()
17+
// ID is usually auto-generated by database.
18+
var newOrder = new Order
1919
{
2020
Id = id,
2121
Description = description,

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example1/Example1Executor.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,23 @@ void CreateNewOrder()
5656

5757
private static void InitializeDatabase()
5858
{
59-
using (var context = new OrderManagementContext1())
60-
{
61-
context.Database.EnsureDeleted();
62-
context.Database.EnsureCreated();
59+
using var context = new OrderManagementContext1();
60+
context.Database.EnsureDeleted();
61+
context.Database.EnsureCreated();
6362

64-
var emma = new Customer { Id = 1, FirstName = "Emma", LastName = "Johnson" };
65-
var marc = new Customer { Id = 2, FirstName = "Marc", LastName = "Milston" };
63+
var emma = new Customer { Id = 1, FirstName = "Emma", LastName = "Johnson" };
64+
var marc = new Customer { Id = 2, FirstName = "Marc", LastName = "Milston" };
6665

67-
var camera = new Order { Id = 1, Description = "Camera", DeliveryAddress = "Address 1", Price = 550 };
68-
var wallet = new Order { Id = 2, Description = "Wallet", DeliveryAddress = "Address 2", Price = 10 };
66+
var camera = new Order { Id = 1, Description = "Camera", DeliveryAddress = "Address 1", Price = 550 };
67+
var wallet = new Order { Id = 2, Description = "Wallet", DeliveryAddress = "Address 2", Price = 10 };
6968

70-
var unitOfWork = new UnitOfWork1(context);
69+
var unitOfWork = new UnitOfWork1(context);
7170

72-
unitOfWork.CustomerRepository.Add(emma);
73-
unitOfWork.CustomerRepository.Add(marc);
74-
unitOfWork.OrderRepository.Add(camera);
75-
unitOfWork.OrderRepository.Add(wallet);
71+
unitOfWork.CustomerRepository.Add(emma);
72+
unitOfWork.CustomerRepository.Add(marc);
73+
unitOfWork.OrderRepository.Add(camera);
74+
unitOfWork.OrderRepository.Add(wallet);
7675

77-
unitOfWork.SaveChanges();
78-
}
76+
unitOfWork.SaveChanges();
7977
}
80-
}
78+
}

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example1/Infrastructure/IUnitOfWork1.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ namespace UnitOfWorkLibrary.Example1.Infrastructure;
66
public interface IUnitOfWork1
77
{
88
IRepository1<Customer> CustomerRepository { get; }
9-
109
IRepository1<Order> OrderRepository { get; }
1110

1211
void SaveChanges();
13-
}
12+
}

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example1/Infrastructure/OrderManagementContext1.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace UnitOfWorkLibrary.Example1.Infrastructure;
66
public class OrderManagementContext1 : DbContext
77
{
88
public DbSet<Customer> Customers { get; set; }
9-
109
public DbSet<Order> Orders { get; set; }
1110

1211
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example1/Infrastructure/UnitOfWork1.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ namespace UnitOfWorkLibrary.Example1.Infrastructure;
77
/// <summary>
88
/// The unit of work class serves one purpose:
99
/// to make sure that when you use multiple repositories, they share a single database context.
10-
/// That way, when a unit of work is complete you can call the SaveChanges method on that instance
10+
/// That way, when a unit of work is completed you can call the SaveChanges method on that instance
1111
/// of the context and be assured that all related changes will be coordinated.
1212
/// </summary>
13-
/// </summary>
1413
public class UnitOfWork1 : IUnitOfWork1
1514
{
1615
private readonly OrderManagementContext1 _context;
17-
private IRepository1<Customer> _customerRepository;
18-
private IRepository1<Order> _orderRepository;
16+
private IRepository1<Customer>? _customerRepository;
17+
private IRepository1<Order>? _orderRepository;
1918

2019
public UnitOfWork1(OrderManagementContext1 context)
2120
{

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example2/Controllers/OrderController2.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public OrderController2(IOrderRepository orderRepository)
1414

1515
public async Task<Order> CreateAsync(int id, string description, string deliveryAddress, decimal price)
1616
{
17-
// ID is usually auto-genereated by database.
18-
var newOrder = new Order()
17+
// ID is usually auto-generated by database.
18+
var newOrder = new Order
1919
{
2020
Id = id,
2121
Description = description,
@@ -31,5 +31,5 @@ public async Task<Order> CreateAsync(int id, string description, string delivery
3131

3232
public IEnumerable<Order> GetAll() => _orderRepository.GetAll();
3333

34-
public Order GetTheMostExpensive() => _orderRepository.GetTheMostExpensive();
35-
}
34+
public Order? GetTheMostExpensive() => _orderRepository.GetTheMostExpensive();
35+
}

src/AdditionalPatterns/UnitOfWork/UnitOfWorkLibrary/Example2/Example2Executor.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,24 @@ async Task CreateNewOrderAsync()
5959

6060
private static async Task InitializeDatabaseAsync()
6161
{
62-
using (var context = new OrderManagementContext2())
63-
{
64-
context.Database.EnsureDeleted();
65-
context.Database.EnsureCreated();
62+
using var context = new OrderManagementContext2();
63+
context.Database.EnsureDeleted();
64+
context.Database.EnsureCreated();
6665

67-
var emma = new Customer { Id = 1, FirstName = "Emma", LastName = "Johnson" };
68-
var marc = new Customer { Id = 2, FirstName = "Marc", LastName = "Milston" };
66+
var emma = new Customer { Id = 1, FirstName = "Emma", LastName = "Johnson" };
67+
var marc = new Customer { Id = 2, FirstName = "Marc", LastName = "Milston" };
6968

70-
var camera = new Order { Id = 1, Description = "Camera", DeliveryAddress = "Address 1", Price = 550 };
71-
var wallet = new Order { Id = 2, Description = "Wallet", DeliveryAddress = "Address 2", Price = 10 };
69+
var camera = new Order { Id = 1, Description = "Camera", DeliveryAddress = "Address 1", Price = 550 };
70+
var wallet = new Order { Id = 2, Description = "Wallet", DeliveryAddress = "Address 2", Price = 10 };
7271

73-
var customerRepository = new CustomerRepository(context);
74-
var orderRepository = new OrderRepository(context);
72+
var customerRepository = new CustomerRepository(context);
73+
var orderRepository = new OrderRepository(context);
7574

76-
customerRepository.Add(emma);
77-
customerRepository.Add(marc);
78-
orderRepository.Add(camera);
79-
orderRepository.Add(wallet);
75+
customerRepository.Add(emma);
76+
customerRepository.Add(marc);
77+
orderRepository.Add(camera);
78+
orderRepository.Add(wallet);
8079

81-
await orderRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync();
82-
}
80+
await orderRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync();
8381
}
84-
}
82+
}

0 commit comments

Comments
 (0)