ioc-container

Castle Windsor - how to resolve components based on constructor parameters

我是研究僧i 提交于 2019-12-04 03:48:52
Say I have a component like this public class MyComponent { public MyComponent(string name) { } } I basically want to have the provided constructor parameters behave as part of the component identifier when resolving it. If you've never resolved it with that set of parameters, it will instantiate a new one. In other words, I want to somehow modify the following test to succeed: IWindsorContainer container = new WindsorContainer(); container.Register(Component.For<MyComponent>()); MyComponent a1 = container.Resolve<MyComponent>(new { name = "a" }); MyComponent a2 = container.Resolve<MyComponent

Unity .NET: List of dependencies

允我心安 提交于 2019-12-04 03:41:56
Is it possible to inject a list of dependencies like this in Unity or other kind of IoC libraries? public class Crawler { public Crawler(IEnumerable<IParser> parsers) { // init here... } } In this way I can register multiple IParser in my container and then resolve them. Is it possible? Thanks It is possible but you need to apply some workarounds. First you need to register each IParser with a name in the Unity Container. Second you need to register a mapping from IParser[] to IEnumerable<IParser> in the container. Otherwise the container cannot inject the parsers to the constructor. Here´s

Construtor/Setter Injection using IoC in HttpHandler, is it possible?

半世苍凉 提交于 2019-12-04 03:18:16
问题 I've ran into a rather hairy problem. There is probably a simple solution to this but I can't find it! I have a custom HttpHandler that I want to process a request, log certain info then enter the details in the database. I'm using NUnit and Castle Windsor. So I have two interfaces; one for logging the other for data entry, which are constructor injected. I quickly found out that there is no way to call the constructor as the default parameterless constructor is always called instead. So I

Laravel5 dependency injection on Model

和自甴很熟 提交于 2019-12-04 02:20:37
I have an Eloquent Model called Surface which is dependent on a ZipCodeRepository object: class Surface extends Model{ public function __construct(ZipCodeRepositoryInterface $zipCode){...} and an Address object that hasMany surfaces. class Address extends Model{ public surfaces() { return $this->hasMany('App/Surface'); } } My issue is when I call $address->surfaces I get the following error: Argument 1 passed to App\Surface::__construct() must be an instance of App\Repositories\ZipCodeRepositoryInterface, none given I thought the IoC would automatically inject that. Thanks to @svmm for

Simple Injector Register All Services From Namespace

眉间皱痕 提交于 2019-12-04 02:10:01
My Service Interfaces has a namespace of Services.Interfaces The implementation of the Service Interfaces has a namespace of Web.UI.Services I have 2 service implementations for example IUserService which needs to register to UserService ICountryService which needs to register to CountryService This is how I currently register these services with SimpleInjector. container.Register<IUserService, UserService> (); container.Register<ICountryService, CountryService> (); Problem: If I have over 100 services to exaggerate a bit. I need to go and add a line for each service. How can I register all

How to inject dependency property using Ioc Unity

筅森魡賤 提交于 2019-12-04 01:47:17
I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { return "MethodA1() " +serviceB.MethodB1(); } } public class ServiceB : IServiceB { public string MethodB1() { return "MethodB1() "; } } I use Unity for IoC, my registration looks like this: container.RegisterType<IServiceA, ServiceA>(); container.RegisterType<IServiceB, ServiceB>(); When I resolve a ServiceA instance, serviceB will be null . How can I resolve this? You have at

Unity Framework IoC with default constructor

限于喜欢 提交于 2019-12-04 00:38:16
问题 I'm trying to inject a dependency to my MVC controllers like this private static void RegisterContainer(IUnityContainer container) { container .RegisterType<IUserService, UserService>() .RegisterType<IFacebookService, FacebookService>(); } The UserService class has a constructor like this... public UserService(): this(new UserRepository(), new FacebookService()) { //this a parameterless constructor... why doesnt it get picked up by unity? } public UserService(IUserRepository repository,

Using Nininject MVC with class libraries

杀马特。学长 韩版系。学妹 提交于 2019-12-03 23:37:40
问题 I'm quite new to IoC frameworks so please excuse the terminology. So what I have is a MVC project with the Nininject MVC references. I have other class libarys in my project e.g. Domain layer, I would like to be able to use the Ninject framework in there but all of my bindings are in the NinjectWebCommon.cs under the App_Start folder in the MVC project: private static void RegisterServices(IKernel kernel) { kernel.Bind<IHardwareService>().To<WindowsHardwareService>(); kernel.Bind<IStatusApi>(

Ninject - binding constructors with arguments / Entity Framework connection string

人走茶凉 提交于 2019-12-03 22:35:55
Please forgive my ignorance, but I am very new to IOC and NinJect. I have searched for high and low for easily understandable solutions but so far they have eluded me. So far I have the following and all works as expected: private class StandardModule : NinjectModule { public override void Load() { Bind<ILog>().To<NLogLogger>(); // Use NLog Bind<IMyEntityFrameWorkRepository().To<MyEntityFrameWorkRepository>(); } } MyEntityFrameWorkRepository then creates its own EF DbContext via a connection string declared in app/web.config: public class MyDbContext : DbContext { public MyDbContext() : base(

Ninject and DataContext disposal

。_饼干妹妹 提交于 2019-12-03 22:20:38
I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important and that whenever you create a direct object of the DataContext (as in: new DataContext()) you should use a using() block. My question thus is: When im retrieving my DataContext from the kernel, should I still have to use a using() block? Or does Ninject fix this for me? I am investigating this for my colleague Bas. I was looking in the Ninject 2