Setter Injection of only one property of same type
See original GitHub issueNot 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
@ChristianSteu I’ve got a backlog of issues to sweep up soon in Lamar, so maybe in the next week or so.
@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)