NHibernate 和抽象基类中的通用列表
这是一个复杂的问题,我尝试了很多角度,我不记得我已经尝试过什么以及还没有尝试过。但基本上,我的项目包括足球联赛的一些基础课程。我有以下课程:
League - 我的图书馆中联赛的抽象基类 Team - 我的库中球队的抽象基类
FootballLeague - 源自 League(可以是任何其他类型的联赛,例如 BaseballLeague、SoccerLeague 等) FootballTeam - 派生自 Team (与上面相同)
基本上我想做的是在 League 类中拥有一个球队列表,这样我就可以在该类中拥有更通用的方法,这些方法只能对 Team 对象进行操作。然后在我的 FootballLeague 中能够引用相同的列表,除了取回 FootballTeam 对象(列表真正包含的)。
到目前为止,这是我在非常基本的层面上的实现:
public abstract class League
{
protected IList mTeams;
}
public abstract class Team
{
public virtual League League
{
get;
protected set;
}
}
public class FootballLeague : League
{
public virtual IList<FootballTeam> Teams
{
get { return this.mTeams.Cast<FootballTeam>().ToList(); }
protected set { this.mTeams = value.ToList(); }
}
public FootballLeague() : base()
{
this.mTeams = new List<FootballTeam>();
this.mConferences = new List<FootballConference>();
}
}
除了 NHibernate 问题之外,这几乎完全按照我想要的方式工作。
1)当我加载 FootballLeague 对象时,它也会加载所有 FootballTeam 对象。如果我删除基类中的 mTeams 声明,并仅对 FootballLeague 对象中的 IList 使用自动获取/设置,则延迟加载有效,并且我的球队在实际访问该集合之前不会加载。因此,基本上在基类中拥有列表访问的成员变量会导致 Niberate 绕过延迟加载。
这种实现方式是您处理此类库的方式吗?尽管您有应该具有列表功能的抽象基类,但派生类也应该能够访问列表的更专门的版本?如果没有,我应该如何实现它才能与 NHibernate 一起使用?
This is kind of a complicated question and I've tried so many angle I can't remember what I have and haven't tried yet. But basically, my project consist of some base classes for a football league. I have the following classes:
League - abstract base class for a league in my library
Team - abstract base class for a team in my library
FootballLeague - derived from League (could be any other type of league though e.g. BaseballLeague, SoccerLeague, etc.)
FootballTeam - derived from Team (same as above)
Basically what I'd like to do is have a list of teams in the League class so I can have more general methods in that class that can operate on just Team objects. And then in my FootballLeague be able to reference that same list except get back the FootballTeam objects (which the list really contains).
Here is my implementation so far at a very basic level:
public abstract class League
{
protected IList mTeams;
}
public abstract class Team
{
public virtual League League
{
get;
protected set;
}
}
public class FootballLeague : League
{
public virtual IList<FootballTeam> Teams
{
get { return this.mTeams.Cast<FootballTeam>().ToList(); }
protected set { this.mTeams = value.ToList(); }
}
public FootballLeague() : base()
{
this.mTeams = new List<FootballTeam>();
this.mConferences = new List<FootballConference>();
}
}
This works pretty much exactly how I want except for an NHibernate problem.
1) When I load a FootballLeague object, it loads all the FootballTeam objects as well. If I remove the mTeams declaration in the base class and just use an auto get/set for the IList in the FootballLeague object lazy loading works and my teams don't load until actually access the collection. So basically having that member variable that the list accesses in the base class, causes NHiberate to bypass lazy loading.
Is this implementation the way you would handle this type of library. Whereas you have abstract base classes that should have functionality on lists, but the derived classes should also be able to access more specialized versions of the list as well? If not, how should I implement that so it works with NHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并将 mTeams 映射为 IList 和 hasmanyToAny 以避免转换错误
这对您有用吗?
and map mTeams as IList and hasmanyToAny to avoid casting errors
Does this work for you?