应该是DDD中的域的本地化部分 [英] Should be localization part of domain in DDD
问题描述
示例1
class Persion {
String name;
//其他字段ommited
void rename(String newName){
String oldName = this.name;
this.name = newName
//发布具有旧名称和新名称的事件
}
字符串名称(){
返回名称;
}
}
class PersionRepository {
void store(Persion persion){
Locale loc = LocaleContextHolder.get()。getLocale ();
//存储对象到DAO - 创建/更新字段为上下文locale
}
//其他方法ommited
}
示例2
< blockquote>
class Persion {
Map< Locale,String>名称;
//其他字段ommited
void rename(String newName){
Locale locale = LocaleContextHolder.get()。getLocale();
String oldName = this.name.put(locale,newName);
//发布具有旧名称和新名称的事件
}
字符串名称(){
Locale locale = LocaleContextHolder.get()。getLocale ();
返回this.name.get(locale);
}
}
class PersionRepository {
void store(Persion persion){
//将对象存储到DAO - 创建/更新所有地区的字段
}
//其他方法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){
// ...
}
@覆盖
public int hashCode(){
// ...
}
}
public class Person {
private final VatCode _identifier ;
public Person(VatCode identifier)
{
_identifier = identifier;
}
//某些命令和一些查询在这里...
}
public class PersonDTO {
private final String _vatCode;
private final String _personalDescription;
public PersonDTO(String _vatCode,String _personalDescription){
this._vatCode = _vatCode;
this._personalDescription = _personalDescription;
}
//其他字段在这里...
public String getVatCode()
{
return _vatCode;
}
public String getPersonalDescription()
{
return _personalDescription;
}
//一些更多的getter这里
}
public interface LocalizedPersonalInformationService {
PersonDTO getInformationOf(VatCode person,Locale localization)throws ProperExceptionList;
}
那是:
- 像一个
VatCode
valueobject(覆盖equals,hashCode和toString),以标识Person
实体
- a
个人
实体,持有确保业务不变量所需的最低数据量并公开一组命令和查询
- a
PersonDTO
,其中包含有用的说明(有些称为 read-model )
- a
LocalizedPersonalInformationService
,可以提供PersonDTO
s。
- (显然)所有的需要 例外 ...: - )
While following DDD concept I'm struggling on decision if I should make my domain localization aware? I came with two two solutions how to solve this. Both makes domain localization aware in different places. Should I even place localized text to domain? Share your solution for this problem or pros and cons of my two examples. Thanks.
Example 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 }
Example 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 }
解决方案In most of cases, the best option is to remove localization from the domain.
Domain classes should only contain data that are relevant to their invariants, since they are responsible for business rules. To retrieve localized descriptions, use projective DTOs and applicative services.
You could use something like this:
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:
- something like a
VatCode
valueobject (that overrides equals, hashCode and toString) to identify thePerson
entity- a
Person
entity, holding the minimum amount of data required to ensure business invariants and exposing a set of command and queries- a
PersonDTO
that carries useful descriptions (some call this a read-model)- a
LocalizedPersonalInformationService
that is able to providePersonDTO
s.- and (obviously) all the needed exceptions... :-)
这篇关于应该是DDD中的域的本地化部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!