ninject

Ninject in an Action Filter

半腔热情 提交于 2019-12-07 14:26:09
问题 I have created a custom Action Filter and am binding it with Ninject's BindFilter method: public class ExtendModelAttribute : FilterAttribute {} public class ExtendModelFilter : IActionFilter { private IKernel kernel; public ExtendModelFilter(Func<IKernel> kernel) { this.kernel = kernel; } public void OnActionExecuted(ActionExecutedContext filterContext) { // TODO: } public void OnActionExecuting(ActionExecutingContext filterContext) { } } I am binding my filter like so: kernel.BindFilter

How to use “Composite Design Pattern” with Ninject

感情迁移 提交于 2019-12-07 13:38:26
问题 Validation Rule Contract: public interface IValidationRule { bool IsValid(); } Concrete Validation Rule: public class MyClass : IValidationRule { public bool IsValid() { return true; } } Composite: public class ValidationRuleComposite : IValidationRule { private readonly IEnumerable<IValidationRule> _validationRules; public ValidationRuleComposite(IEnumerable<IValidationRule> validationRules) { _validationRules = validationRules; } public bool IsValid() { return _validationRules.All(x => x

When should one use Kernel.BeginBlock() in Ninject.MVC3

三世轮回 提交于 2019-12-07 12:30:02
问题 I'm using Ninject.MVC3 with WebAPI. Originally, I was using the implementation of NinjectResolver and NinjectScope as outlined here,i.e. using _kernel.BeginBlock() , I noticed that BeginBlock() gets invoked on each call to the Controller. On load testing the controller (over several hundred invocations) I noticed that the memory consumption of w3wp increased significantly (upwards of 1.4 gigs on high load) and the GC would never reclaim any memory. Per this SO post, the kernel should not be

Actionfilter Injection in ASP.NET MVC 5

落爺英雄遲暮 提交于 2019-12-07 09:00:59
问题 I have a simple filter. public class IsAdmin : ActionFilterAttribute, IAuthenticationFilter { private string _roleName; IBusinessIdentity _identity; public IsAdmin(string roleName, IBusinessIdentity identity) { this._roleName = roleName; this._identity = identity; } public void OnAuthentication(AuthenticationContext filterContext) { } public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext) { if (!_identity.Roles.Contains(_roleName)) filterContext.Result = new

How do I use the Ninject Conventions library to bind to a base type that isn't an interface?

…衆ロ難τιáo~ 提交于 2019-12-07 08:58:48
问题 I'm trying to scan for a set of components that implement a specific base class in the assemblies in the same directory as my application. I need to do this as a sort of plugin-style architecture, as my application uses these types to populate other components. Ninject.Extensions.Conventions supports scanning assemblies in the local directory, so I decided to give it a shot. The problem is that the binding generators that library provides ( DefaultBindingGenerator and RegexBindingGenerator )

AutoMapper with Ninject

廉价感情. 提交于 2019-12-07 08:47:32
问题 I've been trying to setup AutoMapper to instantiate all objects via Ninject. I've got the following code in my global.asax file Mapper.Configuration.ConstructServicesUsing(x => kernel.Get(x)); And as an example I have the following mapping Mapper.CreateMap<TestModel, IndexViewModel>(); However, this does not appear to be working. I get an error that 'IndexViewModel' does not have a default constructor. I can get the mapper to work by explicitly telling automapper to use ninject in the mapping

What is the right way to ensure your Repository and UnitOfWork class share the same nhibernate session object?

怎甘沉沦 提交于 2019-12-07 08:40:51
问题 I am having issues editing and deleting objects and i think its because i am not sharing the same session objects between my repository classes and my unitofwork class. I am trying to find some document on the best way to wire this up so I share the same session object. I am using ninject as my IOC container in the mvc website. 回答1: I usually set the session as a dependency of the repository, so Ninject can resolve the dependency (ISession = NHibernate.ISession): public UserRepository

Using IAuthorizationFilter with Ninject and EF gives DbContext has been disposed error

老子叫甜甜 提交于 2019-12-07 07:49:39
问题 I'm trying to use my UnitOfWork inside an implementation of IAuthorizationFilter , but after I navigate between a few pages I get this exception: System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed. FilterConfig.cs filters.Add(DependencyResolver.Current.GetService(typeof(PermissionFilter))); NinjectMappings.cs public class NinjectMappings : NinjectModule { public override void Load() { Bind<MyContext>().ToSelf().InRequestScope(); Bind

How to set up an optional method interception with Ninject?

萝らか妹 提交于 2019-12-07 06:11:01
问题 Suppose I have a class in which I want to sometimes* (but now always) intercept some (but not all) methods. The way I understand it, this can be done either with, say, InterceptAround() in my Ninject module (in the higher-level code), or with an InterceptAttribute-derived attribute on those methods (at the implementation level). I don't really like the first way of doing it, because it requires the consumer to know the details, there'll be many classes with many methods. But I don't like the

Convention based binding in ASP.NET 5 / MVC 6

▼魔方 西西 提交于 2019-12-07 03:10:15
问题 It is possible to register dependencies manually: services.AddTransient<IEmailService, EmailService>(); services.AddTransient<ISmsService, SmsService>(); When there are too much dependencies, it becomes difficult to register all dependencies manually. What is the best way to implement a convention based binding in MVC 6 (beta 7)? P.S. In previous projects I used Ninject with ninject.extensions.conventions . But I can't find a Ninject adapter for MVC 6. 回答1: No, there is no support for batch