Ninject Lazy Load问题
参考:
 http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject
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问题的更多相关文章
- 在 MVC 中使用  ninject Lazy Load的一个想法
		这这里先声明一下,引用了一个 (http://www.edcourtenay.co.uk/musings-of-an-idiot/2012/11/23/lazy-binding-with-ninjec ... 
- Ninject Lazy Load
		namespace LayzyLoadTest { [TestClass] public class UnitTest1 { private IKernel InitKernel() { 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 ... 
随机推荐
- A8逻辑篇1.点亮一个LED(S5PV210.A8)
			一.虚拟机安装好后,我们用Fedora 双击.vmx文件,将会在虚拟机中打开 相应的生成: 这些文件 二.进入虚拟机页面 等待启动 账号选择其他 用户名:root 密码:111111 设置页面大小: ... 
- MyBatis使用小案例
			首先回顾一下MyBatis封装简化Dao层连接数据库操作的顺序. 首先MyBatis是一个引入的jar包,还有一些依赖包,可能用不到的jar包,一并引入就好了,再多引入一个Juntil.jar测试包( ... 
- codeforces 9 div2 C.Hexadecimal's Numbers 暴力打表
			C. Hexadecimal's Numbers time limit per test 1 second memory limit per test 64 megabytes input stand ... 
- 用JavaScript做一个小小设计
			这个项目是我无聊时完成的,参阅过很多大神的示例,其实方法并不难主要是js和css样式的设计,我发现自己还有很多的js代码写不出来更加不用提看的明白了,(PS吐槽一下:革命尚未成功,同志还需努力啊!)此 ... 
- Java Spring-Bean中属性注入
			2017-11-06 20:29:13 类属性的注入的三种方法 1.接口方法注入 public interface injection{ public void setName(String name ... 
- OOP的感悟
			不要认为你关心的东西就是对象的全部或对象的核心,相对于对象的成员家族而言,它仅仅是其中的一个‘很小的成员而已’ 
- docker的搭建和简单应用
			dockerserver端安装 先下载docker的yum源 wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo ... 
- Vivado_MicroBlaze_问题及解决方法_汇总(不定时更新)
			Vivado_MicroBlaze_问题及解决方法_汇总(不定时更新) 标签: Vivado 2015-07-03 14:35 4453人阅读 评论(0) 收藏 举报 分类: 硬件(14) 版权声 ... 
- rxjava 调用retrofit执行网络请求的过程
			retrofit流程图 -1.RxJava调用Retrofit,从requestGtPushSaeUserInfo()中获得被观察者observable,然后new一个观察者向它订阅 0.从业务中 ... 
- React Native自适应设备宽度解决方案
			px:设备实际像素单位 dp/pt:逻辑像素单位(IOS的尺寸单位为pt,Android的尺寸单位为dp) 在设计和开发过程中,应该尽量使用逻辑像素尺寸来思考界面. UI 给默认 640 的图,采用 ... 
 
			
		