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. LeetCode OJ:Sudoku Solver(数独游戏)

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. LeetCode OJ:Decode Ways(解码方法)

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  3. NAT&Port Forwarding&Port Triggering

    NAT     Nat,网络地址转换协议.主要功能是实现局域网内的本地主机与外网通信.     在连接外网时,内部Ip地址需要转换为网关(一般为路由器Ip地址)(端口号也需要相应的转换)     如: ...

  4. oracle和sql server 比较

    Oracle   SQLServer   比较 字符数据类型  CHAR  CHAR  都是固定长度字符资料但oracle里面最大度为2kb,SQLServer里面最大长度为8kb 变长字符数据类型  ...

  5. 如何使用JFinal开发javaweb

    介绍开始: 编辑器:MyEclipse; 数据库:MySQL; 服务器:tomcat; 1 首先新建web项目 要强调的是Target runtime必须选择为None.然后点击两次Next,选中创建 ...

  6. Service的启动过程分析

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/76039510

  7. web前端开发中的命名规范

      (一)主体 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中: ...

  8. windows之死活格式化不了D/E/F盘

    唉 见教程:Win10系统无法格式化电脑D盘的解决方法

  9. 软件包 com.baidu.location

    http://developer.baidu.com/map/loc_refer/index.html?com/baidu/location/package-summary.html

  10. Intro to DBSCAN

    DBSCAN Density-Based Spatial Clustering of Application with Noise It can discover cluster of arbitra ...