在NHibernate的映射私有字段(与流利NH) [英] Mapping a Private Field in NHibernate (with Fluent NH)

查看:141
本文介绍了在NHibernate的映射私有字段(与流利NH)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,我怎么能映射(用流利的NHibernate的)这种模式:

I'd like to know, How Could I map (with fluent nhibernate) this model:

public class Category
{
   private IList<Product> _products;
   public IEnumerable<Product> Products {
       get { return _products; }
   }
   /* others properties */   


   public Category() {
       _products = new List<Product>();
   }


   // to add a product i'd use something like this:
   public void AddProducts(Product product) {
      product.Category = this;
      _products.Add(products);
   }
}

今天,我使用的IList的属性,但我不希望暴露,如添加,删除,等等......在我的财产的方法,所以我觉得揭露的一个简单属性IEnumerable和封装一个IList像一个私人领域!

Today, I'm using a property of IList, but I don't want to expose the methods like "Add", "Remove", etc... on my property, So I think to expose a simple property of IEnumerable and encapsulate an IList like a private field!

所以,它是一个很好的初步实践?我怎么能使用NHibernate映射呢?

So, Is it a good pratice ? How could I map it using NHibernate?

感谢

干杯

推荐答案

如果你遵循的命名约定NHibernate的可以工作,而你的样品code呢,这是​​非常容易的:

If you follow a naming convention that NHibernate can work with, and your sample code does, it's very easy:

HasMany(x => x.Products).KeyColumn("ProductId")
    .AsBag().Inverse()
    .Access.CamelCaseField(Prefix.Underscore);
    // plus cascade options if desired

我觉得这是不是一个很好的做法多了,我觉得它几乎总是正确的做法。

I think this is more than a good practice, I think it's almost always the right practice.

这篇关于在NHibernate的映射私有字段(与流利NH)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆