简体   繁体   English

在NHibernate中通过ID或父对象获取子对象?

[英]Get child objects by id or parent object in NHibernate?

Let's say I have a Broker class which has a collection of Accounts . 假设我有一个具有Accounts集合的Broker类。 The Broker class maps to a database table called brokers , the Account class maps to a database table called accounts . Broker类映射到称为brokers的数据库表, Account类映射到称为accounts的数据库表。 ie

public class Broker
{
    public virtual IEnumerable<Account> Accounts
    {
        get { return _accounts; }
        protected set { _accounts = new HashSet<Account>(value); }
    }
    // other properties
}

public class Account 
{                
    public virtual Broker Broker { get; set; }
    // other properties
}

I want to write a method to return a list of accounts for a Broker and I'm not sure what the signature should be. 我想编写一种方法来返回Broker的帐户列表,但不确定签名应该是什么。 I thought of two but not sure which to go for: 我想到了两个,但不确定该怎么做:

IList<Account> GetBrokerAccounts(Broker broker)

or 要么

IList<Account> GetBrokerAccounts(int brokerId)

I'd be interested from a design point of view the pros and cons of each and what would be "best practise". 从设计的角度,我会对每种方法的优缺点以及什么是“最佳实践”感兴趣。

Edit 编辑

Here is my mapping file for my collection problem: 这是我的收藏问题的映射文件:

  <class name="MooDB.Domain.Broker,MooDB" table="brokers" >
    <id name="Id" column="brokerId" type="Int32" unsaved-value="0">
      <generator class="increment" />
    </id>
    <version name="Version" column="version" type="integer" unsaved-value="0" />
    <property name="Name" column="`name`" type="String" length="50" not-null="true" />
    <property name="IsActive" column="isActive" type="bool" not-null="true" />
    <property name="IsDefault" column="isDefault" type="bool" not-null="true" />
    <set name="BrokerInstruments" table="brokerInstruments" generic="true" inverse="true" lazy="true" cascade="delete">
      <key column="brokerId" />
      <one-to-many class="MooDB.Domain.BrokerInstrument" />
    </set>
    <set name="Accounts" table="accounts" generic="true" inverse="true" lazy="false" cascade="delete">
      <key column="accountId" />
      <one-to-many class="MooDB.Domain.Account,MooDB"/>
    </set>
  </class>

Not sure why you would need any of the two methods when you can already access a list of accounts directly from the Broker object. 不确定已经可以直接从Broker对象访问帐户列表时,为什么需要两种方法中的任何一种。

var brokersAccounts = broker.Accounts;

NHibernate would already do the required work for you. NHibernate已经为您完成了所需的工作。 In other words first find the broker object/entity and enumerate through its accounts. 换句话说,首先找到经纪人对象/实体并通过其帐户进行枚举。

Use the id, brokerId. 使用id brokerId。 It's a better practice (I don't believe in best practices) to use the simplest possible arguments and return types. 使用尽可能简单的参数和返回类型是一种更好的做法(我不相信最佳做法)。 Passing the id only also is typically much more useful in a stateless application, such as a web app. 通常,在无状态应用程序(例如Web应用程序)中,仅传递ID也会更有用。

Use The Load method. 使用加载方法。 It creates proxy and thus you avoid loading of the Broker if you are only interested in the accounts. 它创建代理,因此,如果您仅对帐户感兴趣,则可以避免加载代理。

public IList<Account> GetBrokerAccounts(int brokerId)
{
    return session.QueryOver<Account>()
    .Where(x => x.Broker == session.Load(brokerId)).List();
}

So if you get the brokerId from UI there is no need to load the Broker object from the database since you know it exists in the database. 因此,如果您从UI获取brokerId,则无需从数据库加载Broker对象,因为您知道该对象存在于数据库中。 The code above will basically create select * from account where broker = brokerId type of query. 上面的代码基本上将创建来自broker = brokerId类型的查询的select * from帐户。 If you would use Get it would generate two queries. 如果使用Get,它将生成两个查询。 One for loading the Broker and second to get the accounts. 一个用于加载代理,第二个用于获取帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM