[C# Advanced Series] [MEF Framework (4)]

C# Advanced Series

Chapter 4 [C# Advanced Series] [MEF Framework (4)]
Chapter 3 [C# Advanced Series] [MEF Framework (3)]
Chapter 2 [C# Advanced Series] [MEF Framework (2)]
First Chapter [C# Advanced Series] [MEF Framework (1)]



Preface

In (1), we have given a basic introduction to MEF. Here we will introduce the delayed loading and metadata of MEF.


1. Lazy loading

Lazy<T, TMetadata> is a type provided by MEF to hold indirect references to exports. In addition to the export object itself, you can also obtain export metadata, or information describing the export object.

Each Lazy<T, TMetadata> contains an IOperation object representing the actual operation and an IOperationData object representing the metadata.

There is no change in the settings of delayed loading at the export part, just keep the original settings.

When importing, you need to use the Lazy class provided by .NET to complete.

Objects encapsulated by Lazy automatically provide a lazy loading mechanism. The way to access the object has also changed slightly. Of course, it cannot be accessed directly through the property (pro.Services_MathBook.GetBookName()), but needs to be accessed through Lazy.Value (pro.Services_MathBook.Value.GetBookName()). This property The value contains even lazy-loaded objects.
In addition, the Lazy.IsValueCreated property provides the function of determining whether the object is loaded. If it is true, it means that the object has been loaded, otherwise it is not loaded.

namespace MEF_P1
{
    
    
    public interface IBookService
    {
    
    
        string BookName {
    
     get; set; }
        string GetBookName();
    }
    [Export("ContractName_MathBook", typeof(IBookService))]
    public class MathBook : IBookService
    {
    
    
        public string BookName {
    
     get; set; }
        public string GetBookName()
        {
    
    
            return "MathBook";
        }
    }
    [Export("ContractName_ChineseBook", typeof(IBookService))]
    public class ChineseBook : IBookService
    {
    
    
        public string BookName {
    
     get; set; }
        public string GetBookName()
        {
    
    
            return "ChineseBook";
        }
    }
    class Program
    {
    
    
        [Import("ContractName_MathBook", typeof(IBookService))]
        public Lazy<IBookService> Services_MathBook {
    
     get; set; }
        [Import("ContractName_ChineseBook", typeof(IBookService))]
        public Lazy<IBookService> Services_ChineseBook {
    
     get; set; }
        static void Main(string[] args)
        {
    
    
            Program pro = new Program();
            pro.Compose();
            if (pro.Services_MathBook != null)
            {
    
    
                Console.WriteLine(pro.Services_MathBook.Value.GetBookName());
            }
            if (pro.Services_ChineseBook != null)
            {
    
    
                Console.WriteLine(pro.Services_ChineseBook.Value.GetBookName());
            }
            Console.Read();
        }
        private void Compose()
        {
    
    
            //创建一个程序集目录,用于从一个程序集获取所有的组件定义
            AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            //创建容器
            CompositionContainer container = new CompositionContainer(catalog);
            //组合部件:如果该类有Import,就会自动去寻找Export
            container.ComposeParts(this);
        }
    }
}

Summarize

Dependency Inversion Principle: High-level templates should not depend on low-level templates, both should depend on abstractions, and abstractions should not depend on details.

Guess you like

Origin blog.csdn.net/Aflashstar/article/details/129101981