ninject

Ninject : Constructor parameter

独自空忆成欢 提交于 2019-11-30 09:54:31
I am using Ninject together with ASP.NET MVC 4. I am using repositories and want to do constructor injection to pass in the repository to one of the controllers. This is my Repository interface: public interface IRepository<T> where T : TableServiceEntity { void Add(T item); void Delete(T item); void Update(T item); IEnumerable<T> Find(params Specification<T>[] specifications); IEnumerable<T> RetrieveAll(); void SaveChanges(); } The AzureTableStorageRepository below is an implementation of IRepository<T> : public class AzureTableRepository<T> : IRepository<T> where T : TableServiceEntity {

Share a Kernel between WebAPI and MVC

為{幸葍}努か 提交于 2019-11-30 08:50:30
问题 I've got a simple MVC4 site which uses ASP.NET webAPI and also MVC pages. I'd like to use Ninject DI for both controller types but I'm wondering how this can be done? I've got Ninject DI working for WebAPI, but now not sure how to share the same Kernel elegantly. Should I be using some sort of Kernel singleton which both Dependency Resolvers can refer to? Anyone had any experience with this? Cheers. 回答1: You should use the same IKernel instance for a single application-level composition root,

Ninject: Bind Constructor Argument to Property of Other Object

可紊 提交于 2019-11-30 08:31:53
I have an IConfig object that contains settings used throughout my application. At the moment, I inject the entire object into the constructor of each object that needs it, as follows: public interface IConfig { string Username { get; } string Password { get; } //... other settings } public class Foo : IFoo { private readonly string username; private readonly string password; public Foo(IConfig config) { this.username = config.Username; this.password = config.Password; } } The downside is that IConfig contains a large number of settings because it's deserialised from an overall config file, so

What's the difference between .ToConstructor and .ToMethod in Ninject 3?

耗尽温柔 提交于 2019-11-30 07:54:21
In Ninject3 there's a new .ToConstructor feature . As described, it helps to strongly-type constructor arguments like: Bind<IMyService>().ToConstructor( ctorArg => new MyService(ctorArg.Inject<IFoo>(), ctorArg.Inject<IBar>())); What's actually the difference between using .ToConstructor and .ToMethod in an almost the same way: Bind<IMyService>().ToMethod( x => new MyService(x.Kernel.Get<IFoo>(), x.Kernel.Get<IBar>())); Is it just a syntax sugar to avoid using Kernel.Get<>() or is there something more that I'm missing? The first case behaves like To<MyService>() except that you explicitly

Ninject Binding

做~自己de王妃 提交于 2019-11-30 07:43:54
How do bind my interface to a concrete class in a different assembly? I have the following projects in my solution: Foo.Data Foo.Domain In Structure Map I add my two assembly names to the StructureMap.config file and a then using the PluginFamily and Pluggable attributes map my interfaces to my concrete class'. How can accomplish the same thing with Ninject? I'll make a couple of assumptions here. You have an interface named IBar in your Foo.Domain project and you have a concrete class called BarClass in your Foo.Data project. You in fact reference Foo.Domain project in your Foo.Data project

Dependency Injection with Ninject, MVC 3 and using the Service Locator Pattern

瘦欲@ 提交于 2019-11-30 06:49:35
Something that has been bugging me since I read an answer on another stackoverflow question (the precise one eludes me now) where a user stated something like " If you're calling the Service Locator, you're doing it wrong. " It was someone with a high reputation (in the hundred thousands, I think) so I tend to think this person might know what they're talking about. I've been using DI for my projects since I first started learning about it and how well it relates to Unit Testing and what not. It's something I'm fairly comfortable with now and I think I know what I'm doing. However, there are a

ASP.NET MVC, Ninject, single instance per request for multiple constructors

不想你离开。 提交于 2019-11-30 06:26:09
Im trying to implement an unit of work pattern by passing an unit of work instance into my repositories. Relevant code from Global.asax. public class SiteModule : NinjectModule { public override void Load() { Bind<IUnitOfWork>().To<SqlUnitOfWork>() .InRequestScope() .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["Entities"].ConnectionString); Bind<IProductRepository>().To<ProductRepository>(); Bind<ICategoryRepository>().To<CategoryRepository>(); } } Repository constructors: public class ProductRepository { IUnitOfWork unitOfWork; public ProductRepository

Custom Authorization MVC 3 and Ninject IoC

纵饮孤独 提交于 2019-11-30 05:50:46
问题 I have a custom authorization class that inherits from FilterAttribute and implements IAuthorizationFilter. I am using the latest version of Ninject w/ asp.net MVC 3 support. The problem I have is I am using constructor injection to inject a repository. But by the the time OnAuthorization is called, the repository is null. Here is the code... public class MyAuthorizeAttribute : FilterAttribute, IAuthorizationFilter { private readonly IMyRepo _MyRepo; public MyAuthorizeAttribute() { } public

IoC, AOP and more

做~自己de王妃 提交于 2019-11-30 05:03:59
What is an IoC container? What is an IoC/DI framework? Why do we need a framework for IoC/DI? Is there any relationship between IoC/DI and AOP? What is Spring.net/ninject with respect to IoC and AOP? JMSA, James Kovacs wrote a fantastic article which covers many of your questions I would recommend reading it Here Spring.Net, Ninject, Unity, Castle Windsor, Autofac are all IOC containers which are configurable in different ways, many of them do also support AOP. Frameworks for IOC/DI are useful because they provide standard mechanisms, for example if you are hiring a new developer it is much

Best location for Fluent IOC configuration/Modules (Currently trying Ninject)

爱⌒轻易说出口 提交于 2019-11-30 04:52:33
问题 I am struggling to find the best place to locate my Ninject configuration "Modules" (the place where Type bindings are specified). I hope I am just missing some obvious trick, as this is starting to turn into a deal-breaker for me with using fluent configuration (and thus Ninject): In a simple Web stack containing three separate projects: Web, BusinessLogic, DataAccess. I do not want the Web tier to have to directly reference the DataAccess tier , but I can't see a way around this because: If