EF Code FirstまたはNHibernateで使用され、リポジトリレイヤーから返される次の2つの単純な永続化クラスを使用してブログアプリケーションを作成するとします。
public class PostPersistence
{
public int Id { get; set; }
public string Text { get; set; }
public IList<LikePersistence> Likes { get; set; }
}
public class LikePersistence
{
public int Id { get; set; }
//... some other properties
}
永続性モデルをドメインモデルにマッピングするためのクリーンな方法がわかりません。ドメインモデルのインターフェースを次のようにしたいと思いPost
ます。
public interface IPost
{
int Id { get; }
string Text { get; set; }
public IEnumerable<ILike> Likes { get; }
void Like();
}
さて、その下の実装はどのように見えるでしょうか?多分このようなもの:
public class Post : IPost
{
private readonly PostPersistence _postPersistence;
private readonly INotificationService _notificationService;
public int Id
{
get { return _postPersistence.Id }
}
public string Text
{
get { return _postPersistence.Text; }
set { _postPersistence.Text = value; }
}
public IEnumerable<ILike> Likes
{
//this seems really out of place
return _postPersistence.Likes.Select(likePersistence => new Like(likePersistence ));
}
public Post(PostPersistence postPersistence, INotificationService notificationService)
{
_postPersistence = postPersistence;
_notificationService = notificationService;
}
public void Like()
{
_postPersistence.Likes.Add(new LikePersistence());
_notificationService.NotifyPostLiked(Id);
}
}
私はDDDについて読むことに時間を費やしましたが、ほとんどの例は理論的であるか、ドメイン層で同じORMクラスを使用していました。実際、ドメインモデルはORMクラスの単なるラッパーであり、ドメイン中心のアプローチではないように思われるため、私のソリューションは本当に醜いようです。また、IEnumerable<ILike> Likes
LINQ to SQLのメリットがないため、実装方法が気になります。より透過的な永続性の実装でドメインオブジェクトを作成するための他の(具体的な!)オプションは何ですか?