I'm new to DDD and trying to wrap my head around it by writing a prototype for an application (Core-Logic-Layer).
My domain model looks like this atm. (I leaved out parts, that weren't necessary for the problem):
So we have an aggregate root DebitAccount
that has assigned Estimations
(about costs / income that will occure in a given interval). It also has assigned Postings
that physically occured (Cost / Income) at a given date.
A Posting
may occure with an estimation assigned(e.g."We have monthly costs for paying the rent"), but it also may occure unforseen ("My notebook broke, so I need to buy a new one.")
Here is the (imho) necessary code to implement the model:
public class DebitAccount : BaseEntity
{
protected ICollection<Estimation> _Estimations;
protected ICollection<Posting> _Postings;
public virtual IEnumerable<Estimation> Estimations
{
get
{
return new List<Estimation>(_Estimations);
}
}
public virtual IEnumerable<Posting> Postings
{
get
{
return new List<Posting>(_Postings);
}
}
public void Post(Posting posting)
{
var associatedEstimation = _Estimations.FirstOrDefault(e => e == posting.BelongsToEstimation);
if(associatedEstimation == null)
{
var now = _DateProvider.GetCurrentDate();
var guid = Guid.NewGuid();
associatedEstimation = new Estimation(new Posting[] { posting }, new Amount(0, _CurrencyProvider.GetDefaultCurrency()), posting.PostingName, new DayInterval(now, now, 0),new EstimationId(guid));
_Estimations.Add(associatedEstimation);
}
_Postings.Add(posting);
}
public DebitAccount TransferAmountTo(DebitAccount to, Amount amount, string transferName)
{
var currentDate = _DateProvider.GetCurrentDate();
var fromPosting = new Posting(amount.Invert(),transferName, currentDate, null);
var toPosting = new Posting(amount.Clone() ,transferName, currentDate, null);
this.Post(fromPosting);
to.Post(toPosting);
return this;
}
}
There are several problems I'm facing:
When calling the
Post
-Method of a debit account, how to get theEstimation
to which the posting belongs (if there is any)? In the application code, one could get all estimations by the public property, find the correct one, and assign it to the current posting. But this requires the client to know, that he have to look for the estimation in the property.The next problem is similar to the first one: when transferring to another
DebitAccount
, how to find the correspondingEstimation
(if any) ?TransferTo
-Function: Where and how to persist bothDebitAccounts
. Shall the calling code (e.g. Application layer) assume, that implicitly the passedDebitAccount
(to
) was changed (call-by-reference) and just persist both of them in a repo?
How could these problems be addressed? Or maybe I'm wrong and my DDD-design is flawed? Any idea, comment and thought is appreciated :)