dependency-injection

Dagger 2 - two provides method that provide same interface

亡梦爱人 提交于 2020-05-22 13:35:12
问题 lets say I have: public interface Shape {} public class Rectangle implements Shape { } public class Circle implements Shape { } and I have a ApplicationModule which needs to provides instances for both Rec and Circle : @Module public class ApplicationModule { private Shape rec; private Shape circle; public ApplicationModule() { rec = new Rectangle(); circle= new Circle (); } @Provides public Shape provideRectangle() { return rec ; } @Provides public Shape provideCircle() { return circle; } }

Dagger 2 - two provides method that provide same interface

夙愿已清 提交于 2020-05-22 13:34:59
问题 lets say I have: public interface Shape {} public class Rectangle implements Shape { } public class Circle implements Shape { } and I have a ApplicationModule which needs to provides instances for both Rec and Circle : @Module public class ApplicationModule { private Shape rec; private Shape circle; public ApplicationModule() { rec = new Rectangle(); circle= new Circle (); } @Provides public Shape provideRectangle() { return rec ; } @Provides public Shape provideCircle() { return circle; } }

Autofac ResolvedParameter/ComponentContext not working

你离开我真会死。 提交于 2020-05-22 10:05:07
问题 I am using asp.net core along with Entity Framework Core . My scenario here is, I want to change the connection string at runtime based on HttpContext query string value. I am trying to pass ResolvedParameter with Reflection components as documented. But, It is not getting registered when I resolve this. Here below, I have attached my code snippet. Autofac Registration class: public class DependencyRegistrar : IDependencyRegistrar { public virtual void Register(ContainerBuilder builder) {

Get the instance of a component provider out of its scope

强颜欢笑 提交于 2020-05-17 06:01:37
问题 Is it possible to access the instance of a service included on a component decorator providers array from out of its scope? The scope is basically all the children components of the component where it's created, but I'd need to access it from a resolver. I guess there should be some way to get it using the injector, but I'm not sure if the injector has access to the ElementInjector hierarchy. 来源: https://stackoverflow.com/questions/61629924/get-the-instance-of-a-component-provider-out-of-its

Get the instance of a component provider out of its scope

余生颓废 提交于 2020-05-17 06:01:36
问题 Is it possible to access the instance of a service included on a component decorator providers array from out of its scope? The scope is basically all the children components of the component where it's created, but I'd need to access it from a resolver. I guess there should be some way to get it using the injector, but I'm not sure if the injector has access to the ElementInjector hierarchy. 来源: https://stackoverflow.com/questions/61629924/get-the-instance-of-a-component-provider-out-of-its

The container can't be changed after the first call to GetInstance, GetAllInstances, Verify, and some calls of GetRegistration

放肆的年华 提交于 2020-05-15 21:31:05
问题 I cant figure out why I am getting this error below within the RegisterCollection method. Am I doing the setup incorrectly? The container can't be changed after the first call to GetInstance, GetAllInstances, Verify, and some calls of GetRegistration. Please see https://simpleinjector.org/locked to understand why the container is locked. The following stack trace describes the location where the container was locked: Logger registration public static void Register(Container container) {

Autofac resolve the same class instance with different parameters

谁说我不能喝 提交于 2020-05-15 02:32:06
问题 I have a Xamarin.Forms application where I am using Autofac for DI. SO what I want is quite simple but I am unable to do something which seems like a sinch to do. So I have a class that has two constructors both of them have a single parameter and based on certain conditions I want to resolve either one. As an example, I have a class A and what I want is based on conditions B or C should be resolved. Eg: public class A { public A(B bObject) {...} public A(C cObject) {...} } But I am unable to

Autofac resolve the same class instance with different parameters

廉价感情. 提交于 2020-05-15 02:32:03
问题 I have a Xamarin.Forms application where I am using Autofac for DI. SO what I want is quite simple but I am unable to do something which seems like a sinch to do. So I have a class that has two constructors both of them have a single parameter and based on certain conditions I want to resolve either one. As an example, I have a class A and what I want is based on conditions B or C should be resolved. Eg: public class A { public A(B bObject) {...} public A(C cObject) {...} } But I am unable to

Name of Design Pattern: get class from class level

谁说胖子不能爱 提交于 2020-05-12 20:32:09
问题 Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test

Name of Design Pattern: get class from class level

 ̄綄美尐妖づ 提交于 2020-05-12 20:31:29
问题 Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test