ioc-container

How do I avoid the service locator pattern? Should I?

倖福魔咒の 提交于 2019-12-20 03:12:52
问题 I'm currently working on a WinForms system (I know) where there's a lot of Constructor Injection when creating forms, but if those forms/views need to open another form, I find the DI container has been injected too so that we can locate the implementation of the desired view interface at runtime. e.g. public partial class MyView : Form, IMyView { private readonly IDIContainer _container; public MyView(IDIContainer container) { InitializeComponent(); _container = container; } public

Does the Spring bean container <import> command eliminate duplicate containers?

霸气de小男生 提交于 2019-12-19 13:43:05
问题 Does the <import> command of the Spring bean container eliminate duplicate containers? For example, if bean container file A imports B and C and each these in turn import D, does Spring eliminate or ignore the duplicate D container? 回答1: It doesn't eliminate duplicate "containers", but it will eliminate duplicate bean definitions. So the beans in D will only be created once in the resulting bean factory. You'll get a face full of warnings about it, though. It's something best avoided. One

Unity Batch Register by convention

时光怂恿深爱的人放手 提交于 2019-12-19 11:49:20
问题 I'm trying to do the equivalent of the following Autofac code in Unity IoC. builder.RegisterAssemblyTypes(typeof (DataRepository<>).Assembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces(); This basically replaces individually registering the following: DataSourceDataRepository : DataRepository<DataSource>, IDataSourceDataRepository For clarity: This registers all of my Repository types as their implemented interfaces, so when i inject IDataSourceDataRepository I get a

C# ASP.NET Dependency Injection with IoC Container Complications

你。 提交于 2019-12-19 11:35:19
问题 I apologise for the length, and I know there are some answers on this but I searched a lot and haven't found the right solution, so please bear with me. I am trying to create a framework for legacy applications to use DI in ASP.NET webforms. I will probably use Castle Windsor as the framework. These legacy applications will use in part an MVP pattern in some places. A presenter would look something like this: class Presenter1 { public Presenter1(IView1 view, IRepository<User> userRepository)

C# ASP.NET Dependency Injection with IoC Container Complications

这一生的挚爱 提交于 2019-12-19 11:35:08
问题 I apologise for the length, and I know there are some answers on this but I searched a lot and haven't found the right solution, so please bear with me. I am trying to create a framework for legacy applications to use DI in ASP.NET webforms. I will probably use Castle Windsor as the framework. These legacy applications will use in part an MVP pattern in some places. A presenter would look something like this: class Presenter1 { public Presenter1(IView1 view, IRepository<User> userRepository)

AutoFac - Registering a decorator for some of an open Generic

拥有回忆 提交于 2019-12-19 09:10:22
问题 I'm attempting to set up an Autofac module that seems to have a complicated requirement. here goes: I have a generic interface: public interface IMyInterface<TFoo, TBar> I have a bunch of classes that implement this interface e.g. class MyImpl1 : IMyInterface<string, bool> { } class MyImpl2 : IMyInterface<bool, string> { } class MyImpl3 : IMyInterface<bool, string> { } Finally, I have a decorator: class MyDecorator<TFoo, TBar> : IMyInterface<TFoo, TBar> I only want to "Decorate" the

AutoFac - Registering a decorator for some of an open Generic

北城余情 提交于 2019-12-19 09:10:15
问题 I'm attempting to set up an Autofac module that seems to have a complicated requirement. here goes: I have a generic interface: public interface IMyInterface<TFoo, TBar> I have a bunch of classes that implement this interface e.g. class MyImpl1 : IMyInterface<string, bool> { } class MyImpl2 : IMyInterface<bool, string> { } class MyImpl3 : IMyInterface<bool, string> { } Finally, I have a decorator: class MyDecorator<TFoo, TBar> : IMyInterface<TFoo, TBar> I only want to "Decorate" the

Using Unity to inject objects into IValueConverter instance

删除回忆录丶 提交于 2019-12-19 07:11:23
问题 I have an instance of IValueConverter in a Silverlight 5 project, which converts custom data into different colors. I need to read the actual color values from a database (as these can be edited by the user). Since Silverlight uses asynchronous calls to load the data through Entity Framework from the database, I created a simple repository, which holds the values from the db. The interface: public interface IConfigurationsRepository { string this[string key] { get; } } The implementation:

Can any of existing IoC containers create the lazy proxy classes dynamically?

夙愿已清 提交于 2019-12-19 04:07:14
问题 I study different DI patterns. And now I interested in the lazy life-time implementation. For example, I want to write a proxy class that hides the factory behind a service's interface. Can any of existing IoC containers (.NET) create this kind of proxy class dynamically at runtime? interface IService { void Foo(); void Bar(); } class ServiceFactoryProxy : IService { private readonly Func<IService> _factory; public ServiceFactoryProxy(Func<IService> factory) { if (factory == null) throw new

Dynamic selection of interface implementation using IoC

时间秒杀一切 提交于 2019-12-19 03:13:24
问题 I have a situation where the implementation of an interface is determined at runtime. For example, I check a string and then determine which subclass to use, without IoC it looks like the following: if (fruitStr == "Apple") { new AppleImpl().SomeMethod(); } else { new BananaImpl().SomeMethod(); } Both classes AppleImpl and BananaImpl are implementation of the same interface, say IFruit . How can this be done using IoC/Dependency Injection, especially in Castle Windsor ? 回答1: This is the