具有实体框架和空间数据的持久无知域

Luc*_* S. 8 c# domain-driven-design entity-framework entity-framework-6

我正在开发一个实现DDD和Repository Pattern的应用程序,如下图所示:

我的软件架构

我希望保持我的Domain Layer持久无知,所以我不想在那里安装实体框架库.我面临的唯一问题是我的应用程序使用空间数据,但是一旦它属于System.Data.Entity.Spatial命名空间,我就不应该使用DbGeography作为我的实体的属性类型,来自EntityFramework程序集.

有没有办法创建一个类来保存域层中的纬度,经度和高程值,如下所示:

public class Location
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double Elevation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的存储库层中将该类转换为DbGeography?

换句话说,域实体只有Location类作为属性:

public class Place : IEntityBase, ILocalizable
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Location Location { get; set; }
    public User Owner { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我将其转换为DbGegraphy以保留空间数据并仅在存储库层中进行一些计算.我的计划是尝试这样的转换:

public class LocationMap : ComplexTypeConfiguration<Location>
{
    public LocationMap()
    {
        Property(l => DbGeography.FromText(string.Format("POINT({0} {1})", l.Longitude, l.Latitude))).HasColumnName("Location");
        Ignore(l => l.Elevation);
        Ignore(l => l.Latitude);
        Ignore(l => l.Longitude);
    }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,永远不会.我怎么能解决这个问题?在这种情况下,最佳做法是什么?

谢谢

Bac*_*cks 0

好吧,我不知道“正确”的方式,但是,我有一个棘手的想法。我希望,它会对您有所帮助或提供更多变体:Ypu 有域实体Place,它完全持久无知,并且位于域程序集中。好的。让我们在 Repository 程序集中再创建一个 Place 类:

internal sealed class EFPlace : Place
{
    DbGeography EFLocation 
    {
        get
        {
            return DbGeography.FromText(string.Format("POINT({0} {1})", Location.Longitude, Location.Latitude);
        }
        set
        {
            //vice versa convertion, I don't know, how to do it :)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我们为实体框架创建了特殊的类,并映射它:

public class PlaceMap : ComplexTypeConfiguration<EFPlace>
{
    public PlaceMap ()
    {
        Property(p => p.EFLocation).HasColumnName("Location");
        Ignore(p => p.Location);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我们必须在存储库中保存时从 Place 转换为 EFPlace。您可以创建特殊的构造函数或转换方法。另一种变体 - 创建分部类放置在域和存储库程序集中。并在存储库一类中添加所需的属性,依此类推。嗯,它看起来很丑:(但是,我不知道持久无知域的“纯粹”、现实生活中的例子。我们总是有实体框架的局限性。NHibernate更多的功能。