constructor-injection

Spring Constructor Argument Ambiguity with Simple Types

孤街醉人 提交于 2019-12-11 00:33:43
问题 I'm following Spring Reference Documentation 4.0.0.RELEASE. It says When a simple type is used, Spring cannot determine the type of the value, and so cannot match by type without help The scenario is as follows package examples; public class ExampleBean { private int years; private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; } } Now to resolve the arguments, it is instructed to use type attribute in

Register IOC container to self

房东的猫 提交于 2019-12-10 23:05:32
问题 Let's say I have an interface IDependencyResolver: public interface IDependencyResolver{ T Resolve<T>() where T: class; object Resolve(Type source); } And an implementation with the use of SimpleInjector: public class SIDependencyResolver:IDependencyResolver{ private readonly Container _container; public SIDependencyResolver(Container container){ _container = container; _container.Options.DefaultScopedLifestyle = new WcfOperationLifeStyle(); RegisterDependencies(_container, LifeStyle.Scoped);

Mapping child classes with parent injected in the constructor using AutoMapper

一个人想着一个人 提交于 2019-12-10 18:47:49
问题 I have a following class structure: class SrcChild { public bool SomeProperty { get; set; } } class SrcParent { public IEnumerable<SrcChild> Children { get; set; } } so the SrcParent has a collection of SrcChild objects. Now I want to map an instance of SrcParent to DstParent. Here are the destination classes: class DstChild { public bool SomeProperty { get; set; } public DstChild(DstParent parent) { if (parent == null) throw new ArgumentNullException(); } } class DstParent { public

Unity/Caliburn Micro, Injection Constructor with multiple parameters

China☆狼群 提交于 2019-12-10 11:57:27
问题 I am currently trying to learn how to implement MVVM using Unity and Caliburn Micro. After looking around for help elsewhere I am still unsure about how to set up the Constructor Injection properly. I don't know if this is not working due to my lack of expertise in MVVM or something else. My problem is I want to pass in two IScreen Objects into my main window(shell) Class that can be navigated between when the user clicks on a button. Here is the code for the constructor in my

How to inject two instances of same object using Autofac?

最后都变了- 提交于 2019-12-10 09:19:24
问题 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 {

Design By Contract, writing test-friendly code, object construction and Dependency Injection putting all together best practices

核能气质少年 提交于 2019-12-08 23:34:38
问题 I have been trying to figure out the best practices to write test-friendly code, but more specifically the practices related to object construction. In the blue book we discovered that we should enforce invariants when creating objects to avoid the corruption of our entities, value objects, etc. with this thought in mind, Design By Contract seems like the solution to avoid the corruption of our objects, but when we follow this, we could end up writing code like this: class Car { //Constructor

How to inject a service into a dynamic component with Angular2

泄露秘密 提交于 2019-12-07 08:52:20
问题 I have a component that dynamically loads another component using DynamicComponentLoader. However, the dynamic component needs to have a service injected. However, I'm unsure how to go about this. I see from Angular.io docs that DynamicComponentLoader accepts a ResolvedProvider array. I've tried to get a provider by doing: var provider = provide(ManagerService, {useExisting: ManagerService}); dcl.loadIntoLocation(this.data.template, this.er, "content",[provider]); This doesn't seem to work.

How to configure Unity to inject an array for IEnumerable

旧街凉风 提交于 2019-12-07 08:32:42
问题 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;

How to use Ninject in constructor injection of a type in an external assembly

江枫思渺然 提交于 2019-12-06 11:30:42
问题 I am loading a type from an external assembly and want to create an instance of the type. However, this type/class is setup for constructor injection by objects currently being managed/bound by Ninject . How can I use Ninject to create an instance of this type and inject any constructor dependencies? Below is how I get this type. Assembly myAssembly = Assembly.LoadFrom("MyAssembly.dll"); Type type = myAssembly.GetType("IMyType"); 回答1: Assuming you've created a Kernel , you should be able to

How do I constructor-autowire HttpServletResponse in Spring 3.1?

青春壹個敷衍的年華 提交于 2019-12-06 07:03:26
问题 I have a request scoped bean, and I need to have access to the HttpServletResponse and HttpServletRequest objects. I need access to these objects in the constructor, so property autowiring is not an option. I did the following: @Component @Scope("request") public class MyClass{ @Autowired(required=true) public MyClass(HttpServletRequest request, HttpServletResponse response) { // do stuff I need to do in the constructor } } This gives me the following error: No default constructor found;