constructor-injection

How to configure Unity to inject an array for IEnumerable

懵懂的女人 提交于 2019-12-05 12:39:31
I have a class which takes an IEnumerable constructor parameter which I want to resolve with Unity and inject an array of objects. These simple classes illustrate the problem. public interface IThing { int Value { get; } } public class SimpleThing : IThing { public SimpleThing() { this.Value = 1; } public int Value { get; private set; } } public class CompositeThing : IThing { public CompositeThing(IEnumerable<IThing> otherThings) { this.Value = otherThings.Count(); } public int Value { get; private set; } } Say I want to inject four SimpleThing in to CompositeThing . I've tried several

How to inject two instances of same object using Autofac?

岁酱吖の 提交于 2019-12-05 12:26:44
I'm using Autofac constructor injection. I need to figure out how to inject a single object instance into more than one constructor argument, without needing to explicitly resolve each argument during the container setup phase. I have a complex scenario which would be simplified by this behavior; the following example is just a simplified scenario so I can demonstrate the behavior I'm looking for. Example: Say I have these two interfaces, IOpenable and ICloseable: public interface IOpenable { void Open(); } public interface ICloseable { void Close(); } And I have this Door class which

Constructor Injection with TinyIoC

给你一囗甜甜゛ 提交于 2019-12-05 01:49:52
问题 I have just changed from Ninject to TinyIoC for dependency injection and I'm having trouble with constructor injection. I have managed to simplify it down to this snippet: public interface IBar { } public class Foo { public Foo(IBar bar) { } } public class Bar : IBar { public Bar(string value) { } } class Program { static void Main(string[] args) { var container = TinyIoCContainer.Current; string value = "test"; container.Register<IBar, Bar>().UsingConstructor(() => new Bar(value)); var foo =

Asp.net Identity, Generate WebApi token OAuthGrantResourceOwnerCredentialsContext - no access to UserManager using Unity

瘦欲@ 提交于 2019-12-04 12:15:17
I am trying to setup a project structure so that I have a WebApi, WebUI and Domain layer. I have moved all the Asp.Net.Identity objects into the Domain layer and have also setup the ApplicationContext here too (inheriting from IdentityContext). (I have used this tutorial and package as a base which is excellent. http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/ ) In the WebAPI layer I am able to use the Account controller correctly to login and register. However, I cannot generate an access token. The OAuthGrantResourceOwnerCredentialsContext method internally

Spring constructor injection of SLF4J logger - how to get injection target class?

左心房为你撑大大i 提交于 2019-12-03 18:51:51
问题 I'm trying to use Spring to inject a SLF4J logger into a class like so: @Component public class Example { private final Logger logger; @Autowired public Example(final Logger logger) { this.logger = logger; } } I've found the FactoryBean class, which I've implemented. But the problem is that I cannot get any information about the injection target: public class LoggingFactoryBean implements FactoryBean<Logger> { @Override public Class<?> getObjectType() { return Logger.class; } @Override public

Best practices for dependency injection via constructor

烂漫一生 提交于 2019-12-03 13:31:27
问题 Inversion of control is a value-proof technique which is used to modularize a system and decouple the components from each other. Low coupling is always an advantage: it simplifies automatic testing of the components and makes the code better conforming to single responsibility principle. Among the ways to declare a dependency to another class (service locator, property injection calling a public method / setting a public property...), the constructor injection seems the best approach. Though

Dependency injection with multiple repositories

邮差的信 提交于 2019-12-03 13:21:23
问题 I have a wcf service and on the client i have: var service = new ServiceReference1.CACSServiceClient() The actual service code is: public CACSService() : this(new UserRepository(), new BusinessRepository()) { } public CACSService(IUserRepository Repository, IBusinessRepository businessRepository) { _IRepository = Repository; _IBusinessRepository = businessRepository; } So, all this works fine, but i don't like how i am newing up all the repositories at the same time because the client code

Constructor Injection - Do we inject factories as well?

馋奶兔 提交于 2019-12-03 12:23:29
After listening to the Clean Code Talks , I came to understand that we should use factories to compose objects. So, for example, if a House has a Door and a Door has a DoorKnob , in HouseFactory we create a new DoorKnob and pass it to the constructor of Door , and then pass that new Door object to the constructor of House . But what about the class that uses the House (say the class name is ABC ) ? It will depend on the HouseFactory , right? So do we pass the HouseFactory in the constructor of ABC ? Won't we have to pass a whole lot of factories in the constructor that way? Staying with the

Can't get value of Ninject ConstructorArgument (passed in as parameter to kernel.Get)

有些话、适合烂在心里 提交于 2019-12-02 09:48:57
Having trouble getting the value of a ConstructorArgument parameter passed to kernel.Get(). I want to use the parameter's value to determine which of two string values will be passed into the constructor. The parameter is in fact there when expected, I just can't get at its value. I end up with a null ref exception after calling the parameter's GetValue method: namespace MyNS.DBProviders { public abstract class DBProviderBase { private ObjectContext _db; public DBProviderBase(ObjectContext db) { _db = db; } ... //abstract methods here ... } public class DBProviderB : DBProviderBase { public

Dagger2 - Project Rebuild Error - Field Injection - Android

假装没事ソ 提交于 2019-12-02 09:23:55
I have been trying to implement Dagger2. Problem: When I use constructor injection, it works fine but when I use field injection, it throws an Error like below: Error:(6, 48) error: cannot find symbol class DaggerApplicationComponent /home/moderator/Downloads/Maulik/Sample Codes/Made/Dagger2Demo/app/src/main/java/com/dagger2demo/dagger2demo/di/component/ApplicationComponent.java Error:(18, 10) error: com.dagger2demo.dagger2demo.mvp.HomePresenter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. This type supports members injection but cannot