ninject

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

那年仲夏 提交于 2019-12-03 20:50:43
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 :) ) Mark Seemann The great thing about proper use of DI is that you can defer the decision about which DI Container to use until the last responsible moment . In

When to use Singleton vs Transient vs Request using Ninject and MongoDB

大兔子大兔子 提交于 2019-12-03 19:22:50
问题 I'm not quite sure when I should use SingletonScope() vs TransientScope() vs RequestScope() when I do my binding in my global.cs file. I have for example my call to MongoSession (using NoRM and the mvcStarter project http://mvcstarter.codeplex.com/) which is set to SingletonScope but I created a repository that use this MongoSession object to make calls to Mongo easier, e.g., I have a NewsRepository which uses MongoSession to fetch my News items from the data. As an example I have a call that

Ways to setup a Ninject singleton

不问归期 提交于 2019-12-03 17:11:47
问题 I have a class ( MyFacade ) that I injected parameter(s) with Ninject : class MyFacade { IDemoInterface demo; public MyFacade(IDemoInterface demo) { this.demo = demo; } public void MyMethod() { Console.WriteLine(demo.GetInfo()); } } Of course, I have to setup the Ninject to inject the appropiate implementation of my parameter ( IDemoInterface ) I know, I can instantiate MyFacade object by doing kernel.Get<MyFacade>(); without setting anything else. Currently my facade doesn't have an

Filter constructor injection using Ninject

柔情痞子 提交于 2019-12-03 17:04:46
问题 I am trying to find a way to use Ninject to inject constructor dependencies into filters. I am finding many articles describing property injection which is now advised against, but the remainder of articles involve complex setups with factories, locators, global wrappers or stub attributes. With MVC allowing you to override almost any part of it's operation I would have thought it would be simply a case of creating your own filter provider in a similar fashion to how you create your own

How do I use AutoMapper with Ninject.Web.Mvc?

浪子不回头ぞ 提交于 2019-12-03 16:34:18
问题 Setup I have an AutoMapperConfiguration static class that sets up the AutoMapper mappings: static class AutoMapperConfiguration() { internal static void SetupMappings() { Mapper.CreateMap<long, Category>.ConvertUsing<IdToEntityConverter<Category>>(); } } where IdToEntityConverter<T> is a custom ITypeConverter that looks like this: class IdToEntityConverter<T> : ITypeConverter<long, T> where T : Entity { private readonly IRepository _repo; public IdToEntityConverter(IRepository repo) { _repo =

Constructor with multiple arguments with Ninject

房东的猫 提交于 2019-12-03 16:18:52
问题 I am tring to use Ninject as a IoC container but could not understand how to create an instance of a class that has more than 1 parameter in the constructor. Basically I have a service interface for authentication in a PCL library and its implementation in a WP8 project that receives in the constructor the cosumer key, secret and baseAddress: //On PCL project public interface IAuthorizationService { bool Authenticate(); } //On WP8 Project pubilc class MyAuthenticator : IAuthorizationService {

What happens to using statement when I move to dependency injection

你。 提交于 2019-12-03 16:16:28
问题 I am currently using the following code: public class MyProvider { public MyProvider() { } public void Fetch() { using (PopClient popClient = new PopClient()) { .... } } } Because I want to be able to unit test the Fetch method and due to the fact that I can't mock PopClient, I created an interface and a wrapper class that calls into PopClient. My updated code looks like: public class MyProvider { private readonly IPopClient popClient; public MyProvider(IPopClient popClient) { this.popClient

Get an instance of an object with Ninject

青春壹個敷衍的年華 提交于 2019-12-03 14:58:54
问题 I installed on my project Ninject.MVC3 via Nuget. I read this article that to inject dependencies in my controllers, all you had to do was install Ninject, add my dependencies in NinjectMVC3.cs and ready. So far so good, but how to retrieve the instance of an object? public ActionResult MyAction() { var myObject = /* HERE ??*/ } In the constructor of the controller I have no problems! public class AccountController : Controller { public AccountController(IRepository repository) { ... } //This

Log ninject resolved dependencies application start-up

半城伤御伤魂 提交于 2019-12-03 14:24:22
We have started using Ninject version 2 as our IoC container along with the extension for resolving by naming conventions. We are also using log4net for our logging. What I would like is for Ninject to log all the dependencies it has found and what will resolve them to, preferably on the application start-up. I have found the logging extension, but can't find documentation or examples on how to use it to get this. Edit: Since it was requested here is the class that logs the default bindings on startup, using log4net public class DefaultBindingGeneratorWithLogging : IBindingGenerator { private

How to Dispose DbContext(or object) in asp.net mvc3 App when Ninject is used as dependency resolver

倖福魔咒の 提交于 2019-12-03 13:56:30
问题 For this Demo I have created a fake Database+repository as below Fake Db + Repository public interface IDemoRepository { string[] GetUsers(); } public class DemoRepository : IDemoRepository, IDisposable { public string[] GetUsers() { string[] Users = { "Robert","Linda","Jack"}; return Users; } public void Dispose() { //do nothing throw new Exception("Disposed is called"); } } My Controller looks this public class TestController:Controller { protected IDemoRepository _repository; public