参考:

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[])
.Invoke(this, new object[] { ctx.Kernel }));
} protected Lazy<T> GetLazyProvider<T>(IKernel kernel)
{
return new Lazy<T>(() => kernel.Get<T>());
}
}

方案二:

Ninject Lazy Load

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. 在 MVC 中使用 ninject Lazy Load的一个想法

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

  2. Ninject Lazy Load

    namespace LayzyLoadTest { [TestClass] public class UnitTest1 { private IKernel InitKernel() { 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. 从0开始学习 GITHUB 系列之「团队合作利器 BRANCH」【转】

    本文转载自:http://stormzhang.com/github/2016/07/09/learn-from-github-from-zero6/ 版权声明:本文为 stormzhang 原创文章 ...

  2. 如何生成ssh密钥对

    答:执行以下命令即可,生成的密钥对在~/.ssh下,会生成两个文件,一个id_rsa和id_rsa.pub,前者是私钥,后者是公钥 ssh-keygen -t rsa -C "your_em ...

  3. Jwt访问api提示401错误 Authorization has been denied for this request

    教程  http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-ap ...

  4. Cache与主存地址映像计算例题

    一.全相连映像 主存中任何一个块均可以映像装入到Cache中的任何一个块的位置上.主存地址分为块号和块内地址两部分,Cache地址也分为块号和块内地址.Cache的块内地址部分直接取自主存地址的块内地 ...

  5. C++函数的返回值——返回引用类型&非引用类型

    函数的返回主要分为以下几种情况: 1.主函数main的返回值: 允许主函数main没有返回值就可结束:可将主函数main返回的值视为状态指示器,返回0表示程序运行成功,其他大部分返回值则表示失败. 2 ...

  6. WPF特效和例子

    https://www.cnblogs.com/AaronYang/p/4710428.html

  7. LeetCode 46

    // 又是可以用回溯法做的一道题.class Solution { public: vector<vector<int>> permute(vector<int>& ...

  8. 在页面和请求中分别使用XML Publisher生成PDF报表且自动上传至附件服务器

    两个技术要点: 1.使用TemplateHelper.processTemplate方法生成目标PDF的InputStream流,再使用ftp中上传流的方法将其上传至附件服务器. 2.在请求中调用AM ...

  9. 042——VUE中组件之子组件使用$on与$emit事件触发父组件实现购物车功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. OpenCV 3.2.0 + opencv_contrib+VS2017

    首先本文假定你的电脑已经配置好了OpenCV3.2.0,并且想要在此基础上,添加opencv_contrib.在学习图像识别中的特征点检测和匹配时,需要用到一些常用的算法如FREAK.Surf和Sif ...