namespace LayzyLoadTest
{
    [TestClass]
    public class UnitTest1
    {

        private IKernel InitKernel()
        {
            Ninject.IKernel kernel = new Ninject.StandardKernel(new LazyBinding());

            //kernel.Load<LazyBinding>();

            kernel.Bind<IPerson>().To<Father>();
            kernel.Bind<IVehicle>().To<Car>();

            kernel.Bind<IPlace>().To<Road>().Named("comm");
            kernel.Bind<IPlace>().To<LazyRoad>().Named("lazy");

            return kernel;
        }

        [TestMethod]
        public void TestMethod1()
        {

            var comm = InitKernel().Get<IPlace>("comm");

            comm.CurSpeed();
            comm.ShowSpeed();

            //Console.WriteLine("------------------------------------------------");

            //var lazy = kernel.Get<IPlace>("lazy");

            ////lazy.CurSpeed();
            ////lazy.ShowSpeed();

        }

 
 
        [TestMethod]
        public void Lazy()
        {

            var lazy = InitKernel().Get<IPlace>("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<IPerson> _person;
        private readonly Lazy<IVehicle> _vehicle;

        public LazyRoad(Lazy<IPerson> person, Lazy<IVehicle> 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<T> : ILazyLoader
        {
            private readonly IKernel _kernel;
            private static readonly Func<IKernel, Lazy<T>> _lazyLoader = k => new Lazy<T>(() => k.Get<T>());

            public LazyLoader(IKernel kernel)
            {
                if (kernel == null)
                    throw new ArgumentNullException("kernel");

                this._kernel = kernel;
            }

            public object Loader { get { return _lazyLoader(this._kernel); } }
        }
    }
}

Ninject Lazy Load的更多相关文章

  1. Ninject Lazy Load问题

    参考: http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject  方案一: public class Module : ...

  2. 在 MVC 中使用 ninject Lazy Load的一个想法

    这这里先声明一下,引用了一个 (http://www.edcourtenay.co.uk/musings-of-an-idiot/2012/11/23/lazy-binding-with-ninjec ...

  3. Lazy Load, 延迟加载图片的 jQuery 插件.

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  4. jQuery延迟加载插件(Lazy Load)详解

    最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...

  5. 延迟加载图片的 jQuery 插件:Lazy Load

    网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...

  6. Lazy Load 图片延迟加载(转)

    jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...

  7. jQuery Lazy Load 图片延迟加载

    基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...

  8. about hibernate lazy load and solution

    about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...

  9. Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

随机推荐

  1. vue创建组件

    vue创建组件是很容易的: js: Vue.component("component-item",{   //component-item就是我们在HTML页面上引用的组件,它会在 ...

  2. HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

  3. 破解google翻译API全过程

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6554340.html 前言 google的翻译不得不承认它 ...

  4. Webstorm常用快捷键大全

    webstorm应该是目前最强的js编辑器了,结合sublime text可以很效率的开发项目.今天整理了一些webstorm比较实用的快捷键: Ctrl+/ 或 Ctrl+Shift+/ 注释(// ...

  5. ant+jmeter+jenkins+git持续集成以及邮件报告展示

    前序准备工作: ant--下载地址:http://ant.apache.org/bindownload.cgi jmeter--下载地址:http://jmeter.apache.org/downlo ...

  6. git clone 提示输入git@xxx的密码

    如下: suse:~/ecox # git clone git@vcs.in.ww-it.cn:ecox/ecox.git 正克隆到 'ecox'... git@vcs.in.ww-it.cn's p ...

  7. Eclipse下配置Maven

    1.修改maven根目录下的conf/setting.xml文件,主要修改localRepository属性,用于管理maven下载的jar文件存放的位置. 2.修改eclipse的maven配置,w ...

  8. 那些年提交AppStore审核踩过的坑

    此文刚刚上了CocoaChina的首页:那些年提交AppStore审核踩过的坑  欢迎围观,谢谢大家支持. //add by 云峰小罗,2016.08.04 做iOS开发近5年了,每次提交版本时不可谓 ...

  9. 一名十年Java程序员回忆阿里面试经历——揭开阿里面试的“遮羞布”

    阿里面试经历 去阿里面试可以说非常非常的偶然和戏剧性,因为本人根本没投简历,以至于阿里hr给我电话的时候我一度认为是诈骗电话.因为深圳这家公司不错我还想在这里干个两年左右再考虑考虑. 这个时候的本人已 ...

  10. Android Hook框架Xposed详解

    1 Introduction 1.1  概述 Xposed 是 GitHUB 上 rovo89 大大设计的一个针对 Android 平台的动态劫持项目,通过替换 /system/bin/app_pro ...