开发者

Conditional Namespace in Winform application

开发者 https://www.devze.com 2023-03-09 23:53 出处:网络
I want to know better way to define name-spaces in my projects. We have 2 different winform projects and there are certain code files and forms which we use in both project. So the structure of projec

I want to know better way to define name-spaces in my projects. We have 2 different winform projects and there are certain code files and forms which we use in both project. So the structure of project 1 and 2 is below:

//Project Pro1
//-------------------------------------
//Class C1 starts
#if pro1
using pro1
#endif
#if pro2
using pro2
#endif
namespace common_fun
{
Class C1
    Method M1
    {
        call to C2.M2
    }

}

//Class C2 starts
namespace pro1
{
Class C2
    Method M2

}

//Project Pro2
//----------------


#if pro1
using pro1
#endif
#if pro2
using pro2
#endif
namespace common_fun
{
Class C1
    Method M1
    {
        call to C2.M2
    }

}

namespace pro2
{
Class C2
    Method M2

}

So here, class C1 (under namespace common_fun) is shared file used in both projects. But in that we need to call method M2 of class C2 and for calling that method we need to write conditional using statements on top. i.e.

#if pro1
using pro1
#elseif pro2
using pro2
#endif

So my question is there any better way to include namespaces for common files? as in future there might be 3-4 projects that will us开发者_如何转开发e the same classes / forms.

Thanks.


I would make a common interface that contains your M2 method:

 interface ICommonFun
 {
      void M2();
 }

Then pass the implementation of that interface to your C1 class:

 class C1
 {
      ICommonFun Instance;

      public void M1()
      { 
         Instance.M2();
      }

      public C1(ICommonFun fun)
      {
          Instance = fun;
      }
  }


there are certain code files and forms which we use in both project.

If I understood you correctly, you reference those code files in both projects instead of putting them into a separate assembly. If you would have tried to put your "common classes" in such a library (as it is best-practice when writing reusable code), you would have noticed that the compiler/linker would not have let you do this: instead, it would have told you about what you really have here: a cyclic dependency between C1 and all variants of C2. That is something you should really avoid when creating code you want to share between different projects, because they make code hard to test and hard to maintain (you already noticed it, since your namespace problem is one of those typical problems which can occur in such a situation).

sacklpicka's solution (using an interface) is one way to solve this problem, making C1 ready to be put in it's own library. Another one is simply to pass M2 as a delegate in the constructor of C1, or give C1 an event which is triggered whenever M1 is called, so users of C1 can register their methods M2 as an event handler .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号