moo

Help with converting to DI

Hi there,​I’m in the process of upgrading a large legacy codebase and looking to move it to DI to help with testing and having it less coupled. Each class requires a user ID specified (for permission and record keeping requirements)My plan is to do something like:public class A:IA { private int UserId { get; set; } private readonly IB LogicClass { get; set; } public A(int userId) : this (userId, new B(userId)) { } public A(int userId, IB logicClass) { UserId = userId; LogicClass = logicClass; } } ​public class B:IB { private int UserId { get; set; } public B(int userId) : this (userId, new C(userId)) { } public B(int userId, IC logicClass) { UserId = userId; LogicClass = logicClass; } } etc etc…​Now, I’m new to DI so still learning it. But my understanding is constructor based DI is a valid option.​Firstly: Am I right in my thoughts around this as an option? There are a LOT of existing classes that need to be converted, so hope to have a good plan before starting it.Secondly, if this is a correct way of doing it, how would it work if (in an extended example above) class C has a requirement of class A? Wouldn’t that create a circular reference that would just create stack overflows? (Constructor of class A, creates a class B that then creates a class A, which then creates a class B etc etc)​Thanks in advance, I’m still new to this so learning as I go 🙂 via /r/dotnet https://ift.tt/3cFWG9Z

Categories: funny, photos