ImportingConstructor and it use

There is a ImportingConstructor atribute where you can annotate on the constructor, but why will we use it? 

From the StackOverflow (Why use ImportingConstructor, from JaredPar), it has the following lines 

Quote: 

The problem with using [Import] is that it separates the creation of an object into two distinct and observable phases: created and initialized. Where [ImportingConstructor] allows this to remain as a single phase exactly as it would for every other .Net object.

This difference becomes observable in a number of ways

  1. Adding a new [Import] on a field changes the logical contract of a type. Yet it doesn't change the public or usage contract. This means any code which previously compiled will continue to compile even though the objects dependencies have changed (think unit tests). This takes what should be a compile time error and makes it a runtime one
  2. Code Contracts are unusable if you have an [Import]. The contract verification engine properly recognizes that all fields can exist as null values and will require a check before every use of a field.
  3. Even though your object can logically have fields which are set an init time and never reset you can't express this with readonly as you would with a normal C# object

and Mathew Abbott has this:

In terms of MEF, you can get away with using [Import] instead of [ImportingConstructor] as MEF will throw an exception when it cannot satisfy all imports on a type, and will only return the type after both initialisation ([ImportingConstructor]) and then [Import]s.

Constructor injection is generally preferable.


So here is the code that you can compare for yourself why to use ImportingConstructor..

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using log4net;
using log4net.Core;

namespace ImportingConstructorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Debugger.Break();

            var batch = new CompositionBatch();
            CompositionContainer container;
            var reflectionCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());

            var extensionPath = System.IO.Path.Combine(Environment.CurrentDirectory, "extensions");
            if (System.IO.Directory.Exists(extensionPath))
            {
                var directoryCatalog = new DirectoryCatalog(extensionPath);
                var defaultCatalogEp = new CatalogExportProvider(reflectionCatalog);
                container = new CompositionContainer(directoryCatalog, defaultCatalogEp);
                defaultCatalogEp.SourceProvider = container;
            }
            else
                container = new CompositionContainer(reflectionCatalog);

            container.Compose(batch);

            //var service1 = container.GetExportedValue<MyService>();
            //var service2 = container.GetExportedValue<MyService2>();
        }
    }

    [Export]
    public class MyService
    {

        [Import]
        public ILog Logger { get; set; }

        public void SaySomething()
        {
            Logger.Info("Something");
        }

    }

    [Export]
    public class MyService2
    {
        private readonly ILog _logger;

        [ImportingConstructor]
        public MyService2(ILog logger)
        {
            if (logger == null) throw new ArgumentNullException("logger");

            _logger = logger;
        }

        public void SaySomething()
        {
            _logger.Info("Sometihng else");
        }
    }
}


转载于:https://my.oschina.net/u/854138/blog/129233

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值