Answered by:
Open Closed Principle with Inheritance and Dependency Injection

Question
-
User439975351 posted
Sorry if this is a stupid question but I can't seem to find a definitive answer via the usual Googling/Stack Overflowing ;-)
If I have an interface as follows:
public interface IMyInterface { string Result(int number, Dictionary<int, string> interfaceParameters); }
Then I inherit like so:
public class TestClass : IMyInterface { public string Result(int number, Dictionary<int, string> interfaceParameters) { var sb = new StringBuilder(); foreach (var m in interfaceParameters) { sb.Append(m.Value); } var result = sb.ToString(); return string.IsNullOrWhiteSpace(result) ? $"{number}" : result; } }
Then I add this for DI (Asp Core): services.AddScoped<IMyInterface, TestClass>();
Use it in the controller:
[ApiController] [Route("[controller]")] public class TestController : ControllerBase { private readonly IMyInterface _testClass; public TestController(IMyInterface testClass) { _testClass = testClass; } [HttpGet] public IEnumerable<string> Get() { var interfaceParameters = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" } }; var model = new List<string>(); for (var i = 1; i <= 10; i++) { model.Add(_testClass.Result(i, interfaceParameters)); } return model; } }
How would I extend the TestClass/IMyInterface using Open Close principles?
Monday, January 27, 2020 7:34 PM
Answers
-
User475983607 posted
Create a new class that implements the interface but has a different implementation. IMHO, it seems your design is very specific and not open to a different implementation
public class NewTestClass : IMyInterface { public string Result(int number, Dictionary<int, string> interfaceParameters) { //New implementation code } }
http://joelabrahamsson.com/a-simple-example-of-the-openclosed-principle/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 27, 2020 8:37 PM -
User475983607 posted
Thanks mgebhard,
How could I use this with DI if I already have it added here?:
services.AddScoped<IMyInterface, TestClass>();
Use a factory pattern if you have multiple implementations of the same interface.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1
https://espressocoder.com/2018/10/08/injecting-a-factory-service-in-asp-net-core/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 27, 2020 9:04 PM
All replies
-
User475983607 posted
Create a new class that implements the interface but has a different implementation. IMHO, it seems your design is very specific and not open to a different implementation
public class NewTestClass : IMyInterface { public string Result(int number, Dictionary<int, string> interfaceParameters) { //New implementation code } }
http://joelabrahamsson.com/a-simple-example-of-the-openclosed-principle/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 27, 2020 8:37 PM -
User439975351 posted
Thanks mgebhard,
How could I use this with DI if I already have it added here?:
services.AddScoped<IMyInterface, TestClass>();
Monday, January 27, 2020 8:49 PM -
User475983607 posted
Thanks mgebhard,
How could I use this with DI if I already have it added here?:
services.AddScoped<IMyInterface, TestClass>();
Use a factory pattern if you have multiple implementations of the same interface.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1
https://espressocoder.com/2018/10/08/injecting-a-factory-service-in-asp-net-core/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 27, 2020 9:04 PM -
User439975351 posted
Thanks mgebhard, I will read through these.
Kind regards,
JustinMonday, January 27, 2020 9:56 PM