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的更多相关文章
- Ninject Lazy Load问题
参考: http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject 方案一: public class Module : ...
- 在 MVC 中使用 ninject Lazy Load的一个想法
这这里先声明一下,引用了一个 (http://www.edcourtenay.co.uk/musings-of-an-idiot/2012/11/23/lazy-binding-with-ninjec ...
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- jQuery延迟加载插件(Lazy Load)详解
最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...
- 延迟加载图片的 jQuery 插件:Lazy Load
网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...
- Lazy Load 图片延迟加载(转)
jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...
- jQuery Lazy Load 图片延迟加载
基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...
- about hibernate lazy load and solution
about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...
- Lazy Load, 延迟加载图片的 jQuery 插件 - NeoEase
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
随机推荐
- 虚拟机VirtualBox及轻量级的CentOS
1,先下载虚拟机VirtualBox和centos(下边有链接),将VirtualBox安装在本机 2,管理 --> 导入虚拟电脑 --> 选择本地centos文件 3,点击下一步 - ...
- shiro的三大功能
1.提供的功能
- hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0
Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...
- SSM整合AOP,日志框架和拦截器
前言 日志是所有系统必不可少的部分,而AOP在MVC通常用于监控方法调用,可以生成一个traceid,记录从用户调用到底层数据库的数据链路,帮助监控和排查问题. AOP 现在做一个简单的前置切面,用来 ...
- Python中的数据结构 --- 列表(list)
列表(list)是Python中最基本的.最常用的数据结构(相当于C语言中的数组,与C语言不同的是:列表可以存储任意数据类型的数据). 列表中的每一个元素分配一个索引号,且索引的下标是从0开始. ...
- JSP和JS的区别
从本科毕业设计开始就一直困扰我,jsp和js这两者的区别,一直处于迷糊状态,也没有搞清楚.今天就简单的介绍下两者的区别. 1.JSP全称是java server page JS全称是javaSc ...
- jQuery 滑动选项卡jQuery tabslet
Tabslet Yet another jQuery plugin for tabs, lightweight, easy to use and with some extra features ...
- HDU - 1142:A Walk Through the Forest (拓扑排序)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- SpringMVC和Freemarker整合,带自定义标签的使用方法
SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342 ...
- System.IO.Pipelines来对消息进行Buffer合并
System.IO.Pipelines来对消息进行Buffer合并 https://www.cnblogs.com/smark/p/9927455.html .net core使用Pipelines进 ...