Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain Driven Design question

I'm new to DDD so please forgive me if I'm not using the terms correctly.

I am Using C# MS/SQL and NHibernate.

I have a class call Payment and this payment has a PaymentCurrency each of these is an entity in the database.

OK. In my Domain Model I want to be able to create Payment as Either

Payment p = new Payment( 100 ) // automatically uses the default currency (defined in the db )

or

Payment p = new Payment( 100, Repository.GetCurrency( "JPY" ) ) // uses Yen defined in the db.

But it seems to me that in order to initialize my Domain Object with the dfault currency I need to pollute the domain model with knowledge of persistance. i.e. before I can completed the default Payment Constructor I need to load the Default Payment object from the db.

The constructor I visualize is somehting like

public Payment( int amount ) {
   Currency = Repository.LoadDefaultCurrency();  // of cource defualt currency would be a singleton
}

public Payment( int amount, Currency c ) {
   Currency = c; // this is OK since c is passed in from outside the domain model.
}

Thanks for your advice.

like image 602
thrag Avatar asked Dec 10 '25 12:12

thrag


1 Answers

I don't think there's a perfect solution to this, but you can avoid putting the persistence code into domain classes by having the default currency (and similar properties) stored on some other class (eg. "DomainDefaults") and have it initialised from another piece of code that logically sits "above" the domain objects. Of course, you'd have to make sure that initialisation code is called before any domain objects can be created. It should probably throw an exception if not initialised, so you can at least catch this easily.

So the constructor becomes

public Payment( int amount )
{
   Currency = DomainDefaults.DefaultCurrency;
}

And somewhere soon after you initialise NHibernate you'd call:

DomainDefaults.DefaultCurrency = Repository.GetCurrency("JPY")
like image 107
EMP Avatar answered Dec 13 '25 20:12

EMP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!