我有以下旧代码。持久模型和(贫血的)域模型之间存在分离。这种分离和隐式转换有什么好处?你看到什么缺点了吗?
我知道使用隐式转换是可能的:SplitAmountEF saEF = dbContext.SplitAmount.Find(id); 拆分金额 sa = saEF; //隐式转换。它们可以互换使用。
如果领域模型和持久性模型几乎一样,那么只使用持久性模型(根本不使用域模型)会更好吗?
例子:
//Domain Model
public class Booking
{
public Guid ID {get; set;}
....
}
public class SplitAmount
{
public Guid ID {get; set;}
public Decimal Amount {get; set;}
...
public Guid IDBooking {get; set;}
}
//Persistence Model
public class BookingEF
{
public Guid ID {get; set;}
...
}
public class SplitAmountEF
{
public Guid ID {get; set;}
public Decimal Amount {get; set;}
...
public virtual BookingEF Parent {get; set;}
//implicit converstion from SplitAmountEF to SplitAmount
public static implicit SplitAmount(SplitAmountEF saEF)
{
return new SplitAmount()
{
ID = saEF.ID,
Amount = saEF.Amount,
...
IDBooking = saEF.Parent.ID,
}
}
//implicit converstion from SplitAmount to SplitAmountEF
public static implicit SplitAmountEF(SplitAmount sa)
{
return new SplitAmountEF()
{
ID = sa.ID,
Amount = sa.Amount,
...
}
}
}