ninject

Injecting an IEnumerable into a constructor with a Ninject factory method

China☆狼群 提交于 2019-12-05 03:56:47
I'm trying to inject an IEnumerable into a constructor with Ninject. My constructor looks like this: public MatrixViewModel(IEnumerable<FooViewModel> fooViewModels) { _fooViewModels = fooViewModels; } My Ninject module looks like this: public class MainModule : NinjectModule { public override void Load() { Bind<IEnumerable<FooViewModel>>() .ToMethod(context => GetFooViewModels()) .InSingletonScope(); // this binding is not working } private IEnumerable<FooViewModel> GetFooViewModels() { // returns a bunch of foo view models } } This doesn't seem to be working. I don't get any error. Ninject

Ninject property injection returns null

倖福魔咒の 提交于 2019-12-05 03:45:30
I've got a WinForms app with the following code: static void Main() { IKernel kernel = new StandardKernel(new MyModule()); TestInterface test = kernel.Get<TestInterface>(); } For the Module.Load() event: Bind<TestClass>().ToSelf().InSingletonScope(); Bind<TestInterface>().To<TestClass>(); At this point test in the Main() method is the proper object I'm expecting. In a form later on, I'm using property injection: [Inject] TestInterface test {get;set;} And once the form is loaded, trying to work with test , but it's a null object. Thoughts? Make sure you call Inject() on the IKernel instance and

How to configure Automapper to be injected with Ninject 2.0?

寵の児 提交于 2019-12-05 02:56:57
问题 There are configuration examples for Structure Map and Windsor: http://www.cprieto.com/index.php/2009/08/20/using-automapper-with-castle-windsor/ But I haven't found anything for Ninject. Do you know how to translate those mappings to Ninject? 回答1: public class AutoMapperModule : NinjectModule { public override void Load() { Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine); } } 回答2: It's really very easy, just load this module: public class AutoMapperModule : NinjectModule { public

How do I use Ninject with ActionResults while making the controller IoC-framework-agnostic?

送分小仙女□ 提交于 2019-12-05 02:46:27
问题 Nearly all of the Ninject examples I've seen explain how to use it with ASP.NET MVC, which will automatically inject dependencies into controllers. How would I use Ninject manually though? Let's say I have a custom ActionResult: public class JsonResult : ActionResult { [Inject] public ISerializer Serializer { get; set; } public JsonResult(object objectToSerialize) { // do something here } // more code that uses Serializer } Then in my controller, I'm using JsonResult in a method like this:

I am looking for a simple yet practical and robust IOC/DI framework for .net

天大地大妈咪最大 提交于 2019-12-05 02:28:09
问题 I am going to use it in a project with less-experienced developers so a complex framework such as Spring.NET is not an option. I was thinking about: Ninject Castle Windsor StructureMap Which would present a moderate learning curve without losing flexibility? and another question - where is the correct place to put the configuration? Since the kernel/configuration on a 3-tier ASP.NET application (not MVC!!! all examples are using MVC :) ) 回答1: The great thing about proper use of DI is that you

Constructor injection with other, non-dependency, constructor arguments

偶尔善良 提交于 2019-12-05 01:49:50
I'm new to IOC containers, and I'm getting started with NInject. What do you do if you want your constructor to have parameters that are not services and don't need to be instantiated by the IOC container? For example: public class Person { private readonly string _name; private readonly IPersonRepository _repository; public Person(string name, IPersonRepository repository) { _name = name; _repository = repository; } ...... } Imagine that name is a requirement of the Person class, so, to ensure that a Person always has a name, we require that it be passed in to the constructor. How would we

Assembly added via Add Reference not copied to output directory unless referred to in code

久未见 提交于 2019-12-04 23:48:11
Situation: Project 1 is an assembly in the solution Project 2 is an executable assembly project in the same solution Project 2 has a project reference (via Add Reference) to Project 1 Project 2 does not directly refer to namespaces/types in Project 1 in the code Project 2 uses Ninject to dynamically load Project 1 and use the implementation classes within it Problem: Even though Copy Local is set to True for the reference, and the referenced assembly does not exist in the GAC, the referenced assembly is not copied to the output build directory Ninject subsequently does not find the assembly,

Using Ninject IOC to replace a factory

為{幸葍}努か 提交于 2019-12-04 23:40:13
I've got a factory method inside a parser. Essentially as I load a token I look up the handler for that token, or drop through to the default handler. I've implemented this as a switch and as a Dictionary<string,Type> but both approaches require me to store the mapping somewhere else than the handler class. We are using Ninject for IOC and so I've realized I can also do it using kernel.Get<ITokenHandler>(tokenName); but that doesn't save me storing the information on what token the handler can deal with in 2 locations. Is there a way I can decorate the handler so this gets mapped automatically

How can Json.NET perform dependency injection during deserialization?

烂漫一生 提交于 2019-12-04 22:51:58
When I have a class with no default constructor, i.e. using dependency injection to pass its dependencies, can Newtonsoft.Json create such an object? For example: public class SomeFoo { private readonly IFooDependency _dependency; public SomeFoo(IFooDependency dependency){ if(dependency == null) throw new ArgumentNullException("dependency"); _dependency = dependency; } public string Data { get; set; } public int MoreData { get; set; } public void DoFoo(){ Data = _dependency.GetFooData(); MoreData = _dependency.GetMoreFooDate(); } } During serialization, I only care of storing Data and MoreData

Bind multiple implementations to the same interface with ninject

烂漫一生 提交于 2019-12-04 22:34:03
Why is it not possible for me to do the following in Ninect? Kernel.Bind<IPresenter>.To<DefaultPresenter>(); Kernel.Bind<IPresenter>.To<DashboardPresenter>(); Kernel.Bind<IPresenter>.To<HeartRatePresenter>(); Kernel.Bind<IPresenter>.To<GPSPresenter>(); Each of the 4 implementations have a different constructor that expect a different type. When i attempt this, Ninject throws an exception telling me that i cannot bind to the same interface more than once. In a class called Presentable which all presenter classes inherit from, I am attempting to do Kernel.Get<IPresenter>(new ConstructorArgument(