ninject

How to configure Ninject for MVC4 & custom Membership provide?

那年仲夏 提交于 2019-11-28 05:57:00
问题 According to this article description custom-membership-provider-with-repository-injection I implement the custom Membership provide with inject. Custom Membership provider using Ninject; public class CustomMembershipProvider : MembershipProvider { [Inject] public IUserRepository UserRepository { get; set; } [...] Custom Role Provider using Ninject; public class CustomRoleProvider : RoleProvider { [Inject] public IUserRoleRepository UserRoleRepository { get; set; } [...] within Web.Config

Ninject and MVC3: Dependency injection to action filters

吃可爱长大的小学妹 提交于 2019-11-28 05:01:44
I've found loads of inconclusive articles and questions on how to do property injection on an ActionFilter in ASP.NET MVC3 using Ninject. Could someone give me a clear example please? Here's my custom auth attribute. public class CustomAuthorizeAttribute : AuthorizeAttribute { [Inject] public IService Service { get; set; } [Inject] public IAuthenticationHelper AuthenticationHelper { get; set; } public override void OnAuthorization(AuthorizationContext filterContext) { //My custom code } } I am using the WebActivator to set up Ninject [assembly: WebActivator.PreApplicationStartMethod(typeof

Error Handling in asp.net mvc 3

佐手、 提交于 2019-11-28 04:54:42
Is there a built in or a proper way to handle errors in asp.net mvc 3? This is what I want to do: If the application crashes, or throws an error, it goes to a specific error page. I can throw my own error from the controller action. (and it goes to an error page). I found the following ways: I see there is a long way to do it here . (for v1 and v2 but also applies to v3). Using errorhandle attribute here . How do I handle this the proper way? If the solution is similar or is like #1 in the list above, I am using ninject and I have not created a base class. How do I still do this? For Global

With.Parameters.ConstructorArgument with ninject 2.0

北慕城南 提交于 2019-11-28 04:54:06
How to use this functionality in ninject 2.0? MyType obj = kernel.Get<MyType>(With.Parameters.ConstructorArgument("foo","bar")); The "With" isn't there :( Ian Davis [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 StandardKernel(); kernel.Bind<IWarrior>().To<Samurai>() .WithConstructorArgument("weapon", new Sword() ); var

Correct use of the NHibernate Unit Of Work pattern and Ninject

纵然是瞬间 提交于 2019-11-28 04:25:11
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 = _sessionFactory.OpenSession(); _transaction = CurrentSession.BeginTransaction(); } public ISession

Using Ninject in a plugin like architecture

两盒软妹~` 提交于 2019-11-28 02:57:54
I'm learning DI, and made my first project recently. In this project I've implement the repository pattern. I have the interfaces and the concrete implementations. I wonder if is possible to build the implementation of my interfaces as "plugins", dlls that my program will load dynamically. So the program could be improved over time without having to rebuild it, you just place the dll on the "plugins" folder, change settings and voilá! Is this possible? Can Ninject help with this? ungood While Sean Chambers' solution works in the case that you control the plugins, it does not work in the case

Is binding ToConstant and calling InSingletonScope redundant?

二次信任 提交于 2019-11-28 02:29:14
问题 Well, this question is pretty simply stated by the title. For a local variable factory : var factory = Fluently.Configure() ... Are these two lines equivalent: Bind<ISessionFactory>().ToConstant(factory).InSingletonScope(); and: Bind<ISessionFactory>().ToConstant(factory); 回答1: In the latest version of ninject, when you create a ToConstant binding it will automatically set the Scope to Singleton. Thus, the InSingletonScope() part in your example is redundant. From ninject code base: ///

HttpHandler Property Injection using Ninject returning null

允我心安 提交于 2019-11-28 02:01:41
问题 I have the following httphandler: public class NewHandler : IHttpHandler { [Inject] public IFile FileReader { get; set; } public NewHandler() { } public void ProcessRequest(System.Web.HttpContext context) { .... var something = SomeMethod(FileReader); .... } public bool IsReusable { get { return true; } } } This is my Ninject Module in the Global.asax. internal class ServiceModule : NinjectModule { public override void Load() { Bind<IFile>().To<FileWrapper>().InSingletonScope(); } } Every

Autofac: any way to resolve the innermost scope?

微笑、不失礼 提交于 2019-11-28 01:32:03
问题 I'm currently trying out Autofac in a new ASP.NET MVC project after having used Ninject, Castle Windsor and other IoC containers in the last years. So while I know about IoC containers in general, I'm fairly new to Autofac and I'm still looking for some best practices. Currently I'm trying to find out if there is a way to resolve the innermost nested scope. I have the following situation: a component that is registered as SingleInstance() has a method that creates a nested lifetime scope,

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

江枫思渺然 提交于 2019-11-28 00:23:37
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? 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") { return new RepoA(); } return new RepoB(); }).InRequestScope(); The following worked for me, Getting A Specific