C# Working with C # classes from different namespaces
-
Hello, There namespace Company.Service01, which contains classes and interfaces. There namespace Company.Service02, which contains classes and interfaces. Both spaces have the same structure of, contain the same classes and interfaces. I need to somehow ensure that I could work with class (for example: Eso9PortClient) from this namespace, depending on the input parameters of the program. It's possible there is some technique to it? The goal is that I did not have to repeat the same code n times the number of namespaces. Example: file01.cs namespace Company.Service0X { public interface Eso9Port {...} public partial class FaultInfo {...} public interface Eso9PortChannel {...} public partial class Eso9PortClient {...} } file02.cs namespace Company { class Program { static void Main (string [] args) { if (args [0] == "01") { MultiService MultiService m = new (); m.CreateOrder (); } else if (args [0] == "02") { MultiService MultiService m = new (); m.CreateOrder (); } else if (args [0] == "03") { MultiService MultiService m = new (); m.CreateOrder (); } } } public class MultiService { public void CreateOrder () { // Work with the instance Company.Service0X.Eso9PortClient // depending on the parameter in class Program Company.Service0X.Eso9PortClient Company.Service0X.Eso9PortClient x = new (); string xx = x.orders ... } } } Thank you in advance for your reply
-
Hello, There namespace Company.Service01, which contains classes and interfaces. There namespace Company.Service02, which contains classes and interfaces. Both spaces have the same structure of, contain the same classes and interfaces. I need to somehow ensure that I could work with class (for example: Eso9PortClient) from this namespace, depending on the input parameters of the program. It's possible there is some technique to it? The goal is that I did not have to repeat the same code n times the number of namespaces. Example: file01.cs namespace Company.Service0X { public interface Eso9Port {...} public partial class FaultInfo {...} public interface Eso9PortChannel {...} public partial class Eso9PortClient {...} } file02.cs namespace Company { class Program { static void Main (string [] args) { if (args [0] == "01") { MultiService MultiService m = new (); m.CreateOrder (); } else if (args [0] == "02") { MultiService MultiService m = new (); m.CreateOrder (); } else if (args [0] == "03") { MultiService MultiService m = new (); m.CreateOrder (); } } } public class MultiService { public void CreateOrder () { // Work with the instance Company.Service0X.Eso9PortClient // depending on the parameter in class Program Company.Service0X.Eso9PortClient Company.Service0X.Eso9PortClient x = new (); string xx = x.orders ... } } } Thank you in advance for your reply
Use an Abstract Factory pattern
-
Hello, There namespace Company.Service01, which contains classes and interfaces. There namespace Company.Service02, which contains classes and interfaces. Both spaces have the same structure of, contain the same classes and interfaces. I need to somehow ensure that I could work with class (for example: Eso9PortClient) from this namespace, depending on the input parameters of the program. It's possible there is some technique to it? The goal is that I did not have to repeat the same code n times the number of namespaces. Example: file01.cs namespace Company.Service0X { public interface Eso9Port {...} public partial class FaultInfo {...} public interface Eso9PortChannel {...} public partial class Eso9PortClient {...} } file02.cs namespace Company { class Program { static void Main (string [] args) { if (args [0] == "01") { MultiService MultiService m = new (); m.CreateOrder (); } else if (args [0] == "02") { MultiService MultiService m = new (); m.CreateOrder (); } else if (args [0] == "03") { MultiService MultiService m = new (); m.CreateOrder (); } } } public class MultiService { public void CreateOrder () { // Work with the instance Company.Service0X.Eso9PortClient // depending on the parameter in class Program Company.Service0X.Eso9PortClient Company.Service0X.Eso9PortClient x = new (); string xx = x.orders ... } } } Thank you in advance for your reply
Firstly, take out common code and put that in base classes in a base interface, and make sure that each implementation implements a common namespace. The main driver code shouldn't have to store references to any of the specific company namespaces. Then the problem simply comes down to creating an instance of the main object within one of the namespaces. Depending on how flexible you want your system to be, and whether you permit runtime plugins, you either want a hard-coded factory:
class MultiServiceFactory {
IMultiService Create(string name){
if(name == "01") return new Company.Service01.MultiService();
else if(name == "02") return new Company.Service02.MultiService();
// ... etc
else throw new ArgumentException("Can't make a service for "+name);
}
}... or you need to use reflection to look up the requested name ("Company.Service"+name+".ClassName") in either the current assemblies or loaded runtime plugin assemblies. I won't go into the entirety of plugin architecture in a Q&A reply, there are plenty of articles on CP about it.