Ninject Lazy Load问题
参考:
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 ...
随机推荐
- python排序算法实现(冒泡、选择、插入)
python排序算法实现(冒泡.选择.插入) python 从小到大排序 1.冒泡排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)) ...
- 采用注解方式实现security
采用注解方式使用security,首先我们需要用注解方式实现Spring MVC,新建一个Maven项目 本项目目录结构如下: 我们会发现在WEB-INF中没有web.xml文件,下面会介绍,采用j ...
- [PyTorch]论文pytorch复现中遇到的BUG
目录 1. zip argument #1 must support iteration 2. torch.nn.DataParallel 3. model.state_dict() 1. zip a ...
- Linux配置NFS实现共享
(1)安装相应rpm包 sudo rpm -ivh nfs-utils-1.2.3-70.el6.x86_64.rpm (2)配置共享目录:sudo vim /etc/exports /app/sof ...
- Python内置函数(9)——callable--转载
英文文档: callable(object) Return True if the object argument appears callable, False if not. If this re ...
- VuePress从零开始搭建自己的博客
VuePress是什么? VuePress是以Vue驱动的静态网站生成器,是一个由Vue.Vue Router和webpack驱动的单页应用.在VuePress中,你可以使用Markdown编写文档, ...
- wpf中把按钮变成圆角
<Button x:Name="btn" Content="改变" Width="100" Height="50&quo ...
- php---------字符串转义函数(addslashes,stripslashes)
在PHP中,有两个函数与字符串的转义有关,他们分别是 addslashes 和 stripslashes. addslashes($string), 在指定的预定义字符前添加反斜杠 (\),用于为存储 ...
- Bitwise Equations
Problem Description You are given two positive integers X and K. Return the K-th smallest positive i ...
- Dlib——C++机器学习库,有传统机器学习的,也有深度学习的
Dlib的目标用户并没有Hyperopt-sklearn细分,它是一个基于C++语言的通用的机器学习和数据分析库.值得一提的是,虽然Dlib的确是由C++实现的,但它却提供了针对Python语言的AP ...
