मुझे लगता है कि मुझे एनएचबीरनेट के बारे में कुछ अवधारणा याद आ रही है। मेरे पास Instrument
ऑब्जेक्ट है जो मेरे डेटाबेस में instruments
तालिका में मानचित्र करता है। मेरे पास BrokerInstrument
ऑब्जेक्ट भी है जो मेरे डेटाबेस में brokerInstruments
तालिका में मैप करता है। brokerInstrumnets
instruments
की एक बाल तालिका है। मेरे वर्गों की तरह लग रहे:,एनएचबीर्नेट में ऑब्जेक्ट लाइफसाइक्ल
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
पुनः प्राप्त और फिर ISession.Delete
साथ इसे हटा, यह बच्चों के साथ डेटाबेस से निकाल दिया जाता है (मैं झरना-सब चालू है मेरी मैपिंग फाइल में)। Instrument
हालांकि स्मृति में अभी भी मौजूद है। उदाहरण के लिए:
[Test]
public void CascadeTest()
{
int instrumentId = 1;
IInstrumentRepo instruments = DAL.RepoFactory.CreateInstrumentRepo(_session);
Instrument i = instruments.GetById<Instrument>(instrumentId); // retrieve an instrument from the db
foreach (BrokerInstrument bi in i.BrokerInstruments)
{
Debug.Print(bi.MinIncrement.ToString()); // make sure we can see the children
}
instruments.Delete<Instrument>(i); // physically delete the instrument row, and children from the db
IBrokerInstrumentRepo brokerInstruments = DAL.RepoFactory.CreateBrokerInstrumentRepo(_session);
BrokerInstrument deletedBrokerInstrument = brokerInstruments.GetById<BrokerInstrument>(1); // try and retrieve a deleted child
Assert.That(instruments.Count<Instrument>(), Is.EqualTo(0)); // pass (a count in the db = 0)
Assert.That(brokerInstruments.Count<BrokerInstrument>(), Is.EqualTo(0)); // pass (a count of children in the db = 0)
Assert.That(i.BrokerInstruments.Count, Is.EqualTo(0)); // fail because we still have the i object in memory, although it is gone from the db
}
स्मृति में वस्तु के बारे में सबसे अच्छा अभ्यास क्या है? अब मैं एक असंगत स्थिति में हूं क्योंकि मेरे पास स्मृति में Instrument
ऑब्जेक्ट है जो डेटाबेस में मौजूद नहीं है। मैं एक नौसिखिया प्रोग्रामर हूं इसलिए लिंक के साथ वर्बोज़ उत्तर बहुत सराहना की जाती है।