2012-01-16 7 views
5

मेरे पास Broker, Instrument और BrokerInstrument नामक तीन वर्ग हैं।किसी आइटम को किसी ISET <T> में कैसे जोड़ें?

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) मुझे कोई त्रुटि मिलती है क्योंकि इसकी शून्य है। उलझन में। मैं क्या खो रहा हूँ? इस तरह के रिश्तों को मॉडल करने का सबसे अच्छा तरीका क्या है? इन वस्तुओं को NHibernate का उपयोग कर डेटाबेस में जारी रखा जाएगा।

उत्तर

5

लगता है क्योंकि आप साधन वर्ग के BrokerInstruments संपत्ति आरंभ नहीं कर रहे हैं आप एक अपवाद मिल (जिसका अर्थ है कि संपत्ति का मूल्य शून्य है)। कि सुलझाने के लिए आपको साधन पर एक निर्माता की जरूरत है:

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

अब अगर आप चाहते हैं एक साधन की अधिसूचना जोड़ा जा रहा है, कि एक अलग समस्या है। सबसे आसान और सबसे सुरक्षित तरीका, BrokerInstruments संपत्ति गेटर एक IEnumerable वापसी कर सेटर को निकालें और 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 उपयोग करें, क्योंकि आप इस समारोह के उपयोगकर्ताओं के लिए इंगित कर सकते हैं कि उन्हें सेट पर सीधे उपकरण जोड़ने की अनुमति नहीं है- उन्हें इसके बजाय अपनी विधि कॉल करने की आवश्यकता है।

+0

वादा करता है हालांकि जब मैं यूनिट परीक्षण चलाता हूं तो एनएचबीर्नेट की शिकायत 'टेस्टफिक्स्चर विफल' के साथ शिकायत करती है: एनएचबीर्नेट.प्रोपर्टी नॉटफाउंड अपवाद: कक्षा 'MooDB.Domain.Instrument' में संपत्ति 'ब्रोकर इंस्ट्रूमेंट्स' के लिए एक सेटटर नहीं मिला, क्या मुझे एनएचबेर्नेट में कुछ विशेष करने की ज़रूरत है अभी व? यह काफी जटिल लगता है, लेकिन मैं बस इतना करना चाहता हूं कि एक मॉडल मॉडल में एक-दूसरे के रिश्ते मॉडल हों और उसके बाद NHBernate डीबी में परिवर्तन जारी रहे। –

+0

आह, मैंने NHibernate बिट को याद किया। मैं तदनुसार संशोधित करूँगा ... हो गया। –

+0

लगभग! 'संरक्षित सेट' लाइन पर मुझे त्रुटि मिल रही है: 'पूरी तरह से प्रकार को परिवर्तित नहीं कर सकता' System.Collections.Generic.IEnumerable 'to' System.Collections।Generic.ISet '। एक स्पष्ट रूपांतरण मौजूद है (क्या आप एक कास्ट गायब हैं?) ' –

1

संग्रह में जोड़ने में सक्षम होने के लिए आपको वास्तव में पहले इसका उदाहरण बनाना होगा। यह संबंधित रचनाकारों में किया जा सकता है। ISet<T> के ठोस कार्यान्वयन के रूप में HashSet<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>(); 
    }  
} 

तो फिर तुम Instrument और Broker के दोनों BrokerInstruments -sets को 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); 
+0

धन्यवाद, मुझे नामस्थान क्लैश और कास्टिंग त्रुटि के कारण ऐसा करना था: 'ब्रोकर इंस्ट्रूमेंट्स = (आईसेट <ब्रोकर इंस्ट्रूमेंट>) नई प्रणाली। चयन। जेनेरिक। हैशसेट <ब्रोकर इंस्ट्रूमेंट>(); ' –

+0

ग्रेट! अगर यह मददगार था तो कृपया उत्तर स्वीकार करें। –

0

आप instrument.BrokerInstruments कभी नहीं प्रारंभ। आप की जरूरत है: instrument.BrokerInstruments = new ...; मैं नए HashSet या नए SortedSet

संबंधित मुद्दे