मैं पीओसीओ का उपयोग कर प्रोटोटाइप ईएफ एप्लिकेशन पर काम कर रहा हूं। मुख्य रूप से ढांचे के परिचय के रूप में मैं एक अच्छी संरचना में आवेदन स्थापित करने के लिए एक अच्छा तरीका सोच रहा हूं। बाद में मैं इसमें डब्ल्यूसीएफ को शामिल करने की योजना बना रहा हूं।एक ईएफ अनुप्रयोग की संरचना सेट करना
मैंने क्या किया गया है निम्नलिखित है:
1) मैं एक edmx फ़ाइल बनाया है, लेकिन कोड पीढ़ी संपत्ति के साथ कोई नहीं पर सेट और मेरे डेटाबेस स्कीमा उत्पन्न,
2) मैं Pocos बनाया जो सभी की तरह लग रहे:
public class Person
{
public Person()
{
}
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
3) मैं एक संदर्भ बनाया
public class PocoContext : ObjectContext, IPocoContext
{
private IObjectSet<Person> persons;
public PocoContext() : base("name=PocoContainer", "PocoContainer")
{
ContextOptions.LazyLoadingEnabled = true;
persons= CreateObjectSet<Person>();
}
public IObjectSet<Person> Persons
{
get
{
return persons;
}
}
public int Save()
{
return base.SaveChanges();
}
}
इंटरफ़ेस इस तरह दिखता है:
public interface IPocoContext
{
IObjectSet<Person> Persons { get; }
int Save();
}
4) अंत में मैं एक भंडार बनाया है, एक अंतरफलक को लागू करने:
public class PersonRepository : IEntityRepository<Person>
{
private IPocoContext context;
public PersonRepository()
{
context = new PocoContext();
}
public PersonRepository(IPocoContext context)
{
this.context = context;
}
// other methods from IEntityRepository<T>
}
public interface IEntityRepository<T>
{
void Add(T entity);
List<T> GetAll();
T GetById(int id);
void Delete(T entity);
}
अब, जब मैं इस के साथ प्रयोग करना पर मिलता है, इस डिजाइन मुझे तय दृष्टांत के लिए एक हर बार जब मैं लाने या कुछ डेटा उत्परिवर्तित करने के लिए, इस तरह चाहते हैं भंडार:
using (var context = new PocoContext())
{
PersonRepository prep = new PersonRepository();
List<Person> pers = prep.GetAll();
}
किसी तरह यह सिर्फ गलत और त्रुटिपूर्ण लगता है, दूसरे हाथ पर, सिर्फ डी में हर भंडार instantiating rived संदर्भ संभावित रूप से तत्काल वस्तुओं की वजह से बहुत अच्छा महसूस नहीं करता है, मुझे शायद इसकी आवश्यकता नहीं है।
इस डिजाइन को कैसे बनाया जाए इस पर कोई सुझाव? क्या मुझे इसे इस तरह से छोड़ना चाहिए? ऐसा करने पर मुझे किसी चीज को सामान्य रूप से जोड़ना या टालना चाहिए?
यह किस प्रकार का एप्लिकेशन है। वेबसाइट सेवा, डब्ल्यूपीएफ-ऐप, कुछ और? – alun
इस स्थिति में यह केवल एक कंसोल एप्लिकेशन है, क्योंकि यह केवल एक नंगे-न्यूनतम प्रोटोटाइप है। – duress
कारण मैं पूछता हूं कि आप अपने संदर्भ को कैसे संभालेंगे, आवेदन के प्रकार से काफी प्रभावित है। उदाहरण के लिए एक डब्ल्यूपीएफ ऐप में प्रति फॉर्म एक संदर्भ और एक वेब ऐप में प्रति http एक अनुरोध और एक webservice में एक विधि प्रति विधि कॉल के लिए एक संदर्भ है। – alun