में वर्चुअल विधि को कॉल करने के लिए वैकल्पिक मैं अपने सी # pojects के लिए NHibernate का उपयोग कर रहा हूं और इसलिए मेरे पास कई मॉडल वर्ग हैं।सी #
using System;
namespace TestProject.Model
{
public class Room
{
public virtual int Id { get; set; }
public virtual string UniqueID { get; set; }
public virtual int RoomID { get; set; }
public virtual float Area { get; set; }
}
}
इन वस्तुओं का मिलान के साथ NHibernate अब तक ठीक काम करता है:
निम्न उदाहरण मान देता है। अब मैं एक नया कक्ष वस्तु उत्पन्न करना चाहता हूं और मैं इसे डेटाबेस में संग्रहीत करना चाहता हूं। प्रत्येक सदस्य को अलग-अलग सेट करने से बचने के लिए, मैं मॉडल वर्ग में एक नया कन्स्ट्रक्टर जोड़ता हूं। आभासी सदस्यों नीचे मैं लिखने:
public RoomProperty()
{
}
public RoomProperty(int pRoomId, int pArea)
{
UniqueID = Guid.NewGuid().ToString();
RoomID = pRoomId;
Area = pArea;
}
FxCop के साथ अपने कोड का विश्लेषण करना मेरा पीछा बताता है:
"ConstructorShouldNotCallVirtualMethodsRule"
This rule warns the developer if any virtual methods are called in the constructor of a non-sealed type. The problem is that if a derived class overrides the method then that method will be called before the derived constructor has had a chance to run. This makes the code quite fragile.
This page भी बताता है कि क्यों यह गलत है और मैं भी यह समझते हैं। लेकिन मैं इस समस्या को हल करने के लिए शर्मिंदा नहीं हूं।
जब मैं सभी निर्माताओं को मिटाने और निम्न विधि जोड़ें ...
public void SetRoomPropertyData(int pRoomId, int pArea)
{
UniqueID = Guid.NewGuid().ToString();
RoomID = pRoomId;
Area = pArea;
}
.... के बाद मैं मानक निर्माता कहा जाता है मैं नहीं कर सकते मेरे आवेदन शुरू becaue NHibernate आरंभ में विफल रहता है डेटा सेट करने। इसे कहते हैं:
NHibernate.InvalidProxyTypeException: The following types may not be used as proxies:
VITRIcadHelper.Model.RoomProperty: method SetRoomPropertyData should be 'public/protected virtual' or 'protected internal virtual'
लेकिन आभासी के लिए इस विधि की स्थापना जब मैं सिर्फ निर्माता में आभासी सदस्यों सेट के रूप में ही गलती होगी। मैं इन गलतियों (उल्लंघन) से कैसे बच सकता हूं?
क्यों खेतों में मूल्य निर्धारित नहीं करना निर्माण पर गुण नहीं है? –
@ voroninp आप उन क्षेत्रों तक नहीं पहुंच सकते हैं जो आसानी से NHibernate – Andrey
के साथ हैं क्योंकि मेरे मॉडल में वास्तव में लगभग 10 सदस्य हैं और मैं नए कमरे की वस्तुओं qiet iften बनाते हैं। मैं प्रत्येक संपत्ति को अलग से सेट नहीं करना चाहता हूं। – Metalhead89