[英]Domain Driven Design question
我是DDD的新手,如果我没有正确使用这些条款,请原谅我。
我正在使用C#MS / SQL和NHibernate。
我有一个班级电话付款,这笔付款有一个PaymentCurrency,每个都是数据库中的一个实体。
好。 在我的域模型中,我希望能够创建付款作为任何一种
Payment p = new Payment( 100 ) // automatically uses the default currency (defined in the db )
要么
Payment p = new Payment( 100, Repository.GetCurrency( "JPY" ) ) // uses Yen defined in the db.
但在我看来,为了使用dfault货币初始化我的域对象,我需要使用持久性知识来污染域模型。 即在我完成默认的Payment Constructor之前,我需要从db加载Default Payment对象。
我想象的构造函数就像是
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.
}
谢谢你的建议。
我认为没有一个完美的解决方案,但你可以通过将默认货币(和类似的属性)存储在其他类(例如“DomainDefaults”)并将其初始化来避免将持久性代码放入域类中另一段代码,逻辑上位于域对象“上方”。 当然,您必须确保在创建任何域对象之前调用初始化代码。 如果没有初始化,它应该抛出一个异常,所以你至少可以很容易地捕获它。
所以构造函数变成了
public Payment( int amount )
{
Currency = DomainDefaults.DefaultCurrency;
}
在初始化NHibernate之后的某个地方,你会打电话给:
DomainDefaults.DefaultCurrency = Repository.GetCurrency("JPY")
我不明白为什么这与持久性有任何关系。
如果我用Java编写这个,我会从应用程序的Locale中获取默认货币。
这个问题告诉我CultureInfo是C#的等价物。 也许你应该在你的设计中尝试,并留下像“JPY”和持久性的字符串。
你的答案是依赖注入 。
使用它并且不要使用配置来规范模型。 如果您需要创建具有所需金额和货币的付款 - 只需这样做:
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();
}
因此,您可以默认情况下将该货币提供者(使用Windsot,Unity或其他)注入您实例化付款的方法:
var p = new Payment(100.00m, defaultCurrencyProvider);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.