映射类中未映射的属性 (Fluent NHibernate 1.1)
背景
使用 Ninject 在 ASP.NET MVC 2 项目中实现流畅的 NHibernate 1.1(具有使用 LINQ to NHibernate 的存储库实现)。
我有一个映射类 (Station
) 和另一个映射类 (Report
) 的集合。我还希望 Station
有一个方便的属性 (MostRecentReport
),它返回该 Station
的 Report
,其中包含最近的时间戳。
部分架构
Stations
--------
Id
Reports
---------
Id
StationId
Timestamp
部分代码
public abstract class Entity
{
public virtual int Id { get; private set; }
}
public class Station : Entity
{
public virtual IList<Report> Reports { get; private set; }
public Station()
{
Reports = new List<Report>();
}
}
public class Report : Entity
{
public virtual Station Station { get; set; }
public virtual DateTime Timestamp { get; set; }
}
public StationMap : ClassMap<Station>
{
public StationMap()
{
Id(s => s.Id);
HasMany<Report>(s => s.Reports)
.Table("Reports")
.KeyColumn("StationId");
}
}
public ReportMap : ClassMap<Report>
{
public ReportMap()
{
Id(r => r.Id);
References<Station>(r => r.Station, "StationId");
Map(r => r.Timestamp);
}
}
我天真地尝试添加一个未映射的属性,该属性返回 Reports.OrderByDescending(r => r.Timestamp).FirstOrDefault()
但这会导致“无法解析属性:MostRecentReport”NHibernate.QueryException(即使我没有使用自动映射)。我还尝试将其实现为具有相同返回值的方法,但这会导致“索引超出范围”异常。
有没有办法让它与属性(首选)或方法方法一起工作?或者有什么方法可以用 Formula()
来映射它?
更新
Fluent NHibernate 配置(发生在我的 NinjectModule
实现的 Load
覆盖中):
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(Settings.ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Domain.Station>())
.BuildSessionFactory();
Background
Fluent NHibernate 1.1 (with a repository implementation using LINQ to NHibernate) in an ASP.NET MVC 2 project using Ninject.
I have a mapped class (Station
) with a collection of another mapped class (Report
). I would also like Station
to have a convenience property (MostRecentReport
) that returns the Report
for that Station
with the most recent timestamp.
Partial Schema
Stations
--------
Id
Reports
---------
Id
StationId
Timestamp
Partial Code
public abstract class Entity
{
public virtual int Id { get; private set; }
}
public class Station : Entity
{
public virtual IList<Report> Reports { get; private set; }
public Station()
{
Reports = new List<Report>();
}
}
public class Report : Entity
{
public virtual Station Station { get; set; }
public virtual DateTime Timestamp { get; set; }
}
public StationMap : ClassMap<Station>
{
public StationMap()
{
Id(s => s.Id);
HasMany<Report>(s => s.Reports)
.Table("Reports")
.KeyColumn("StationId");
}
}
public ReportMap : ClassMap<Report>
{
public ReportMap()
{
Id(r => r.Id);
References<Station>(r => r.Station, "StationId");
Map(r => r.Timestamp);
}
}
I naively tried adding an unmapped property that returned Reports.OrderByDescending(r => r.Timestamp).FirstOrDefault()
but that causes "could not resolve property: MostRecentReport" NHibernate.QueryException
s (even though I am not using auto mapping). I also tried implementing it as a method with identical return, but that causes "Index was out of range" exceptions.
Is there a way to make it work with either the property (preferable) or method approach? Or is there some way to map it with Formula()
, perhaps?
Update
Fluent NHibernate configuration (occurs in my NinjectModule
implementation's Load
override):
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(Settings.ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Domain.Station>())
.BuildSessionFactory();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有理由必须映射这样的属性——如果您不自动映射,NHibernate 应该忽略它。
我碰巧同意大卫的回答,你可能会破坏领域模型的抽象。
也就是说,我认为答案是您在某处(存储库?)有一个 NHibernate 查询(HQL/Criteria/LINQ)正在尝试访问此属性。也就是说,问题不在映射中,而是在查询数据库/缓存时尝试使用
MostRecentReport
。There is no reason that you would have to map a property like this -- NHibernate should just ignore it if you are not automapping.
I happen to agree with David's answer, that you might be breaking the abstraction of your domain model.
That said, I think the answer is that you have an NHibernate query (HQL/Criteria/LINQ) somewhere (a repository?) that is trying to access this property. That is, it isn't in the mappings that is the problem, it is in trying to use
MostRecentReport
when querying the database/cache.我不确定将未映射的属性添加到您的实体是个好主意。将便利属性添加到 ViewModel 并使用存储库查询来获取值是一种更好的关注点分离。
I'm not sure that adding unmapped properties to your Entities is such a good idea. It's a better separation of concerns to add convenience properties to a ViewModel and use a repository query to get the value.