2009-07-23 10 views
7

मैं एमवीपी और इकाई फ्रेमवर्क दुनिया के लिए बिल्कुल नया हूं इसलिए मेरे साथ भालू।ईएफ ऑब्जेक्ट कॉन्टेक्स्ट, सेवा और रिपोजिटरी - संदर्भ जीवनकाल प्रबंधित करना।

मेरे पास वर्तमान में एक व्यू + प्रेजेंटर संयोजन है, दृश्य में दो ईवेंट संपादित और हटाए गए हैं और प्रस्तुतकर्ता सिर्फ इन घटनाओं के लिए सुनता है। मेरे पास एक सेवा ऑब्जेक्ट और रिपॉजिटरीज भी स्थापित है। सेवा परत कुछ भंडार कार्यान्वयन करना, जिससे एक ObjectContext तो निर्माण के क्रम में (इसके नीचे एक करने के लिए शीर्ष वस्तु में गुजर रहा है ले लेता है:

ObjectContext 
    | 
    V 
Repositries 
    | 
    V 
Service Object 
    | 
    V 
Presenter 

अब समस्या यह है कि जब मैं शीर्ष पर वस्तु संदर्भ बनाने के लिए, यह पूरे समय जीवित है कि प्रस्तुतकर्ता जीवित है जिसका अर्थ है कि संपादित करें और हटाएं सेवा से एक ही संदर्भ उदाहरण का उपयोग करें।

तो सर्विसऑब्जेक्ट.डिलेट और सर्विस ऑब्जेक्ट को कॉल करना। उसी संदर्भ का उपयोग करें जो परिवर्तन ट्रैकिंग को प्रबंधित करना मुश्किल बनाता है। जो मैं समझता हूं उससे संदर्भ वास्तव में केवल अल्पकालिक रहना चाहिए और केवल काम की एक इकाई के लिए, मेरे लिए संपादन और हटाएं दोनों अलग-अलग काम हैं।

आप इकाई ढांचे के साथ DI कैसे करते हैं और अभी भी संदर्भ जीवन समय का प्रबंधन करते हैं?

मैंने लोगों को रिपोजिटरी के पक्ष में ऑब्जेक्ट संदर्भ को नया देखा है, क्या यह एक अच्छा पैटर्न है।

ServiceObject{ 
    public void Edit(// some args) { 
    Using(var context = new MyObjectContext) { 
     var repo = new MyRepo(context); 
     var entity = repo.GetForID(12); 
     // Do some stuff for edit 
     context.SaveChanges(); 
    } 
    } 
} 

लेकिन मुझे अब अपनी भंडार ServiceObject के निर्माता में गुजर रहा हूँ और डि नहीं कर रही :(तो मुझे लगता है कि जैसे कि यह कार्य करें:

या मैं उस सेवा वस्तु में, कुछ की तरह करना चाहिए।

मैं इस स्थिति में क्या कर सकते हैं?

किसी को भी किसी भी ओपन सोर्स प्रोजेक्ट है कि मुझे लगता है कि देख सकते हैं पता है मुझे इस समस्या से मदद मिल सकती है।

धन्यवाद।

उत्तर

24

मैं शीर्ष (प्रस्तुतकर्ता) से जाऊंगा और प्रतिभागियों के बीच संबंधों का वर्णन करूंगा।

प्रस्तुतकर्ता निर्भरता के माध्यम से सेवा वस्तु प्राप्त करता है। सेवा कार्यक्षमता अपने अनुबंध का उपयोग कर उल्लिखित है:

class Presenter 
{ 
    public Presenter(IService service) 
    { 
    ... 
    } 
} 

सेवा कार्यान्वयन विशेष डेटा का उपयोग परत कार्यान्वयन से निकाला गया है। असल में जब भी सेवा कुछ क्रियाएं करती है जिसके लिए डेटा स्रोत इंटरैक्शन की आवश्यकता होती है, यह काम की इकाई का एक उदाहरण बनाता है और इसे समाप्त होने पर इसे डिस्पोजेक्ट करता है।

interface IService 
{ 
    void Do(); 
} 

class Service : IService 
{ 
    private readonly IUnitOfWorkFactory unitOfWorkFactory; 
    public Service(IUnitOfWorkFactory unitOfWorkFactory) 
    { 
    this.unitOfWorkFactory = unitOfWorkFactory; 
    } 

    public void Do() 
    { 
    // Whenever we need to perform some data manipulation we create and later dispose 
    // dispose unit of work abstraction. It is created through a factory to avoid 
    // dependency on particular implementation. 
    using(IUnitOfWork unitOfWork = this.unitOfWorkFactory.Create()) 
    { 
     // Unit of work holds Entity Framework ObjectContext and thus it used 
     // create repositories and propagate them this ObjectContext to work with 
     IRepository repository = unitOfWork.Create<IRepository>(); 
     repository.DoSomethingInDataSource(); 

     // When we are done changes must be commited which basically means committing 
     // changes of the underlying object context. 
     unitOfWork.Commit(); 
    } 
    } 
} 


/// <summary> 
/// Represents factory of <see cref="IUnitOfWork"/> implementations. 
/// </summary> 
public interface IUnitOfWorkFactory 
{ 
    /// <summary> 
    /// Creates <see cref="IUnitOfWork"/> implementation instance. 
    /// </summary> 
    /// <returns>Created <see cref="IUnitOfWork"/> instance.</returns> 
    IUnitOfWork Create(); 
} 

/// <summary> 
/// Maintains a list of objects affected by a business transaction and coordinates the writing out of 
/// changes and the resolution of concurrency problems. 
/// </summary> 
public interface IUnitOfWork : IDisposable 
{ 
    /// <summary> 
    /// Creates and initializes repository of the specified type. 
    /// </summary> 
    /// <typeparam name="TRepository">Type of repository to create.</typeparam> 
    /// <returns>Created instance of the repository.</returns> 
    /// <remarks> 
    /// Created repositories must not be cached for future use because once this 
    /// <see cref="IUnitOfWork"/> is disposed they won't be able to work properly. 
    /// </remarks> 
    TRepository Create<TRepository>(); 

    /// <summary> 
    /// Commits changes made to this <see cref="IUnitOfWork"/>. 
    /// </summary> 
    void Commit(); 
} 

/// <summary> 
/// Represents factory of <see cref="UnitOfWork"/>s. 
/// </summary> 
public class UnitOfWorkFactory : IUnitOfWorkFactory 
{ 
    private readonly IUnityContainer container; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="UnitOfWorkFactory"/> class. 
    /// </summary> 
    /// <param name="container"> 
    /// Dependency injection container instance used to manage creation of repositories 
    /// and entity translators. 
    /// </param> 
    public UnitOfWorkFactory(IUnityContainer container) 
    { 
       this.conainer = container; 
    } 


    /// <summary> 
    /// Creates <see cref="IUnitOfWork"/> implementation instance. 
    /// </summary> 
    /// <returns>Created <see cref="IUnitOfWork"/> instance.</returns> 
    public IUnitOfWork Create() 
    { 
     var unitOfWork = this.container.Resolve<UnitOfWork>(); 
     unitOfWork.SetupObjectContext(); 
     return unitOfWork; 
    } 

    ... other members elidged for clarity 
} 

IUnitOfWork के कार्यान्वयन IUnityContainer के कहने प्राप्त करता है और उसके बाद बच्चे कंटेनर बनाता है और वहाँ उदाहरण ObjectContext पंजीकृत करता है। इस बच्चे के कंटेनर का उपयोग भंडार बनाने और ऑब्जेक्ट कॉन्टेक्स्ट को प्रचारित करने के लिए किया जाएगा।

class UnitOfWork : IUnitOfWork 
{ 
    private readonly IUnityContainer container; 
    private ObjectContext objectContext; 

    public UnitOfWork (IUnityContainer container) 
    { 
    this.container = container.CreateChildContainer(); 
    } 

    public void SetupObjectContext() 
    { 
    this.objectContext = ... // Create object context here 
    this.container.RegisterInstance(context.GetType(), context); 
    } 

    public void Create<TRepository>() 
    { 
    // As long as we registered created object context instance in child container 
    // it will be available now to repositories during resolve 
    return this.container.Resolve<TRepository>(); 
    } 

    public void Commit() 
    { 
    this.objectContext.SaveChanges(); 
    } 
} 

class Repository : IRepository 
{ 
    private readonly SomeObjectContext objectContext; 

    public Repository(SomeObjectContext objectContext) 
    { 
    this.objectContext = objectContext; 
    } 

    public void DoSomethingInDataSource() 
    { 
    // You can use object context instance here to do the work 
    } 
} 
+1

uuh यह अच्छी बात है:

यहाँ IUnitOfWork का एक सरलीकृत कार्यान्वयन है! – Roubachof

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