CRUD over aggregate root using traditional ado.net
-
19-09-2019 - |
Question
Can anyone show me simple CRUD statements for aggregate root using traditional ado.net? Thanks in advance!
Solution
(This is written on the assumption that a GUID or some non-database generated primary key is used) Also alot of boiler code such as connection management etc... should be moved to a base class for Repository. If Order is the aggregate root, one possibly should make OrderLineRepo private to the assembly
public class OrderRepository : Repository
{
public void Save(Order order)
{
if(order.IsDirty)
{
//sets up connection if required, command and sql
ICommand command = BuildCommandForSave(order);
command.Execute();
OrderLineRepository orderLineRepo = GetOrderLineRepo();
foreach(OrderLine line in order.OrderLines)
{
orderLineRepo.Save(line);
}
}
}
}
However I'd stress that this is really a simple naive implementation, and that I'd personally utilize an ORM like nHibernate for my persistence if doing DDD as the requirements for a good well tested persistence layer are non-trivial
Also this assumes that the IsDirty function takes children into account - we would also require a means to see if the order is new/edited, not just dirty