ioc-container

Castle Windsor DI installer: dependency factory method has nested dependency on ApiController property

你说的曾经没有我的故事 提交于 2020-01-03 05:18:08
问题 I am trying to implement DI with Castle Windsor. Currently I have a controller with overloaded constructors like this (this is an antipattern as described here: https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=97): public class MyController : ApiController { protected IStorageService StorageService; protected MyController() { StorageService = StorageServiceFactory.CreateStorageService(User.Identity as ClaimsIdentity); } protected MyController(IStorageService storageService) {

Confused over using IOC container, service locator and factory

為{幸葍}努か 提交于 2020-01-02 06:45:30
问题 Suppose I have a BaseForm which depends on an ILogger or IResourceManager or something like that. Currently it resolves the correct implementation of the required service using the service locator which I know is an anti-pattern. Is using the constructor injection the right way to resolve this kind of dependency? Do I have to register my BaseForm (and its' derived types) in the container in order to create instances of them with resolved dependencies? Doesn't that complicate everything? Is it

How to create Illuminate/Support/Facade/App facade for standalone Illuminate IoC Container

只愿长相守 提交于 2020-01-02 03:51:08
问题 In my standalone (without Laravel) project i want to use Illuminate IoC container. Also i would like to access the app container through App facade provided by illuminate/support component. I installed both components (v5.0.28). Here is my (simplified) code: function setup_App(){ $container = new Illuminate\Container\Container(); Illuminate\Support\Facades\Facade::setFacadeApplication($container); class_alias('Illuminate\Support\Facades\App', 'App'); } setup_App(); App::bind('w', 'Widget');

Should I encapsulate my IoC container?

亡梦爱人 提交于 2020-01-02 03:00:13
问题 Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. I'm trying to decide whether or not it makes sense to go through the extra effort to encapsulate my IoC container. Experience tells me that I should put a layer of encapsulation between my apps and any third-party component. I just don't know if this is bordering on overkill. I can think of

Castle Windsor: UsingFactoryMethod can't instantiate with a weird error

北慕城南 提交于 2020-01-01 12:02:30
问题 When I use this registration: container.Register( Component .For<IFooFactory>() .ImplementedBy<FooFactory>(), Component .For<IFoo>() .UsingFactoryMethod(kernel => kernel.Resolve<IFooFactory>().CreateFoo()) ); I get this exception: Castle.MicroKernel.ComponentRegistrationException: Type MyNamespace.IFoo is abstract. As such, it is not possible to instansiate it as implementation of MyNamespace.IFoo service I'm not really sure what the problem is. But the stack trace shows that in

Creating new instances while still using Dependency Injection

落爺英雄遲暮 提交于 2020-01-01 09:55:19
问题 A quick description of the environment: I have a class that represents a chatroom and has a dependency on a logger. It's not the same as a system-wide logger with cross-cutting concerns, but a logger that's tied to that specific chatroom. It logs all activity in that chatroom to it's unique log file. When the chatroom is created I want to open the log file, and when it's destroyed I want to close the log file. The Problem Here's the relevant code I'm using. public interface IChatroomLogger {

Dependency Injection - When to use property injection

依然范特西╮ 提交于 2020-01-01 05:27:07
问题 I've a class which have a constructor like this: private string _someString; private ObjectA _objectA; private ObjectB _objectB; private Dictionary<Enum, long?> _dictionaryA; private Dictionary<Tuple<Enum,long?>, long?> _dictionaryB; public SomeDiClass(string someString) { _someString = someString; _objectA = new ObjectA(); _objectB = new ObjectB(); _dictionaryA = new Dictionary<Enum, long?>(); _dictionaryB = new Dictionary<Tuple<Enum, long?>, long?>(); } I want to get the dependency creation

Configuring Unity to resolve a type that takes a decorated dependency that has a parameter that varies with the type into which it is injected

我的梦境 提交于 2019-12-31 21:30:31
问题 This is a fairly straight forward decorator pattern scenario, with the complication that the decorated type has a constructor parameter that is dependent on the type into which it is being injected. I have an interface like this: interface IThing { void Do(); } And an implementation like this: class RealThing : IThing { public RealThing(string configuration) { ... implementation ... } public void Do() { ... implementation ... } } And a decorator like this: class DecoratingThing : IThing {

C# automatic creation of repositories by entity type

落爺英雄遲暮 提交于 2019-12-31 02:44:08
问题 I found an example of Generic Repository which is based on Entity Framework and trying to understand how to automatically resolve repositories by the same interface and entity type. The link above leads to the repo where you can see the following approach: public class HomeController : Controller { private readonly ICategoryRepository _repository; public HomeController(ICategoryRepository repository) { _repository = repository; } in this case we have to create a separate CategoryRepository -

How does Castle Windsor respond to a class which implements multiple interfaces?

柔情痞子 提交于 2019-12-30 09:40:51
问题 For example I have two interfaces: ICustomerService and IOrderService which each has a couple of functions like GetCustomer, GetOrder, etc. I want one class to implement both interfaces: Server. How does Castle Windsor respond to this? Is it possible in the first place? When I resolve the Server object based on one of the two interfaces, will I get the same object? What happens when I have a constructor that has both interfaces in its parameters? Will there still be one object constructed.