Repository<T>, Syntactic Sugar and DDD

time to read 5 min | 920 words

I am toying around with several ideas about the use of Repository<T> in DDD context. Right now it is theoretical because my current project has little need for DDD.

At the moment, I am using the static access for Repository<T> as my gateway for the repositories.

   1:  public class OrdersController : BaseController
   2:  {
   3:      public void CalculateTaxes(int customerId)
   4:      {
   5:          Customer customer = Repository <Customer>.Load(customerId);
   6:          ICollection<Order> orders = Repository<Order>.FindAll(Where.Order.Customer == customer &&
   7:                         Where.Order.Status == OrderStatus.ReadyToShip &&
   8:                         Where.Order.Date >= DateTime.Today);
   9:   
  10:          Employee lowestEmployeeResponsibleForCustomer = 
  11:              Repository<Employee>.FindFirst(Where.Employees.Customers.Contains(customer), 
  12:                  OrderBy.Employee.Level.Asc);
  13:   
  14:          foreach (Order order in orders)
  15:          {
  16:              customer.ApplyDiscount(order);
  17:              lowestEmployeeResponsibleForCustomer.Dispatch(order);
  18:          }
  19:      }
  20:  }

Another style may utilize a base controller as the place to put the repositories, like this:

image

The resulting code looks like this:

   1:  public class OrdersController : BaseController
   2:  {
   3:      public void CalculateTaxes(int customerId)
   4:      {
   5:          Customer customer = Customers.Load(customerId);
   6:          ICollection<Order> orders = Orders.FindAll(Where.Order.Customer == customer &&
   7:                         Where.Order.Status == OrderStatus.ReadyToShip &&
   8:                         Where.Order.Date >= DateTime.Today);
   9:   
  10:          Employee lowestEmployeeResponsibleForCustomer = 
  11:              Employees.FindFirst(Where.Employees.Customers.Contains(customer), 
  12:                  OrderBy.Employee.Level.Asc);
  13:   
  14:          foreach (Order order in orders)
  15:          {
  16:              customer.ApplyDiscount(order);
  17:              lowestEmployeeResponsibleForCustomer.Dispatch(order);
  18:          }
  19:      }
  20:  }
 
 

I really like the Repository<T> static accessor for stuff like tests that requires data setup, but I think that using it in the controllers may be over doing it. I like the second approach better, because the syntax doesn't have <T> all over the place, as well. It is also nicer from the point of view of syntax and it allows to replace the current IRepository<User> with IUserRepository very easily.

Thoughts?