博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ninject Lazy Load问题
阅读量:6173 次
发布时间:2019-06-21

本文共 5433 字,大约阅读时间需要 18 分钟。

参考:

http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject 

方案一:

public class Module : NinjectModule{    public override void Load()    {        Bind(typeof(Lazy<>)).ToMethod(ctx =>                 GetType()                    .GetMethod("GetLazyProvider", BindingFlags.Instance | BindingFlags.NonPublic)                    .MakeGenericMethod(ctx.GenericArguments[0])                    .Invoke(this, new object[] { ctx.Kernel }));    }    protected Lazy
GetLazyProvider
(IKernel kernel) { return new Lazy
(() => kernel.Get
()); }}

 

方案二:

namespace LayzyLoadTest{    [TestClass]    public class UnitTest1    {        private IKernel InitKernel()        {            Ninject.IKernel kernel = new Ninject.StandardKernel(new LazyBinding());            //kernel.Load
(); kernel.Bind
().To
(); kernel.Bind
().To
(); kernel.Bind
().To
().Named("comm"); kernel.Bind
().To
().Named("lazy"); return kernel; } [TestMethod] public void TestMethod1() { var comm = InitKernel().Get
("comm"); comm.CurSpeed(); comm.ShowSpeed(); //Console.WriteLine("------------------------------------------------"); //var lazy = kernel.Get
("lazy"); lazy.CurSpeed(); lazy.ShowSpeed(); }
 
 
[TestMethod]        public void Lazy()        {            var lazy = InitKernel().Get
("lazy"); lazy.CurSpeed(); Console.WriteLine("----over curspeed--------------------"); lazy.ShowSpeed(); } }}

 

namespace LayzyLoadTest.LayzyClasses{    #region Persons    interface IPerson    {        int RunSpeed();    }    class Child : IPerson    {        public Child()        {            Console.WriteLine("Ctor Child");        }        public int RunSpeed()        {            Console.WriteLine("Child's Speed");            return 100;        }    }    class Father : IPerson    {        public Father()        {            Console.WriteLine("Ctor Father");        }        public int RunSpeed()        {            Console.WriteLine("Father's Speed");            return 1000;        }    }    interface IVehicle    {        int Improve();    }    class Car : IVehicle    {        public Car()        {            Console.WriteLine("Car's Ctor");        }        public int Improve()        {            Console.WriteLine("Car Improve");            return 1000;        }    }    class Bicycle : IVehicle    {        public Bicycle()        {            Console.WriteLine("Bicycle's Ctor");        }        public int Improve()        {            Console.WriteLine("Bicycle Improve");            return 100;        }    }    #endregion    #region Place    interface IPlace    {        int CurSpeed();        int ShowSpeed();    }    class Road : IPlace    {        private readonly IPerson _person;        private readonly IVehicle _vehicle;        public Road(IPerson person, IVehicle vehicle)        {            Console.WriteLine(" Road's Ctor ");            _person = person;            _vehicle = vehicle;        }        public int CurSpeed()        {            Console.WriteLine("Road CurSpeed");            return _person.RunSpeed();        }        public int ShowSpeed()        {            Console.WriteLine("Road ShowSpeed");            return _person.RunSpeed() * _vehicle.Improve();        }    }    class LazyRoad : IPlace    {        private readonly Lazy
_person; private readonly Lazy
_vehicle; public LazyRoad(Lazy
person, Lazy
vehicle) { Console.WriteLine(" LazyRoad's Ctor "); _person = person; _vehicle = vehicle; } public int CurSpeed() { Console.WriteLine("LazyRoad CurSpeed"); return _person.Value.RunSpeed(); } public int ShowSpeed() { Console.WriteLine("LazyRoad ShowSpeed"); return _person.Value.RunSpeed() * _vehicle.Value.Improve(); } } #endregion}
namespace LayzyLoadTest{    public class LazyBinding : NinjectModule    {        public override void Load()        {            this.Bind(typeof(Lazy<>))                .ToMethod(                    context =>                    ((ILazyLoader)Activator.CreateInstance(typeof(LazyLoader<>).MakeGenericType(context.GenericArguments),                                                           new object[] { context.Kernel })).Loader);        }        public interface ILazyLoader        {            object Loader { get; }        }        public class LazyLoader
: ILazyLoader { private readonly IKernel _kernel; private static readonly Func
> _lazyLoader = k => new Lazy
(() => k.Get
()); public LazyLoader(IKernel kernel) { if (kernel == null) throw new ArgumentNullException("kernel"); this._kernel = kernel; } public object Loader { get { return _lazyLoader(this._kernel); } } } }}

 

转载于:https://www.cnblogs.com/duanweishi/p/4583327.html

你可能感兴趣的文章
队列组 iOS之多线程GCD(二)
查看>>
Flutter滚动, 中间显示整图, 前后露出部分图
查看>>
Flutter入坑指南:开发环境搭建
查看>>
跨Navigation跳转(类似微信)方案二
查看>>
JavaScript 复习之 对象的继承
查看>>
从开源小白到 Apache Member,我的成长之路
查看>>
logstash简介
查看>>
Java多线程之synchronized理论
查看>>
Android NestedScrolling解决滑动冲突问题(2) - fling问题与NestedScroll++
查看>>
Tomcat和JVM的性能调优总结
查看>>
硬件线程和软件线程的区别
查看>>
时间戳前
查看>>
11月22日晚上海交大《PMI敏捷实践指南解读》线上沙龙欢迎你的参与!
查看>>
初识 Linux (VMware、CentOS 7)
查看>>
使用SpringMVC完成文件上传
查看>>
mysql Load Data InFile 的用法
查看>>
Go new vs make
查看>>
【云宏大讲坛】超融合,融合的不仅是基础架构
查看>>
pytnon入门的一些小实例
查看>>
ubuntu下的dock工具
查看>>