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

本文关键字:lt gt ISet 项目 添加 | 更新日期: 2023-09-27 18:20:22

我有三个类,分别称为BrokerInstrumentBrokerInstrument

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; }
}

如果我使用这些类创建了三个新对象(每种类型一个),我该如何将它们关联起来?例如:

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 
};

instrument如何"知道"新的brokerInstrument现在与其关联?如果我现在运行if (instrument.Brokerinstruments == null),我得到true。我是否必须将BrokerInstrument声明中的对象关联起来,然后返回并将其添加到instrument.BrokerInstruments ISet中?

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

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

您会得到一个异常,因为您没有初始化Instrument类的BrokerInstruments属性(意味着该属性的值为null)。要解决这个问题,您需要在Instrument:上使用构造函数

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

现在,如果您想要添加仪器的通知,那就另当别论了。最简单和最安全的方法是使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
}

我使用上面的IEnumerable是因为你想向这个函数的用户表明,他们不允许直接向集合中添加仪器——他们需要调用你的方法。

您从未初始化过仪器。经纪工具。您需要:instrument.BrokerInstruments = new ...;我猜是新的HashSet或新的SortedSet

要添加到集合中,您必须首先创建它的实例。这可以在相应的构造函数中完成。我使用HashSet<t>作为ISet<T>的具体实现的例子:

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>(); 
    }     
}

然后必须将brokerInstrument对象添加到InstrumentBroker:的BrokerInstruments集合中

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);