Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Is there a way to control class coupling in C#

Posted on 2011-04-18
Medium Priority
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
  • 2
  • 2
LVL 30

Expert Comment

ID: 35421202
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

ID: 35421559
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 30

Accepted Solution

anarki_jimbel earned 1000 total points
ID: 35421580
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

ID: 35428783
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

oxyoo earned 1000 total points
ID: 35431748
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 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