ioc-container

The right way to do property dependency injection using Unity

只谈情不闲聊 提交于 2019-12-13 01:16:43
问题 I have a class that needs a dependency injecting. As the class is already an implementation of another abstraction, and its 'sibling' implementations may not share the same dependencies, I am attempting to use property injection and not constructor injection. (All these classes/interface names are just for illustrative purposes) My IProvider abstraction: public interface IProvider { void ProviderMethod(); } My IProvider implementation (with the IData dependency I want to inject) : public

Simple Injector: RegisterInitializer does not always fire

佐手、 提交于 2019-12-12 22:22:47
问题 I use the RegisterInitializer method to inject properties in base types as follows: container.RegisterInitializer<BaseHandler>(handler => { handler.Context = container.GetInstance<HandlerContext>(); }); This works great, but the RegisterInitializer doesn't get fired on all registered types that inherit from BaseHandler. It doesn't seem to run when I call new myself: var url = ConfigurationManager.AppSettings["NotificationServiceUrl"]; container.Register<Handler<NotifyCustomerMessage>>(() =>

How do i use WindsorInstaller across multiple assemblies for registration

人走茶凉 提交于 2019-12-12 19:23:47
问题 I am using Castle Windsor to handle my Dependency Injection and it has been working great up until now. However, i am now trying to extend my project and add some additional libraries - im now struggling to figure the best way to leverage Castle. I currently have the following assemblies MyProject.Interfaces (contains IDBContext interface) MyProject.BusinessLogic (contains the Castle Windsor implementation) MyProject.DataAccess (contains implementation of IDBContext) I currently have an

Can I write the results of SimpleInjectors diagnostics to a log file?

China☆狼群 提交于 2019-12-12 18:46:49
问题 Using SimpleInjector I call container.Verify() at the end of my configuration, and get the diagnostics information in the debugger as described in the documentation. I would like to write that information to a log file. Is there a way access it programmatically or a way hook a logger or tracer into SimpleInjector? 回答1: Simple Injector 2.4 contains a diagnostic API (SimpleInjector.Diagnostics.dll) that allow you to query the container for diagnostic warnings. Using this API you can write

ASP.NET MVC3 - Use DependencyResolver AND Windsor Castle: Why?

可紊 提交于 2019-12-12 16:31:32
问题 Can somebody shine a little light for me? I've got my website all running using Windsor Castle. I have a controller factory and installers for controllers and services. All nice. Now I've just created a IDependencyResolver implementing class called WindsorDependencyResolver with a straigh-forward implementation: public class WindsorDependencyResolver : System.Web.Mvc.IDependencyResolver { private readonly IKernel _kernel; public WindsorDependencyResolver (IKernel kernel) { _kernel = kernel; }

How do I manage object disposal when I use IoC?

﹥>﹥吖頭↗ 提交于 2019-12-12 10:53:28
问题 My case it is Ninject 2. // normal explicit dispose using (var dc = new EFContext) { } But sometimes I need to keep the context longer or between function calls. So I want to control this behavior through IoC scope. // if i use this way. how do i make sure object is disposed. var dc = ninject.Get<IContext>() // i cannot use this since the scope can change to singleton. right ?? using (var dc = ninject.Get<IContext>()) { } Sample scopes Container.Bind<IContext>().To<EFContext>()

IoC and managing interfaces

醉酒当歌 提交于 2019-12-12 10:11:27
问题 Say I had a business object library which was using IoC to implement a data access library. Where should I define the Data Access Interface? Which library does it belong? Or should it be in a separate library just for interfaces? 回答1: I would define the interfaces within the business domain. Then the implementations of the interfaces would be in a library that references the business domain (and is referenced by whatever the application context is, or by an IoC library which is referenced by

composing MEF parts in C# like a simple Funq container

冷暖自知 提交于 2019-12-12 03:48:15
问题 In Funq and probably most other IoC containers I can simply do this to configure a type: container.Register<ISomeThing>(c => new SomeThing()); How could I quickly extend MEF (or use existing MEF functionality) to do the same without using attributes. Here is how I thought I could do it: var container = new CompositionContainer(); var batch = new CompositionBatch(); batch.AddExport<ISomeThing>(() => new SomeThing()); batch.AddExportedValue(batch); container.Compose(batch); With this extension

Injecting a nullable dependency via constructor requires unity to register that nullable dependency

我的未来我决定 提交于 2019-12-12 02:17:09
问题 Just wondering if anyone had this scenario with Unity(as an IoC container) where the class has two injected dependencies(interfaces) where one dependency can be null. For example: public MyServiceDll(IRepository repository, ICanBeNullDependency canBeNullDependency = null) { _repository = repository; _canBeNullDependency = canBeNullDependency; } ICanBeNullDependency is from another assembly. MyServiceDll is another assembly. MyServiceDll is referenced by web api and injected its interface in

Using StructureMap, when a default concrete type is defined in one registry, can it be redefined in another registry?

拈花ヽ惹草 提交于 2019-12-12 01:30:03
问题 In the project I'm working on I have a StructureMap registry for the main web project and another registry for my integration tests. During some of the tests I wire up the web project's registry, so that I can get objects out of the container for testing. In one case I want to be able to replace a default concrete type from the web registry with one in the test registry. Is this possible? How do you do it? 回答1: In a similar situation I created a NestedContainer ( Container.GetNestedContainer(