The*_*Sky 5 .net c# nhibernate fluent-nhibernate
我有一个简单的架构,如下所示:
这个想法是有用户,部门和流程.对于每个部门,用户可以"加注"多个流程以"标记"它们(因此部门/用户对可以有许多流程).
理想情况下,我想使用以下类:
public class User
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
}
public class Department
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IDictionary<User, IList<StarredFlow>> StarredFlows { get; protected set; }
public Department()
{
this.UserStarredFlows = new Dictionary<User, IList<StarredFlow>>();
}
}
public class Flow
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
protected Flow() { }
}
public class StarredFlow
{
public virtual int Id { get; protected set; }
public virtual User User { get; protected set; }
public virtual Department EntryPoint { get; protected set; }
public virtual Flow Flow { get; protected set; }
public virtual string Name { get; protected set; }
protected StarredFlow() { }
public StarredFlow(User user, Department ep, Flow flow, string name)
{
this.User = user;
this.EntryPoint = ep;
this.Flow = flow;
this.Name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许非常文字的代码:
var userStarredFlowsForDepartment = department.StarredFlows[currentUser];
Run Code Online (Sandbox Code Playgroud)
但是,NHibernate不支持映射列表的字典.我发现了一个类似的问题/答案,涉及将IList<StarredFlow>一个新的类移动到一个新类并使用一个组件进行映射.遗憾的是,FluentNHibernate不支持使用组件进行集合映射(HasMany组件映射没有关闭).
对于NHibernate来说,这似乎是一件微不足道的事情,但我很难找到解决方案.这样做有更好的解决方案吗?我真的希望不会有决心只是用session.Save(starredFlow)和Query<StarredFlow>(),因为他们移动的逻辑模型之外.
你想做的事情肯定是不支持的。我并不是说这在理论上不是一个有效的设计,但 NHibernate 不支持它,而且将来也不太可能这样做。
也就是说,你总是可以用一个方法来Department代替字典,采用IQueryable:
public IEnumerable<StarredFlow> GetUserStarredFlows(IQueryable<StarredFlow> source,
User user)
{
return source.Where(x => x.User == user)
.ToList(); //you could skip this depending on your usage
}
Run Code Online (Sandbox Code Playgroud)
或者(更复杂一点)您可以注入IQueryable或会话。
| 归档时间: |
|
| 查看次数: |
283 次 |
| 最近记录: |