我有三个叫Broker,Instrument和BrokerInstrument.
using Iesi.Collections.Generic;
public class Broker : ActiveDefaultEntity
{
public virtual string Name { get; set; }
public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; }
}
public class Instrument : Entity
{
public virtual string Name { get; set; }
public virtual string Symbol {get; set;}
public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; }
public virtual bool IsActive { get; set; }
}
public class BrokerInstrument : Entity
{
public virtual Broker Broker { get; set; }
public virtual Instrument Instrument { get; set; }
public virtual decimal MinIncrement { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这些类创建三个新对象(每种类型之一)如何关联它们?例如:
Instrument instrument = new Instrument {
Name = "Test Instrument",
Symbol = "Test",
IsActive = true
};
Broker broker = new Broker {
Name = "My Test Broker",
IsActive = true,
IsDefault = false
};
BrokerInstrument brokerInstrument = new BrokerInstrument {
Broker = broker,
Instrument = instrument,
MinIncrement = 0.01M
};
Run Code Online (Sandbox Code Playgroud)
如何instrument"知道"新brokerInstrument的现在与它相关联?如果我现在跑,if (instrument.Brokerinstruments == null)我得到true.我是否必须在BrokerInstrument声明中关联对象,然后返回并将其添加到instrument.BrokerInstruments ISet?
如果我尝试做:instrument.BrokerInstruments.Add(instrument)我得到一个错误,因为它为null.困惑.我错过了什么?建模这样的关系的最佳方法是什么?这些对象将使用NHibernate持久保存到数据库.
您得到一个异常,因为您没有初始化Instrument类的BrokerInstruments属性(意味着该属性的值为null).要解决这个问题,您需要在Instrument上使用构造函数:
public Instrument() {
BrokerInstruments = new HashSet<BrokerInstrument>();
}
Run Code Online (Sandbox Code Playgroud)
现在,如果您想要添加仪器的通知,那就是另一个问题.最简单和最安全的方法是使BrokerInstruments属性getter返回IEnumerable,删除setter,并添加一个AddBrokerInstrument方法:
// With this, you don't need the constructor above.
private ISet<BrokerInstrument> _brokerInstruments = new HashSet<BrokerInstrument>();
public virtual IEnumerable<BrokerInstrument> BrokerInstruments {
get { return _brokerInstruments; }
// This setter should allow NHibernate to set the property while still hiding it from external callers
protected set { _brokerInstruments = new HashSet<BrokerInstrument>(value); }
}
public void AddBrokerInstrument(BrokerInstrument brokerInstrument) {
// Any other logic that needs to happen before an instrument is added
_brokerInstruments.Add(brokerInstrument);
// Any other logic that needs to happen after an instrument is added
}
Run Code Online (Sandbox Code Playgroud)
我使用上面的IEnumerable是因为你想向这个函数的用户表明他们不允许直接将乐器添加到集合中 - 他们需要调用你的方法.