如何映射我的类以使用自定义方法获取数据库记录?
我正在使用 ASP.NET MVC、NHibernate 和 Fluent NHibernate 开发一个网站,并且想知道如何自定义我的 Map 以使用自定义方法获取记录。
我的实体:
public class ImageGallery {
public virtual int Id { get; set; }
public virtual string Titulo { get; set; }
public virtual IList<Image> Images { get; set; }
public virtual bool IsActive { get; set; }
}
public class Image {
public virtual int Id { get; set; }
public virtual ImageGallery ImageGallery { get; set; }
public virtual string Low { get; set; }
public virtual bool IsActive { get; set; }
}
我的 Fluent NHibernate 地图:
public class ImageGalleryMap:ClassMap<ImageGallery> {
public ImageGalleryMap() {
Id(x => x.Id);
Map(x => x.Titulo);
HasMany(x => x.Images)
.KeyColumn("ImageGalleryID");
Map(x => x.IsActive);
}
}
public class ImageMap:ClassMap<Image> {
public ImageMap() {
Id(x => x.Id);
References(x => x.ImageGallery);
Map(x => x.Low);
Map(x => x.IsActive);
}
}
并且,我的 ImageRepository 类上的方法:
public IList<Image> ListActive() {
return this.Session.CreateCriteria<Image>()
.Add(Restrictions.Eq("IsActive", true))
.List<Image>();
}
如果我以这种方式创建一个对象:
ImageGallery ig = new ImageGallery();
我可以使用以下方式访问我的图像列表:
foreach(Image img in ig.Images) {
...
}
但是,ig.Images 使我可以访问所有图像记录,我希望使用 ListActive() 的存储库方法仅访问活动记录(或其他条件)。
如何在我的地图上指定这一点?
非常感谢!
I'm developing a website with ASP.NET MVC, NHibernate and Fluent NHibernate and want to know how can I customize my Map to get records using a custom Method.
My entities:
public class ImageGallery {
public virtual int Id { get; set; }
public virtual string Titulo { get; set; }
public virtual IList<Image> Images { get; set; }
public virtual bool IsActive { get; set; }
}
public class Image {
public virtual int Id { get; set; }
public virtual ImageGallery ImageGallery { get; set; }
public virtual string Low { get; set; }
public virtual bool IsActive { get; set; }
}
My Fluent NHibernate Maps:
public class ImageGalleryMap:ClassMap<ImageGallery> {
public ImageGalleryMap() {
Id(x => x.Id);
Map(x => x.Titulo);
HasMany(x => x.Images)
.KeyColumn("ImageGalleryID");
Map(x => x.IsActive);
}
}
public class ImageMap:ClassMap<Image> {
public ImageMap() {
Id(x => x.Id);
References(x => x.ImageGallery);
Map(x => x.Low);
Map(x => x.IsActive);
}
}
And, a method on my ImageRepository class:
public IList<Image> ListActive() {
return this.Session.CreateCriteria<Image>()
.Add(Restrictions.Eq("IsActive", true))
.List<Image>();
}
If I create an object this way:
ImageGallery ig = new ImageGallery();
I can access my the Image's list using:
foreach(Image img in ig.Images) {
...
}
However, ig.Images give me access to all images records and I would like to access just active records (or another criteria), using ListActive()'s repository methods.
How can I specify this on my Map?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
映射中的Where约束将始终将图像限制为活动图像:
如果您想加载图像集合但动态过滤它以查找活动记录,最好的方法是创建一个扩展方法:
然后您可以像这样访问活动记录:
我喜欢扩展方法解决方案,因为它可以用于任何
IEnumerable
,而不仅仅是 ImageGallery 上的集合。我肯定会使用这个解决方案,除非加载非活动图像是一个可测量的性能问题。如果您只想有选择地加载活动图像,请先阅读阅读此内容关于 NHibernate Filters 的文章,因为我没有任何使用它们的经验。
A Where constraint in the mapping will limit the Images to active ones always:
If you want to load the Images collection but dynamically filter it for active records, the best way is to create an extension method:
Then you can access the active records like this:
I like the extension method solution because it can be used on any
IEnumerable<Image>
, not just the collection on ImageGallery. I would definitely use this solution unless loading inactive images was a measurable performance problem.If you want to selectively load active Images only then start by reading this article about NHibernate Filters because I don't have any experience with them.