Tag: ninject 2

拦截Ninject实例激活?

我试图在WP7和Ninject上使用Caliburn Micro。 一切都很直接。 但是,一旦实例被Ninject激活,我就会被困在如何解决事件。 这是Caliburn Micro的SimpleContainer中的ActivateInstance方法,它是手机CM附带的IoC容器。 protected virtual object ActivateInstance(Type type, object[] args) { var instance = args.Length > 0 ? Activator.CreateInstance(type, args) : Activator.CreateInstance(type); Activated(instance); return instance; } 我在Ninject中注册我的类型,当它们被激活时,我需要激活Activated事件。 我看了拦截可能是要走的路,但我不认为动态代理和林福将在手机上工作。 为了澄清更多,我没有使用SimpleContainer,上面是显示SimpleContainer在激活实例时所做的事情。 我有一个NinjectBootstrapper和一个实现IPhoneContainer的NinjectContainer。 我无法弄清楚如何实现event Action Activated; 与Ninject。 更新: .OnActivation()看起来像票。 Kernel.Bind().To().InSingletonScope().OnActivation();

同时使用InRequestScope和InTransientScope将Ninject解析为相同类型

我有一个Ninject设置,创建一个JobContext解析器InRequestScope()这很好,但是,我在网站上有一个非常具体的调用,要求我循环几个数据库(数据库中的所有数据按年)。 我无法弄清楚发生了什么,因为我忘记了JobContext是InRequestScope但最后一段代码并没有按照我的想法行事。 这是设置 //Ninject module Bind().To().InRequestScope(); //Controller’s Initialize protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); //set a connection string for the jobContext this.jobContext = DependencyResolver.Current.GetService(); jobContext.SetYear(currentYear); } 由于JobContext在请求范围内,因此每年都会重复使用相同的对象。 这是我需要InTransientScope而不是InRequestScope的唯一实例。 //Special function foreach (int year in ActiveYears) { jobContext = DependencyResolver.Current.GetService(); jobContext.SetYear(year); DoSomething(); } 我怎么能做到这一点?

获取ninject工厂扩展以允许将工厂参数传递给依赖项

使用Ninject Factory扩展,您可以自动生成工厂,并让工厂将参数传递给类的构造函数。 以下测试通过: public interface IBar { int Foo { get; } } public class Bar : IBar { int _foo; public Bar(int foo) { _foo = foo; } public int Foo { get { return _foo; } } } public interface IBarFactory { IBar CreateTest(int foo); } [Test] public void ExecuteTest() { var kernel […]

Ninject WithConstructorArgument:没有匹配的绑定可用,并且该类型不可自绑定

我对WithConstructorArgument的理解可能是错误的,因为以下内容不起作用: 我有一个服务,让我们称之为MyService,其构造函数采用多个对象,以及一个名为testEmail的字符串参数。 对于此字符串参数,我添加了以下Ninject绑定: string testEmail = “test@example.com”; kernel.Bind().To().WithConstructorArgument(“testEmail”, testEmail); 但是,在执行以下代码行时,我得到一个exception: var myService = kernel.Get(); 这是我得到的例外: 激活字符串时出错没有匹配的绑定可用,并且该类型不可自我绑定。 激活路径: 2)将依赖字符串注入到MyService类型的构造函数的参数testEmail中 1)请求MyService 建议: 1)确保已为字符串定义了绑定。 2)如果在模块中定义了绑定,请确保已将模块加载到内核中。 3)确保您没有意外创建多个内核。 4)如果使用构造函数参数,请确保参数名称与构造函数参数名称匹配。 5)如果使用自动模块加载,请确保搜索路径和filter正确无误。 我在这做错了什么? 更新 : 这是MyService构造函数: [Ninject.Inject] public MyService(IMyRepository myRepository, IMyEventService myEventService, IUnitOfWork unitOfWork, ILoggingService log, IEmailService emailService, IConfigurationManager config, HttpContextBase httpContext, string testEmail) { this.myRepository = myRepository; this.myEventService = myEventService; this.unitOfWork […]

Ninject:如何使用多个类型参数绑定开放generics?

我正在使用Ninject 2.2,我正在尝试为一个带有两个类型参数的开放generics设置绑定。 根据qes的这个答案 ,将IRepository绑定到Repository的正确语法是这样的: Bind(typeof(IRepository)).To(typeof(Repository)); 如果IRepository仅使用一个类型参数,则上述语法可以正常工作,但如果需要更多,则会中断(给出Using the generic type ‘Repository’ requires 2 type arguments编译时错误。) 如何将IRepository绑定到Repository ? 谢谢。

“注入”属性不适用于字段

Inject属性不适用于字段。 [Inject] public MyContext context; //Not injected [Inject] public MyContext context {get; set;} //Injected 我正在使用默认的Ninject设置。 为什么不注入田地?

调试代码时Ninject没有源可用错误

我使用NuGet来安装最新版本的Ninject(v2.2.1.4)。 然后我创建了我自己的NinjectDependencyResolver(归功于Adam Freeman和Steve Sanderson ): public class NinjectDependencyResolver : IDependencyResolver { private IKernel kernel; public NinjectDependencyResolver() { kernel = new StandardKernel(); AddBindings(); } public object GetService(Type serviceType) { return kernel.TryGet(serviceType); } public IEnumerable GetServices(Type serviceType) { return kernel.GetAll(serviceType); } public IBindingToSyntax Bind() { return kernel.Bind(); } public IKernel Kernel { get { return kernel; } […]

Ninject入门

我在dimecasts.net上观看了Ninject的前2个初学者教程。 现在,我想在ASP.NET MVC 3中使用Ninject 2.2。我想要一个带有模拟模型的视图。 在调用我的服务时,我没有将对象引用设置为对象的实例; public class HomeController : Controller { private readonly IMilestoneService _service; public HomeController() { } HomeController(IMilestoneService service) { _service = service; } public ActionResult Index() { ViewBag.Message = “Change Request System”; return View(); } public ActionResult About() { return View(); } #region Partial views public ActionResult Milestone() { var result […]

ASP.NET MVC 3:具有inheritance/多态的DefaultModelBinder

首先,对于这个大post(我先尝试做一些研究)和同一问题的混合技术(ASP.NET MVC 3,Ninject和MvcContrib)感到抱歉。 我正在使用ASP.NET MVC 3开发一个项目来处理一些客户端订单。 简而言之:我有一些inheritance自的对象和抽象类Order ,当我向控制器发出POST请求时,我需要解析它们。 我该如何解决正确的类型? 我是否需要覆盖DefaultModelBinder类,还是有其他方法可以执行此操作? 有人可以提供一些代码或其他链接,如何做到这一点? 任何帮助都会很棒! 如果post令人困惑,我可以做任何改变,以明确! 所以,对于我需要处理的订单,我有以下inheritance树: public abstract partial class Order { public Int32 OrderTypeId {get; set; } /* rest of the implementation ommited */ } public class OrderBottling : Order { /* implementation ommited */ } public class OrderFinishing : Order { /* implementation ommited */ […]