A
aa7im
I am trying to design a good domain model for a system I am working on
but I am having a hard time trying to figure out how to deal with
relations and transaction...
For example if I create an Invoice object and then add LineItem objects
to the invoice all in memory. How do I get everything to commit to the
database in the right order.
Invoice invoice = new Invoice();
invoice.OrderDate = DateTime.Now;
invoice.Comments = "Thank you for your order";
invoice.ToAddress = "Joe Blow 123 Drive Somewhere,CA.";
InvoiceItem item = new InvoiceItem();
item.Name = "Large Widget";
item.Price = 20.00;
item.Quantity = 5;
invoice.AddItem(item);
item = new InvoiceItem();
item.Name = "Small Widget";
item.Price = 15.00;
item.Quantity = 2;
invoice.AddItem(item);
invoice.Save();
One way is to implement by Save() method like this:
public void Save()
{
MapperClass.SaveInvoice(this);
foreach(InvoiceItem item in Invoice.InvoiceItems)
{
MapperClass.SaveItem(item,this.InvoiceID);
}
}
The problem is that when looking at an existing Invoice object items
like: invoice.InvoiceItems it will return a collection of InvoiceItems
from the database (lazy loaded). I need users of the object model to
be able to insert, update, and delete objects in this collection and
when Save is called figure out what to do...
I can't decide how and where to track the state of objects related the
invoice object. For example when an object is removed from a
collection I still need sitting around somewhere so when I commit to
the database I know to actually remove from the DB. If was completely
removed from the collection entirely I wouldn't have any to know that
it was removed.
Where and how to I track the state of the related objects?
As properties on all my entity objects (I set the properties when items
are added and remove from collections)?
Should my invoice object maintain seperate internal arrays of objects
that need to be removed and inserted for each related object?
I have read about using a "Unit of Work" object, is this a solution to
the problem?
Thanks
but I am having a hard time trying to figure out how to deal with
relations and transaction...
For example if I create an Invoice object and then add LineItem objects
to the invoice all in memory. How do I get everything to commit to the
database in the right order.
Invoice invoice = new Invoice();
invoice.OrderDate = DateTime.Now;
invoice.Comments = "Thank you for your order";
invoice.ToAddress = "Joe Blow 123 Drive Somewhere,CA.";
InvoiceItem item = new InvoiceItem();
item.Name = "Large Widget";
item.Price = 20.00;
item.Quantity = 5;
invoice.AddItem(item);
item = new InvoiceItem();
item.Name = "Small Widget";
item.Price = 15.00;
item.Quantity = 2;
invoice.AddItem(item);
invoice.Save();
One way is to implement by Save() method like this:
public void Save()
{
MapperClass.SaveInvoice(this);
foreach(InvoiceItem item in Invoice.InvoiceItems)
{
MapperClass.SaveItem(item,this.InvoiceID);
}
}
The problem is that when looking at an existing Invoice object items
like: invoice.InvoiceItems it will return a collection of InvoiceItems
from the database (lazy loaded). I need users of the object model to
be able to insert, update, and delete objects in this collection and
when Save is called figure out what to do...
I can't decide how and where to track the state of objects related the
invoice object. For example when an object is removed from a
collection I still need sitting around somewhere so when I commit to
the database I know to actually remove from the DB. If was completely
removed from the collection entirely I wouldn't have any to know that
it was removed.
Where and how to I track the state of the related objects?
As properties on all my entity objects (I set the properties when items
are added and remove from collections)?
Should my invoice object maintain seperate internal arrays of objects
that need to be removed and inserted for each related object?
I have read about using a "Unit of Work" object, is this a solution to
the problem?
Thanks