Domain related issue

I am new to DDD, so please forgive me if I do not use these terms correctly.

I am using C # MS / SQL and NHibernate.

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

OK In my domain model, I want to be able to create a "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 durability. that is, before I can complete the default payment constructor, I need to load the "Default Payment" object from db.

The constructor that I am visualizing looks 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.

+3
3

, , , ( ), - (, "DomainDefaults" ), , "" . , , , - . , , , , , .

,

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

- NHibernate :

DomainDefaults.DefaultCurrency = Repository.GetCurrency("JPY")
+2

, - .

Java, Locale .

SO , CultureInfo #. , , "JPY" .

+2

.

. - :

var currency = injectedCurrencyRepository.GetByName("JPY");
var p = new Payment(100.00m, currency);

.

inteface:

public interface ICurrencyProvider {
   Currency GetCurrency();
}
// Implment the default:
public class DefaultCurrencyProvider : ICurrencyProvider {
   public Currency GetCurrency() {
      return new Currency("AUD");
   }
}

// And the Payment constructor will look like:
public Payment(decimal amount, ICurrencyProvider currencyProvider) {
   c = currencyProvider.GetCurrency();
}

INJECT ( Windsot, Unity - ), , Payment:

var p = new Payment(100.00m, defaultCurrencyProvider);
0
source

Source: https://habr.com/ru/post/1725484/


All Articles