在 NHibernate 中映射私有字段(使用 Fluent NH)
我想知道,我如何映射(使用流畅的 nhibernate)这个模型:
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!
那么,这是一个好的做法吗?我如何使用 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);
}
}
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!
So, Is it a good pratice ? How could I map it using NHibernate?
Thanks
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您遵循 NHibernate 可以使用的命名约定,并且您的示例代码也这样做,那么这非常简单:
我认为这不仅仅是一个好的实践,我认为这几乎总是正确的实践。
If you follow a naming convention that NHibernate can work with, and your sample code does, it's very easy:
I think this is more than a good practice, I think it's almost always the right practice.
您需要阅读此 Fluent-nhibernate wiki 页面:
https:/ /github.com/jagregory/ Fluent-nhibernate/wiki/Mapping-private-properties
You'll want to read this fluent-nhibernate wiki page:
https://github.com/jagregory/fluent-nhibernate/wiki/Mapping-private-properties