Reputation: 7444
Say I have a one to many relationship in my database between orderstatus and orders. My view for creating an new order would need to have a dropdown of orderstatuses.
I have separate repositories for order and order status as well as seperate services for manipulating orders and order statuses. Something like:
public class OrderService : IOrderService
{
private readonly IRepository<Order> _orderRepository;
public OrderService(IRepository<Order> orderRepository) {_orderRepository = orderRepository }
public IEnumerable<Orders> GetAllOrders(){...}
}
public class OrderStatusService : IOrderStatusService
{
private readonly IRepository<OrderStatus> _OrderStatusRepository;
public OrderStatusService(IRepository<OrderStatus> orderStatusRepository) {_orderStatusRepository = orderStatusRepository }
public IEnumerable<OrderStatus> GetAllOrderStatuses(){...}
}
My order controller has a reference to the OrderService, a bit like this:
public class OrderController : Controller
{
private readonly IOrderService orderService;
What is the best way to do get a list of orderstatuses from the db?
1) Include a reference to both repositories in the OrderService and include a method that will return orderstatuses.
public class OrderService : IOrderService
{
private readonly IRepository<Order> _OrderRepository;
private readonly IRepository<OrderStatus> _OrderStatusRepository; ...
2) Make the controller aware of both services and use the GetOrderStatus method to get the list of OrderStatuses:
public class OrderController : Controller
{
private readonly IOrderService orderService;
private readonly IOrderService orderStatusService; ...
3) Use the OrderStatusService from the OrderService to get the list of Order Statuses, something like:
public class OrderService : IOrderService
{
private readonly IRepository<Order> _orderRepository;
private readonly IOrderService _orderService; ...
public IEnumerable<OrderStatus> GetOrderStatuses()
{ return _orderService.GetOrderStatuses; } ...
4) Another cool way that I cant think of :)
Upvotes: 7
Views: 2990
Reputation: 3944
I'm sorry, but I can't why an order status needs to a class at all. Why isn't the order status an enum or some such?
In any event, do not fall into the trap of trying to devise a way to project your database into an exposed service layer so that all entities and relationships are exposed with no aggregation or value-adding logic.
By doing this, basically you move normalised data from where it is best manipulated (the database) into an application environment where the best paradigm is an object/domain centered approach, not a normalised approach.
Simply put, your service should be designed in terms of the domain, not in terms of your database tables, so I would have only the only order "service" that had methods "AddOrder", "UpdateOrder", "DeleteOrder", "SearchOrders" etc...
To get a list of order statuss' have another service, maybe called a MetadataService, that returned a DTO containing all the enums or enum-like things for your application.
Hope this helps!
Upvotes: -1