ninject

Ninject Bind When Ancestor Of Type T

江枫思渺然 提交于 2019-12-05 15:37:42
问题 I've got a dependency chain that looks roughly like this: public class CarSalesBatchJob { public CarSalesBatchJob(IFileProvider fileProvider) { ... } } public class MotorcycleSalesBatchJob { public MotorcycleSalesBatchJob(IFileProvider fileProvider) { ... } } public class FtpFileProvider : IFileProvider { public FtpFileProvider(IFtpSettings settings) { ... } } public class CarSalesFtpSettings : IFtpSettings { ... } public class MotorcycleSalesFtpSettings : IFtpSettings { ... } Up until now, I

How to inject an object into a WCF validator class

99封情书 提交于 2019-12-05 15:16:36
问题 Following up on using dependency injection for WCF services, is there any way of using DI for WCF validators , so that one could do this: public class DIValidator : UserNamePasswordValidator { private readonly IService service; [Inject] public DIValidator(IService service) { this.service = service; } public override void Validate(string userName, string password) { service.Login(userName, password); } } EDIT - I tried to apply Dzmitry's advice to my custom behaviour extension, since my

How do I Inject Dependencies with Ninject, where instances are deserialised from json

本秂侑毒 提交于 2019-12-05 14:47:09
This is my first try using DI, I've chosen ninject for it's reportedly easy learning curve, and have this question. I'm creating objects like this: var registrants = JsonConvert.DeserializeObject<List<Registrant>>(input); I currently have this constructor for Registrant [Inject] public Registrant(IMemberRepository memberRepository) { _memberRepository = memberRepository; } What is the best way to have the repository dependency be injected into the deserialized object(s) using Ninject? You can't use constructor injection with objects that are not created by Ninject (e.g. deserialized objects).

Ninject conditional binding

廉价感情. 提交于 2019-12-05 12:45:52
I'm playing around with Ninject for a simple test-bed project at home, just to see what I can do with it. As a starting point I'm building a console runner for some service, which accepts a variety of arguments and based on what it gets in, uses the same methods provided for a fluent interface to configure a model to run. As an example, suppose I have a verbosity switch, /o . /o can be passed as /o:quiet , /o:normal , or /o:verbose . The various options are self-explanatory. To satisfy this argument I would like to attach various implementations of ILogger - quiet gets a quiet logger that

How to set up an optional method interception with Ninject?

安稳与你 提交于 2019-12-05 12:25:58
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 second way either, since I don't see how to disable (or, rather, not to enable) the interception, as

Register Generic Type in Unity Based On Concrete Type

泪湿孤枕 提交于 2019-12-05 11:54:33
I'm trying to emulate a behavior that I can configure in Ninject, only using Unity instead. I am attempting to use the Cached Repository Pattern, given the following classes and interface: public interface IRepository<T> { T Get(); } public class SqlRepository<T> : IRepository<T> where T : new() { public T Get() { Console.WriteLine("Getting object of type '{0}'!", typeof(T).Name); return new T(); } } public class CachedRepository<T> : IRepository<T> where T : class { private readonly IRepository<T> repository; public CachedRepository(IRepository<T> repository) { this.repository = repository; }

Getting SNAP(AOP), NInject and ASP.Net MVC 3 working together

荒凉一梦 提交于 2019-12-05 10:35:51
问题 Has anyone got the SNAP AOP framework working with MVC 3 and Ninject. The samples given when adding Snap using NuGet to an MVC 3 project don't specifcally work well with a previously added NInject package. I have tried to get it working based on the normal NInject approach but just cannot get it to actually intercept! Can anyone show how to do this in code please? 回答1: I figured it out with the latest version of Ninject through NuGet which now adds a class call NinjectMVC3 in a new AppStart

Ninject with a base controller?

血红的双手。 提交于 2019-12-05 10:08:54
I am wondering how do you do constructor inject with ninject 2.0 when you have a base controller? I have private readonly IBaseService baseService; public BaseController(IBaseService baseService) { this.baseService = baseService; } Bind<IBaseService>().To<BaseService>(); public class OtherController : BaseController { private readonly IOtherService otherService; public OtherController(IOtherService otherService, IBaseService baseService) { this.otherService = otherService; } Yet I get 'BaseController' does not contain a constructor that takes 0 arguments You need to inject both services into

Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0' or one of its dependencies

偶尔善良 提交于 2019-12-05 09:44:23
问题 I am adding Ninject in MVC project using the following commands in Package Manager Console: Install-Package Ninject -version 3.0.1.10 Install-Package Ninject.Web.Common -version 3.0.0.7 Install-Package Ninject.MVC3 -Version 3.0.0.6 When I run the application, I get error like this: Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly

NamedScopes Ninject bindings and async (threading)

我的未来我决定 提交于 2019-12-05 09:29:12
My project is structured by services and repositories (all repositories share the db context). In one of my service layers, I have an asynchronous method that writes to the database using a repository. The web request will finish and dispose of the context before this method can get to use it. I tried to understand NamedScopes as stated in this answer . I still can't seem to understand how to implement it. I'll show how my project is structured and hope someone can help me at the code level. Bindings private static void RegisterServices(IKernel kernel) { //dbcontext kernel.Bind