Ioc 比较

 

 public interface IC {
        }
        public class A
        { 
                IC ic_;
                public A(IC ic) { ic_ = ic; }
       
        }

        public class B : IC {

        }

        [Export]
        public class A_MEF
        {
                [Import]
                public IC ic_{get;set;}
        }
        [Export(typeof(IC))]
        public class B_MEF : IC
        {
        }

 


 class Program
        {
                /// <summary>
                /// install-package Ninject
                /// install-package autofac
                /// install-package Munq.IocContainer
                /// install-package Unity
          /// Install-Package Microsoft.Composition /// </summary> /// <param name="args"></param> static void Main(string[] args) {              Cost("Ninject", () => { IKernel kernel = new StandardKernel(); kernel.Bind<IC>().To<B>(); IC ic = kernel.Get<IC>(); A AFromNinject = new A(ic); });              Cost("Autofac", () => { var builder = new ContainerBuilder(); builder.RegisterType<B>().As<IC>(); builder.RegisterType<A>(); var container = builder.Build(); var AFromAutofac = container.Resolve<A>(); });              Cost("Munq", () => { var ioc_container = new IocContainer(); ioc_container.Register<IC, B>(); var AFromMunq = ioc_container.Resolve<A>(); });               Cost("Unity", () => { var containerUnity = new UnityContainer(); containerUnity.RegisterType<IC, B>(); var AFromUnity = containerUnity.Resolve<A>(); });             Cost("MEF", () =>               {                   var assemblyCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());                   CompositionContainer container = new CompositionContainer(assemblyCatalog);                   var AMEF = container.GetExports<A_MEF>();               });
          }
        static void Cost(string ioc,Action action)
                {
                      
                        var stopwatch = new Stopwatch();
                        stopwatch.Start(); 
                        for (int i=0;i<1000;i++)
                        {
                                 action();
                        }
                        stopwatch.Stop();
                        var ticks = stopwatch.ElapsedMilliseconds;
                        Console.WriteLine(ioc + ": " + (ticks / 1000.00).ToString()+" s");
                }
}

  

 

 

 static void Cost(string ioc,Action action)
                {
                        var stopwatch = new Stopwatch();
                        stopwatch.Start(); 
                        for (int i=0;i<1000;i++)
                        {
                                Task.Run(action); // 换成多线程
                        }
                        stopwatch.Stop();
                        var ticks = stopwatch.ElapsedMilliseconds;
                        Console.WriteLine(ioc + ": " + (ticks / 1000.00).ToString()+" s");
                }

 

posted @ 2014-08-05 16:37  Yu  阅读(256)  评论(0编辑  收藏  举报