ninject

With.Parameters.ConstructorArgument with ninject 2.0

℡╲_俬逩灬. 提交于 2019-11-27 00:42:20
问题 How to use this functionality in ninject 2.0? MyType obj = kernel.Get<MyType>(With.Parameters.ConstructorArgument("foo","bar")); The "With" isn't there :( 回答1: [Fact] public void CtorArgTestResolveAtGet() { IKernel kernel = new StandardKernel(); kernel.Bind<IWarrior>().To<Samurai>(); var warrior = kernel .Get<IWarrior>( new ConstructorArgument( "weapon", new Sword() ) ); Assert.IsType<Sword>( warrior.Weapon ); } [Fact] public void CtorArgTestResolveAtBind() { IKernel kernel = new

Configuring Ninject with Asp.Net MVC & Web Api

杀马特。学长 韩版系。学妹 提交于 2019-11-27 00:35:12
问题 i have setup my project with Ninject IoC . My project has regular Asp.Net MVC controllers and Web Api controllers. Now, Ninject works with Web Api but Ninject doesn't work with regular Asp.MVC controllers. My regular MVC controller implementation; public class GalleryController : BaseController { public GalleryController(IUow uow) { Uow = uow; } ........ } Error when using with regular controller An error occurred when trying to create a controller of type 'Web.Controllers.HomeController'.

Correct use of the NHibernate Unit Of Work pattern and Ninject

江枫思渺然 提交于 2019-11-27 00:24:50
问题 I have the following implementation and would like some feedback as to whether it makes correct use of NHibernate for sessions and transactions. public interface IUnitOfWork : IDisposable { ISession CurrentSession { get; } void Commit(); void Rollback(); } public class UnitOfWork : IUnitOfWork { private readonly ISessionFactory _sessionFactory; private readonly ITransaction _transaction; public UnitOfWork(ISessionFactory sessionFactory) { _sessionFactory = sessionFactory; CurrentSession =

MVC5, Web API 2 and Ninject

若如初见. 提交于 2019-11-26 23:58:47
I have created a new MVC5 project with Web API 2, I then added the Ninject.MVC3 package from NuGet. Constructor injection is working fine for the MVC5 controllers, but i am getting an error when trying to use it with the Web API Controllers. An error occurred when trying to create a controller of type 'UserProfileController'. Make sure that the controller has a parameterless public constructor. Constructor for working MVC5 controller: public class HomeController : Controller { private IMailService _mail; private IRepository _repo; public HomeController(IMailService mail, IRepository repo) {

Looking for a Ninject scope that behaves like InRequestScope

孤人 提交于 2019-11-26 23:16:07
On my service layer I have injected an UnitOfWork and 2 repositories in the constructor. The Unit of Work and repository have an instance of a DbContext I want to share between the two of them. How can I do that with Ninject ? Which scope should be considered ? I am not in a web application so I can't use InRequestScope . I try to do something similar... and I am using DI however, I need my UoW to be Dispose d and created like this. using (IUnitOfWork uow = new UnitOfWorkFactory.Create()) { _testARepository.Insert(a); _testBRepository.Insert(b); uow.SaveChanges(); } EDIT: I just want to be

how to inject quartz's job with ninject?

北城以北 提交于 2019-11-26 23:04:52
问题 I use ninject and quartz.net in my application and I want to inject job with ninject,But I do not know how to ,because all I know is that jobdetail is created by class of Jobimpl instead of an instance,such as: JobBuilder.Create<SomeJob>() Does anyone know how? 回答1: You'll have to implement an Quartz.Spi.IJobFactory - which uses an IResolutionRoot to create the job (see below for implementation). Then configure the scheduler to use it: Quartz.IScheduler.JobFactory = kernel.Get

How to use Ninject in a Windows Forms application?

五迷三道 提交于 2019-11-26 23:02:00
问题 I have an WinForms application with this Main Form : ICountRepository countRepository; public MainForm(ICountRepository countRepository) { this.countRepository = countRepository; } public void IncrementCount() { countRepository.IncrementCount(); } but i am struggling to inject ICountRepository into the mainform. How do I do that ? 回答1: Well the first steps are to switch from: var form = new MainForm(); Application.Run(form); to: var kernel = new StandardKernel( new

Continued Ninject support in ASP.NET MVC 6?

落爺英雄遲暮 提交于 2019-11-26 22:57:41
I have been very happily using Ninject for a long time now, and I really like it, but I am faced with a difficult choice since the release of ASP.NET 5 and MVC 6 . Basically, out of the gate, Microsoft has revealed their own dependency injection system; Which is one that to my knowledge has gotten a lot of criticism. But my bigger problem lies with how it affects other libraries. From another question I asked and other resources online , it seems that Ninject does not work out of the box with MVC 6. Though there is a "solution" given in the form of a verbose library Microsoft.Framework

How to use DI container when OwinStartup

让人想犯罪 __ 提交于 2019-11-26 22:20:37
问题 It's a Web API 2 project. When I implement DI using Ninject, I got an error message An error occurred when trying to create a controller of type 'TokenController'. Make sure that the controller has a parameterless public constructor. [assembly: OwinStartup(typeof(Web.Startup))] namespace Web { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); ConfigureWebApi(app); } } } public class TokenController : ApiController { private IUserService

Can a Ninject binding be based on a URL/route value?

懵懂的女人 提交于 2019-11-26 21:41:51
问题 I have a single controller that I want to use for CRUD operations on two different entities which implement the same interface. I'd like for Ninject to give it a different repository based on a query string value in the URL (or maybe a different URL, routed to the same controller). Is this possible? How can I do it? 回答1: That's usually a design smell but you could define the binding like this: kernel.Bind<IRepo>().ToMethod(ctx => { var a = HttpContext.Current.Request["a"]; if (a == "b") {