Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Working with C # classes from different namespaces

C# Working with C # classes from different namespaces

Scheduled Pinned Locked Moved C#
csharptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lada_vyvojar
    wrote on last edited by
    #1

    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

    V B 2 Replies Last reply
    0
    • L lada_vyvojar

      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

      V Offline
      V Offline
      Vladimir Kniazkov
      wrote on last edited by
      #2

      Use an Abstract Factory pattern

      1 Reply Last reply
      0
      • L lada_vyvojar

        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

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups