question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setter Injection of only one property of same type

See original GitHub issue

Not sure if that is a bug or feature, but working with some stuff, I stumbled into some to me un-intuitive behavior:

I have an interface with some get/set Properties that have the same Type but different names. The default-implementation provides initialization for them, but I want to replace one of those values without the need to re-implement/decorate the whole thing. So my thought was: Lamar has setter injection, so just inject the value I want into the specific property.

Unfortunately that didn’t work that easily, as Lamar used the new values for all properties of this type 😕. To fix that I would need to provide values for all properties, while optimally I would just like not invoking these other setters at all.

Here is code to reproduce the issue (using net5.0 with Lamar 5.0.3):

using System;
using Lamar;

namespace DependencyShenanigans
{
	public class Program
	{
		static void Main(string[] args)
		{
			var container = new Container(new RegistryViaLamar());
			var thing = container.GetInstance<IThingWithProperties>();
			
			Console.WriteLine($"{thing.PropertyA}-{thing.PropertyB}-{thing.PropertyC}");
			// outputs "X-X-X", but I only want to set PropertyB to get the output "A-X-C"
			// (instead of the default "A-B-C")
		}
	}

	public class RegistryViaLamar : ServiceRegistry
	{
		public RegistryViaLamar()
		{
			For<IThingWithProperties>().Use<ThingWithProperties>()
				.Setter<object>(nameof(IThingWithProperties.PropertyB)).Is("X");
		}
	}
	
	public interface IThingWithProperties
	{
		object PropertyA { get; set; }
		object PropertyB { get; set; }
		object PropertyC { get; set; }
	}

	public class ThingWithProperties : IThingWithProperties
	{
		public object PropertyA { get; set; } = "A";
		public object PropertyB { get; set; } = "B";
		public object PropertyC { get; set; } = "C";
	}
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremydmillercommented, Aug 17, 2021

@ChristianSteu I’ve got a backlog of issues to sweep up soon in Lamar, so maybe in the next week or so.

0reactions
ChristianSteucommented, Aug 17, 2021

@jeremydmiller I appreciate you saying I shouldn’t use setter injection (I don’t like it myself), but it was the most sensible solution for the problem I had.

The code I provided is everything that is to the project that reproduces that behavior, so I guess that makes it a bug (although from what I read I have the feeling that it has a pretty low/minimal priority)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to inject only one property into class - java
For arguments to the constructor use the constructor-arg xml tag and for parameters that are set using setters use the property xml tag....
Read more >
Can a setter injection and constructor injection be used ...
Yes Perfectly you can use both at same time but the thing is Overriding Setter injection always overrides the constructor injection.
Read more >
Why I Prefer Constructor Injection over Setter or Property ...
If new properties are added to an existing class, setter injection does not guarantee that all will be found and set. New properties...
Read more >
Setter injection versus constructor injection and the use of ...
The second reason why setter injection is used a lot more often than you would expect, is the fact that frameworks like Spring...
Read more >
Why You Should Use Constructor Injection in Spring
In setter-based injection, we provide the required dependencies as field parameters to the class and the values are set using the setter methods ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found