没有空构造函数和设置器的NHibernate映射
我正在测试将NHibernate与一个完全不知道它的域一起使用有多困难,并且不打算考虑任何限制 在我在网上发现的许多例子中,被映射的域是另一个贫乏域的例子,在这个例子中,对象离简单的数据持有者不远。当然,这使得映射变得简单,并且可能会吸引以数据为中心的人/情况,但我不喜欢在脑海中听到这样的声音:“C也有结构,你知道吗?”,“类不仅仅是花哨的名称空间,你知道吗?”,或者“为什么不改为使用CREATE TABLE?” 但是回到尼伯内特。NHibernate强制我将属性设置为虚拟,以便能够代理它们进行延迟加载。这是我不介意的,因为我可能也需要它们作为一些AOP的虚拟工具。 我不满意的限制是需要空构造函数和setter/properties。我希望在有效状态下创建实体,大多数情况下这意味着没有空构造函数。 出于通常的原因,我也不想公开集合属性的setter。哦,还有不应该直接更改的属性的setter 考虑一下某个领域模型中的简化和人为聚合:没有空构造函数和设置器的NHibernate映射,nhibernate,orm,nhibernate-mapping,mapping,Nhibernate,Orm,Nhibernate Mapping,Mapping,我正在测试将NHibernate与一个完全不知道它的域一起使用有多困难,并且不打算考虑任何限制 在我在网上发现的许多例子中,被映射的域是另一个贫乏域的例子,在这个例子中,对象离简单的数据持有者不远。当然,这使得映射变得简单,并且可能会吸引以数据为中心的人/情况,但我不喜欢在脑海中听到这样的声音:“C也有结构,你知道吗?”,“类不仅仅是花哨的名称空间,你知道吗?”,或者“为什么不改为使用CREATE TABLE?” 但是回到尼伯内特。NHibernate强制我将属性设置为虚拟,以便能够代理它们进行
public class ShoppingCartItem
{
private readonly Product product;
public ShoppingCartItem(Product product, int quantity)
{
if(quantity <= 0)
throw new ArgumentOutOfRangeException("quantity");
this.product = product;
this.quantity = quantity;
}
public virtual Product Product
{
get { return product; }
}
private int quantity;
public virtual int Quantity
{
get { return quantity; }
set
{
if(value <= 0)
throw new ArgumentOutOfRangeException("value");
quantity = value;
}
public virtual Money TotalPrice
{
get { return product.Price * quantity; }
}
}
public class ShoppingCart : Entity
{
private readonly IDictionary<Product, ShoppingCartItem> items =
new Dictionary<Product, ShoppingCartItem>();
private readonly ISet<DiscountCoupon> discountCoupons =
new HashSet<DiscountCoupon>();
public virtual IEnumerable<ShoppingCartItem> Items
{
get { return items.Values; }
}
public virtual IEnumerable<DiscountCoupon> DiscountCoupons
{
get { return discountCoupons; }
}
public virtual void AddProduct(Product product)
{
ShoppingCartItem item;
if(items.TryGetValue(product, out item))
item.Quantity++;
else
items.Add(product, new ShoppingCartItem(product, 1));
}
public virtual void RemoveProduct(Product product)
{
ShoppingCartItem item;
if(!items.TryGetValue(product, out item))
throw new ArgumentException("product");
if(item.Quantity == 1)
items.Remove(product);
else
item.Quantity--;
}
public virtual int AddDiscountCoupon(DiscountCoupon coupon)
{
discountCoupons.Add(coupon);
}
public virtual int RemoveDiscountCoupon(DiscountCoupon coupon)
{
discountCoupons.Remove(coupon);
}
public virtual Money CalculatePrice()
{
// Missing complex discount logic
return items.Values.Sum(item => item.TotalPrice);
}
}
公共类ShoppingCartItem
{
私人只读产品;
公共购物车项目(产品,整数数量)
{
如果(数量很好,您可以在属性/集合上放置私有/内部/受保护的设置器,nhibernate将正确加载它们(第4.1.1章)
构造器必须在那里,但您没有义务将其公开(第4.1.2章)
最新版本的第ref章首先,您可以将空构造函数设为私有,这样其他人就无法访问它。NH仍然可以访问它
第二,NH可以根据您的需要访问您的酒店
access=“backfield”用于公共虚拟产品产品{get;private set;}
m_产品的access=“field.pascalcase-m-下划线”
access=“field.pascalcase下划线”用于_产品
还有其他访问策略,我敢肯定,如果需要,您甚至可以创建自己的访问策略。事实上,访问策略并不总是需要明确定义。这并不完全正确。应保护构造函数。避免出现异常:“NHibernate.InvalidProxyTypeException:以下类型不能用作代理:ClassA:类型应具有可见(公共或受保护)无参数构造函数”。文档(4.1.2)说明:“类必须具有默认构造函数(可能是非公共的)”