Is there a way to control class coupling in C#

Posted on 2011-04-18
Last Modified: 2012-05-11
In part of my design I have two classes, A and B, that are both aware of a common data structure C (also a class).  While most of the contents of A and B are private or protected, there are some public members for various reasons. A and B are supposed to be completely decoupled - I would like to enforce a rule at compile time that says that A will not talk to B directly at all and vice versa. Logic using these two classes (or instances of these 2 classes) would be by a 3rd party generally using an instance of the common data structure C.  

I was wondering about a good way to express this rule in my code (the rule being that A and B are completely decoupled).  Would namespaces be good for this and/or is there another way?
Question by:MajorBigDeal
    LVL 29

    Expert Comment

    No, I believe different namespaces are not enough. It is possible to add reference to one of namespaces to another - and it's possible to refer to classes in this namespace. Of course, circular references are not allowed - namespace two cannot refer to the first one...

    Honestly, I can't see any way to do what you want (if I understand requirements properly).
    LVL 11

    Author Comment

    I should clarify that this code won't get used outside of some internal projects.  It would be similar reasons to why I use private members whenever possible. I could always change them to public if I wanted to but private clarifies my intentions and makes maintenance easier.

    So I was thinking that I could fairly easily check that there is no using statement but other than that how would I check that the 2 classes are not interdependent?  Isn't there some mechanism to address this? I was wondering what other programmers do in this situation or am I just dithering on stuff that is not important?
    LVL 29

    Accepted Solution

    Not sure what to advise.

    If I want (at design time) to check how my class or class method is used - I look for all references (right click "Find all references"). Then I can analyse how it's used.

    The idea to minimize public members can be good one. Say, classA and class be can reference through some class "mediator". Probably, this class can be used to set / call pablic methods on classA and classB, and, e.g., classA.Mediator is the only public property of classA (and class B). "Mediator" expose interface to deal with both classes and provide their communication.

    Anyway, hard to discuss because I can't imagine your specific needs...
    LVL 11

    Author Comment

    Your mediator suggestion is very interesting to me, although I don't think I fully understand it.  Let me try to do a better job of explaining what I am doing currently.  I am passing an instance of C to public methods of both A & B as a parameter.  Class A is also capable of generating an instance of C which might be subsequently passed into B (possibly after modification).   What I think you are suggesting is that class A & B could both be private inside class C - that would not work for me because class A and B are both used in other places.  
    LVL 10

    Assisted Solution

    The only way I can think of that will make this possible is to use a tool such as NDepend (commercial). It has a Code Query Language (CQL) that let's you ask questions to your codebase. You can use this to enforce certain architectural decisions.

    The way I would use it would be to incorporate NDepend in the build process.
    Note though, that this seems like overkill if it is a smaller project :)

    Good Luck!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now