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 ...
随机推荐
- SqlBulkCopy 批量导入数据 转换表字段类型
在使用SqlBulkCopy导入数据时,要有一个跟数据库里面同样的DataTable 要赋值表名 要求每个列跟数据库中列同名,并且列的类型要赋值跟数据库中列的类型对应的NET类型 要求数据库中为Nul ...
- 通过委托来实现异步 Delegate的BeginInvoke和EndInvoke
什么是.net的异步机制呢? 解释这个话题之前,先让我们来看看同步执行的程序 https://github.com/chucklu/Test/blob/master/DotNet4.5开发指南/并行处 ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table 很有想法的一个二分
D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input stand ...
- MongoDB中的数据聚合工具Aggregate和Group
周煦辰 2016-01-16 来说说MongoDB中的数据聚合工具. Aggregate是MongoDB提供的众多工具中的比较重要的一个,类似于SQL语句中的GROUP BY.聚合工具可以让开发人员直 ...
- PHP如何生成文章预览图
PHP如何生成文章预览图 一.总结 一句话总结:php的wkhtmltox扩展,php官方文档有怎么使用,或者github,或者百度,等等等等 wkhtmltox 1.PHP如何自动生成文章预览图? ...
- git-it 教程,一些git知识点。/ 如何解决merge conflict/ 如何使用Github Pages./Git术语表
一个git使用教程 https://:.com/jlord/git-it-electron#what-to-install 一个在线Github的功能教学:https://lab.github.com ...
- 矩阵快速幂——POJ3070
矩阵快速幂和普通的快速幂差不多,只不过写起来比较麻烦一点,需要重载*运算符. 模板: struct mat { int m[maxn][maxn]; }unit; mat operator * (ma ...
- 019PHP基础知识——函数(二)
<?php /** * 变量的作用范围 * 函数体内的变量只作用于函数体内. */ /*$bbs="bbs.blog.com"; function say(){ $bbs=& ...
- hrbust 1621 迷宫问题II 广搜
题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...
- SGU 138. Games of Chess 构造 难度:2
138. Games of Chess time limit per test: 0.25 sec. memory limit per test: 4096 KB N friends gathered ...
