खोज…


टिप्पणियों

एमईएफ की अन्य तकनीकों पर एक बड़ा लाभ यह है कि उलटा नियंत्रण प्रणाली का समर्थन करता है, यह बहुत अधिक (यदि कोई हो) कॉन्फ़िगरेशन की आवश्यकता के बिना, डिजाइन-समय पर ज्ञात नहीं होने वाली निर्भरता का समर्थन करता है।

सभी उदाहरणों के लिए System.ComponentModel.Composition विधानसभा के संदर्भ की आवश्यकता होती है।

इसके अलावा, सभी (मूल) उदाहरणों का उपयोग उनके नमूना व्यवसाय वस्तुओं के रूप में किया जाता है:

using System.Collections.ObjectModel;

namespace Demo
{
    public sealed class User
    {
        public User(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }

        public int Id { get; }
        public string Name { get; }
        public override string ToString() => $"User[Id: {this.Id}, Name={this.Name}]";
    }

    public interface IUserProvider
    {
        ReadOnlyCollection<User> GetAllUsers();
    }
}

एक प्रकार का निर्यात (बेसिक)

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;

namespace Demo
{
    [Export(typeof(IUserProvider))]
    public sealed class UserProvider : IUserProvider
    {
        public ReadOnlyCollection<User> GetAllUsers()
        {
            return new List<User>
            {
                new User(0, "admin"),
                new User(1, "Dennis"),
                new User(2, "Samantha"),
            }.AsReadOnly();
        }
    }
}

इसे वस्तुतः कहीं भी परिभाषित किया जा सकता है; यह सब मायने रखता है कि आवेदन जानता है कि इसे कहां देखना है (इसके द्वारा बनाई जाने वाली CompartPartCatalogs के माध्यम से)।

आयात करना (मूल)

using System;
using System.ComponentModel.Composition;

namespace Demo
{
    public sealed class UserWriter
    {
        [Import(typeof(IUserProvider))]
        private IUserProvider userProvider;

        public void PrintAllUsers()
        {
            foreach (User user in this.userProvider.GetAllUsers())
            {
                Console.WriteLine(user);
            }
        }
    }
}

यह एक प्रकार है जिसमें IUserProvider पर निर्भरता है, जिसे कहीं भी परिभाषित किया जा सकता है। पिछले उदाहरण की तरह, यह सब मायने रखता है कि आवेदन जानता है कि मिलान निर्यात के लिए कहाँ देखना है (के माध्यम से ComposablePartCatalogs बनाता है)।

कनेक्टिंग (मूल)

अन्य (बेसिक) उदाहरण ऊपर देखें।

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace Demo
{
    public static class Program
    {
        public static void Main()
        {
            using (var catalog = new ApplicationCatalog())
            using (var exportProvider = new CatalogExportProvider(catalog))
            using (var container = new CompositionContainer(exportProvider))
            {
                exportProvider.SourceProvider = container;

                UserWriter writer = new UserWriter();

                // at this point, writer's userProvider field is null
                container.ComposeParts(writer);

                // now, it should be non-null (or an exception will be thrown).
                writer.PrintAllUsers();
            }
        }
    }
}

जब तक अनुप्रयोग के असेंबली खोज पथ में कुछ है [Export(typeof(IUserProvider))] , UserWriter के संगत आयात संतुष्ट हो जाएंगे और उपयोगकर्ताओं को मुद्रित किया जाएगा।

अन्य प्रकार के कैटलॉग (उदाहरण के लिए, DirectoryCatalog ) का उपयोग (या इसके अलावा) ApplicationCatalog बजाय निर्यात को आयात करने के लिए अन्य स्थानों पर देखने के लिए किया जा सकता है।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow