在遵循 DDD 概念的同时,我正在努力决定是否应该让我的域本地化意识?我提出了两个解决方案来解决这个问题。两者都使域本地化能够在不同的地方实现。我是否应该将本地化文本放入域中?分享您对此问题的解决方案或我的两个示例的优缺点。谢谢。
实施例1
class Persion {
String name;
// other fields ommited
void rename(String newName) {
String oldName = this.name;
this.name = newName
// publish event with old name and new name
}
String name() {
return name;
}
}
class PersionRepository {
void store(Persion persion) {
Locale loc = LocaleContextHolder.get().getLocale();
// store object to DAO - create/update fields for context locale
}
// other methods ommited
}
实施例2
class Persion {
Map<Locale, String> name;
// other fields ommited
void rename(String newName) {
Locale locale = LocaleContextHolder.get().getLocale();
String oldName = this.name.put(locale, newName);
// publish event with old name and new name
}
String name() {
Locale locale = LocaleContextHolder.get().getLocale();
return this.name.get(locale);
}
}
class PersionRepository {
void store(Persion persion) {
// store object to DAO - create/update fields for all locales
}
// other methods ommited
}
在大多数情况下,最好的选择是从域中删除本地化。
域类应该只包含与其不变量相关的数据,因为它们负责业务规则。要检索本地化描述,请使用投影 DTO 和应用服务。
你可以使用这样的东西:
public final class VatCode {
private final String _code;
public VatCode(String code)
{
// VAT code validation here...
_code = code;
}
@Override
public String toString() {
return _code;
}
@Override
public boolean equals(Object obj) {
// ...
}
@Override
public int hashCode() {
// ...
}
}
public class Person {
private final VatCode _identifier;
public Person(VatCode identifier)
{
_identifier = identifier;
}
// some command and some query here...
}
public class PersonDTO {
private final String _vatCode;
private final String _personalDescription;
public PersonDTO(String _vatCode, String _personalDescription) {
this._vatCode = _vatCode;
this._personalDescription = _personalDescription;
}
// other fields here...
public String getVatCode()
{
return _vatCode;
}
public String getPersonalDescription()
{
return _personalDescription;
}
// some more getter here
}
public interface LocalizedPersonalInformationService {
PersonDTO getInformationOf(VatCode person, Locale localization) throws ProperExceptionList;
}
That is:
- 像一个
VatCode
valueobject(覆盖 equals、hashCode 和 toString)来标识Person
entity
- a
Person
实体,持有确保所需的最小数据量商业不变量并暴露一组命令和查询 http://epic.tesio.it/doc/manual/command_query_separation.html
- a
PersonDTO
带有有用的描述(有些人称之为读取模型 http://gorodinski.com/blog/2012/04/25/read-models-as-a-tactical-pattern-in-domain-driven-design-ddd/)
- a
LocalizedPersonalInformationService
能够提供PersonDTO
s.
- 和(显然)所有needed http://epic.tesio.it/2012/12/05/exceptions-are-the-norm.html 例外情况 http://epic.tesio.it/2013/03/04/exceptions-are-terms-ot-the-ubiquitous-language.html... :-)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)