Skip to content

Commit 1fe67e9

Browse files
committed
Update repository pattern examples
1 parent e2421a9 commit 1fe67e9

File tree

10 files changed

+25
-31
lines changed

10 files changed

+25
-31
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public OrderController(IOrderRepository orderRepository, IUnitOfWork unitOfWork)
1616

1717
public Order Create(int id, string description, string deliveryAddress, decimal price)
1818
{
19-
// ID is usually auto-genereated by database.
20-
var newOrder = new Order()
19+
// ID is usually auto-generated by database.
20+
var newOrder = new Order
2121
{
2222
Id = id,
2323
Description = description,
@@ -34,6 +34,6 @@ public Order Create(int id, string description, string deliveryAddress, decimal
3434
public IEnumerable<Order> GetAll() =>
3535
_orderRepository.GetAll();
3636

37-
public Order GetTheMostExpensive() =>
37+
public Order? GetTheMostExpensive() =>
3838
_orderRepository.GetTheMostExpensive();
3939
}

src/AdditionalPatterns/Repository/OrderManagement/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/Repository/OrderManagement/Domain/Entity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
public abstract class Entity
44
{
55
public int Id { get; set; }
6-
}
6+
}

src/AdditionalPatterns/Repository/OrderManagement/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/Repository/OrderManagement/Infrastructure/Repositories/Contracts/ICustomerRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ namespace OrderManagement.Infrastructure.Repositories.Contracts;
44

55
public interface ICustomerRepository : IRepository<Customer>
66
{
7-
}
7+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace OrderManagement.Infrastructure.Repositories.Contracts;
44

55
public interface IOrderRepository : IRepository<Order>
66
{
7-
Order GetTheMostExpensive();
8-
}
7+
Order? GetTheMostExpensive();
8+
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
using System.Linq.Expressions;
2-
using OrderManagement.Domain;
1+
using OrderManagement.Domain;
2+
using System.Linq.Expressions;
33

44
namespace OrderManagement.Infrastructure.Repositories.Contracts;
55

66
public interface IRepository<T>
77
where T : Entity
88
{
99
T Add(T entity);
10-
1110
IEnumerable<T> GetAll();
12-
1311
IEnumerable<T> Get(Expression<Func<T, bool>> predicate);
14-
1512
T GetById(int id);
16-
1713
void Delete(T entity);
18-
}
14+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
public interface IUnitOfWork
44
{
55
void Commit();
6-
}
6+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public OrderRepository(OrderManagementContext context)
1010
{
1111
}
1212

13-
public Order GetTheMostExpensive() =>
13+
public Order? GetTheMostExpensive() =>
1414
_context.Orders
1515
.OrderByDescending(order => order.Price)
1616
.FirstOrDefault();
17-
}
17+
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ protected Repository(OrderManagementContext context)
2424
}
2525

2626
public virtual T Add(T entity) =>
27-
_context
28-
.Add(entity)
29-
.Entity;
27+
_context.Add(entity).Entity;
3028

3129
public virtual IEnumerable<T> GetAll() =>
3230
_context.Set<T>().ToList();
@@ -37,8 +35,11 @@ public virtual IEnumerable<T> Get(Expression<Func<T, bool>> predicate) =>
3735
.Where(predicate)
3836
.ToList();
3937

40-
public virtual T GetById(int id) =>
41-
_context.Find<T>(id);
38+
public virtual T GetById(int id)
39+
{
40+
var entity = _context.Find<T>(id);
41+
return entity ?? throw new ArgumentException($"Entity with {id} doesn't exists");
42+
}
4243

4344
public virtual void Delete(T entity) =>
4445
_context.Remove(entity);

0 commit comments

Comments
 (0)