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. MySql设计规范及SQL索引优化【呕心之作】

    数据库及表结构基本设计规范 1. 所有表必须使用Innodb存储引擎 没有特殊要求(即Innodb无法满足的功能如:列存储,存储空间数据等)的情况下,所有表必须使用Innodb存储引擎(mysql5. ...

  2. laravel中上传图片之后图片的处理

    $file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...

  3. poj 1258 Agri-Net 最小生成树 prim算法+heap不完全优化 难度:0

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41230   Accepted: 16810 Descri ...

  4. ZOJ 2975 Kinds of Fuwas(暴力+排列组合)

    Kinds of Fuwas Time Limit: 2 Seconds      Memory Limit: 65536 KB In the year 2008, the 29th Olympic ...

  5. _routing字段介绍

    一个document通过以下公式被路由到该索引下一个特定的分片: shard_num = hash(_routing) % num_primary_shards _routing的默认值是文档的_id ...

  6. Android程序员学WEB前端(2)-HTML(2)-锚点链接列表表单-Sublime

    转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76522417觉得博文有用,请点赞,请评论,请关注,谢谢!~锚点 链接 列表 表单 &l ...

  7. java中高级面试题整理及参考答案

    面试问题: 一.Java基础方面: 1.Java面相对象的思想的理解(主要是多态): http://blog.csdn.net/zhaojw_420/article/details/70477636 ...

  8. Python3 字符串操作

    截掉指定字符串 # 截掉指定字符串 string.strip("what you want to delete") #截掉字符串左边的空格 string.lstrip() #截掉字 ...

  9. 【剑指offer】输入一颗二叉树的根节点,求二叉树的深度,C++实现

    原创博文,转载请注明出处! # 题目 # 举例        下图二叉树的深度为4,最长路径为1-2-5-7. # 思路(递归)       如果一个树只有一个节点,它的深度为1: 如果根节点只有左子 ...

  10. android 获取 图片或视频略缩图

    /** * 根据指定的图像路径和大小来获取缩略图 此方法有两点好处: 1. * 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度, * 第二次读取的bitmap是根 ...