ioc-container

UnityContainer and internal constructor

旧城冷巷雨未停 提交于 2019-12-10 02:19:08
问题 I have a class with internal constructor and want to Resolve it from Unity (2.0). public class MyClass { internal MyClass(IService service) { } } then I'm doing _container.Resolve<MyClass>(); when I do so I have an exception Exception is: InvalidOperationException - The type MyClass cannot be constructed. IService is registered and the only problem is that constructor is internal. I really want this class to be public, but I want it to be creatable only via a factory (in which I'm actually

How to register a class instance including a parameter in SimpleIOC

杀马特。学长 韩版系。学妹 提交于 2019-12-09 23:49:19
问题 I need to create an instance of a ViewModel with a specific parameter passed to the ViewModel when created. At the same time this ViewModel instance should be registered in SimpleIOC i thought this was the method for it: SimpleIoc.Register<TClass> Method (Func<TClass>, String, Boolean) with set to true for the last parameter for instant creation. so if i understood that correctly, this method wants a reference to a method that will create my ViewModel instance. This is called a ClassFactory

Autofac with Open Generics and Type Specified at Runtime

穿精又带淫゛_ 提交于 2019-12-09 16:45:09
问题 The documentation states that Autofac supports open generics and I am able to register and resolve in a basic case like so: Registration: builder.RegisterGeneric(typeof(PassThroughFlattener<>)) .As(typeof(IFlattener<>)) .ContainerScoped(); Resolve: var flattener = _container.Resolve<IFlattener<Address>>(); The above code works just fine. However, assuming that I will not know the type provided to IFlattener until runtime, I want to do something like this: object input = new Address(); var

Unity .NET: List of dependencies

北城余情 提交于 2019-12-09 16:12:14
问题 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 回答1: 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

Is it possible to inject mocks for testing purposes with AndroidAnnotations?

孤街醉人 提交于 2019-12-09 16:07:27
问题 I haven't found any examples on how to do this. I'm assuming it is not possible based on examples like this: @Bean(MyImplementation.class) MyInterface myInterface; where the class to inject is already determined. 回答1: The question is, are you unit testing or integration testing? If you are unit testing, I would suggest using mocks the old fashioned way, by using a setter and trying to test the Java code without the dependency injection framework involved. This will test your class in

Laravel5 dependency injection on Model

懵懂的女人 提交于 2019-12-09 15:29:04
问题 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

How to inject dependency property using Ioc Unity

百般思念 提交于 2019-12-09 15:06:50
问题 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,

Simple injector open generic decorators

人走茶凉 提交于 2019-12-09 14:00:25
问题 I am trying to make use of some of the nice features in simple injector. I am currently having problems with the decorators, they are not getting hit when I expect them too. I am registering them like this: container.RegisterManyForOpenGeneric( typeof(ICommandHandler<>), AppDomain.CurrentDomain.GetAssemblies()); container.RegisterDecorator( typeof(ICommandHandler<>), typeof(CreateValidFriendlyUrlCommandHandler<>), context => context.ServiceType == typeof(ICommandHandler<CreateProductCommand>)

Unittesting IoC registration?

主宰稳场 提交于 2019-12-09 11:59:39
问题 Should you unittest the code that registers components into your IoC container? If so, how? 回答1: In spring you can have a unit test that simply loads the application-context without asserting anything. It's actually a fairly useful test in conjunction with automatic build, since spring complains about a lot of problems when loading the full context. 回答2: It somehow feels wrong to me to have the IoC container running in my test-projects. I also noticed that most of the bugs that are caused by

Registering NLog ILogger with Simple Injector

流过昼夜 提交于 2019-12-09 09:25:12
问题 Is there any way I can get the context so I can retrieve the loggerName and use LogManager.GetLogger(loggerName) instead of LogManager.GetCurrentClassLogger() ? I noticed container.RegisterConditional() has access to the context. Also, I want to avoid solutions like SimpleLogging.NLog for now. Finally, I am willing to accept this is not the right approach. BTW, AOP is an option that I've already explored (Is it a good practice to have logger as a singleton?). Note: I am aware that