参考:

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. 解决margin重叠的问题

    margin重叠有两种情况: 1.兄弟级的垂直块之间,margin这个属性上下边距,会发生重叠的情况 解决办法:float浮动或display:inline-block 2 .父子级的块之间,子级的上 ...

  2. IntelliJ IDEA 常用快捷键,maven依赖图,个性化设置,禁用Search Everywhere

    查看idea 中jar关系图 快捷键: Ctrl+/ 用于注释,取消注释 Ctrl+Shift+F 全文搜索 Ctrl+F 单页面查找 Ctrl+Alt+Shift+L  格式化代码 ======== ...

  3. LA 6891 Money Transfers(最短路)

    https://vjudge.net/problem/UVALive-6891 题意: 给定一个加权无向图,还有起点和终点,现在有个SWERC公司,拥有图中的m个顶点,现在可以使图中的每一条边都加上k ...

  4. [WCF安全2]使用wsHttpBinding构建UserName授权的WCF应用程序,非SSL

    上一篇文章中介绍了如何使用basicHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建非SSL的UserName安全授权的WCF应用程 ...

  5. 爬虫模拟登陆之formdata表单数据

    首先HTTP协议是个无连接的协议,浏览器和服务器之间是以循环往复的请求回复来交互的,交互的形式是以文件形式来进行的.比如在chrome开发者工具network中看到了 每一行是一个文件,又文件大小啊, ...

  6. Spring IOC 源码简单分析 02 - Bean Reference

    ### 准备 ## 目标 了解 bean reference 装配的流程 ##测试代码 gordon.study.spring.ioc.IOC02_BeanReference.java   ioc02 ...

  7. vue双向数据绑定最最最最最简单直观的例子

    vue双向数据绑定最最最最最简单直观的例子 一.总结 一句话总结:双向绑定既不仅model可以影响view的数据,view也可以影响model的数据 view model 数据 1.vue双向数据绑定 ...

  8. js 文件系统API操作示例

    最近有个需求是:自动抓取某网站登录页面的验证码图片并保存,抓取n次.使用chrome插件来实现,其中使用到了js操作文件系统的api,特将代码记录下来,以备查阅. PS:第一次使用js文件系统的api ...

  9. Vue跨路由触发事件,Vue监听sessionStorage

    近来,在做公司的聊天系统,引用的是极光的api.项目需求实时监听别人发过来的消息,进行渲染到页面,还有历史记录也要渲染,历史记录和实时聊天记录返回的结构体还不一样,看到需求的我欲哭无泪,首先登录是在首 ...

  10. 310. Minimum Height Trees -- 找出无向图中以哪些节点为根,树的深度最小

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...