C# 如何将项目添加到ISet<;T>;?

C# 如何将项目添加到ISet<;T>;?,c#,collections,C#,Collections,我有三个类,分别是Broker、Instrument和BrokerInstrument using Iesi.Collections.Generic; public class Broker : ActiveDefaultEntity { public virtual string Name { get; set; } public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; } } p

我有三个类,分别是
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; }
}
仪器
如何“知道”一个新的
代理仪器
现在与之关联?如果我现在运行
If(instrument.Brokerinstruments==null)
我得到
true
。我是否必须将
BrokerInstruments
声明中的对象关联起来,然后返回并将其添加到
instrument.BrokerInstruments ISet


如果我尝试这样做:
instrument.BrokerInstruments.Add(instrument)
我会得到一个错误,因为它为null。困惑的我错过了什么?建立这种关系模型的最佳方法是什么?这些对象将使用NHibernate持久化到数据库。

要能够添加到集合中,您必须首先实际创建它的实例。这可以在各自的构造函数中完成。我的示例使用
HashSet
作为
ISet
的具体实现:


您从未初始化过instrument.BrokerInstruments。 您需要:
instrument.BrokerInstruments=new

我猜new HashSet或new SortedSet会出现异常,因为您没有初始化Instrument类的BrokerInstruments属性(表示该属性的值为null)。要解决此问题,您需要在仪器上安装构造函数:

public Instrument() {
    BrokerInstruments = new HashSet<BrokerInstrument>();
}

我使用上面的IEnumerable是因为您想向此函数的用户指出,不允许他们直接向集合中添加工具-他们需要调用您的方法。

谢谢,由于名称空间冲突和强制转换错误,我不得不这样做:
BrokerInstruments=(ISet)new System.Collections.Generic.HashSet();看起来很有希望,但是当我运行单元测试时,NHibernate抱怨TestFixture失败:NHibernate.PropertyNotFoundException:在类“MooDB.Domain.Instrument”中找不到属性“BrokerInstruments”的设置程序。
我现在需要在NHibernate中执行一些特殊操作吗?这看起来很复杂,但我只想在域模型中建立一个一对多的关系模型,然后让NHibernate将更改持久化到db中。啊,我错过了NHibernate。我将相应地修改。。。差不多了!在
protectedset
行上,我遇到了一个错误:
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.ISet”。存在显式转换(您是否缺少演员阵容?
感谢您的帮助,现在似乎可以工作了。我仍然想知道为什么我必须添加两次BrokersTrument。1) 在my
new BrokerInstance
声明中,以及2)使用
Instrument.Add()
?你可能知道我是新来的。:)因为ISet接口(及其任何实现者)公开了在添加或删除项时通知您的事件,所以您需要自己控制添加。
public class Broker : ActiveDefaultEntity
{
    public virtual string Name { get; set; }
    public virtual ISet<BrokerInstrument> BrokerInstruments { get; set; }

    public Broker() {
        BrokerInstruments = new HashSet<BrokerInstrument>();
    }
}

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 Instrument() {
        BrokerInstruments = new HashSet<BrokerInstrument>(); 
    }     
}
var instrument = new Instrument { 
    Name = "Test Instrument", 
    Symbol = "Test", 
    IsActive = true 
};
var broker = new Broker { 
    Name = "My Test Broker", 
    IsActive = true, 
    IsDefault = false 
};
var brokerInstrument = new BrokerInstrument { 
    Broker = broker, 
    Instrument = instrument, 
    MinIncrement = 0.01M 
};
instrument.BrokerInstruments.Add(brokerInstrument);
broker.BrokerInstruments.Add(brokerInstrument);
public Instrument() {
    BrokerInstruments = new HashSet<BrokerInstrument>();
}
// 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
}