9

I am thinking about creating a class diagram in Visual Studio. I notice that the Toolbox only appears to allow Association relationships between classes.

Is it possible to create stronger relationships in the class diagram, i.e. Aggregation and Composition relationships?

Also, is it possible to automatically create the class diagram from the code?

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
w0051977
  • 15,099
  • 32
  • 152
  • 329

1 Answers1

9

Great Question.

First of all: Visual Studio (VS) Class Designer is not a CASE Tools (like Enterprise Architect and etc.).
It is only a class representation of the source codes. Meaning that classes in class designer are always synchronized with the classes in the source code. If you delete some attributes or methods in source code, it synchronized with class designer and vise versa.

Secondly: The answer of this question hides in mapping Association and Aggregation (and Composition) to source code.

To map Associations, Aggregation and Composition to source code see: Reference 1 and Reference 2.

Detecting Association or Aggregation or Composition relationships between classes from the Source Code is impossible in some cases.

For example, what type of relationship is between Customer and Car in below code?

public class Customer {
    private String name;
    private String address;
    private String contactNumber;

    private Car car;        
}


public class Car {
    private String modelNumber;
    private Customer owner;
}

It can be Association or Aggregation.

Finally, because of first and second reasons as described above, Visual Studio Class Diagram do not have Aggregation and Composition.

Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
  • 1
    Thanks. +1 for the reference to CASE tools. – w0051977 Dec 20 '17 at 22:39
  • I have an interface and a class. The class implements all of the methods from the interface. How do I add a link between the class and the interface? – w0051977 Dec 20 '17 at 22:40
  • Also, say I have a class that contains an IEnumerable. On the class diagram there is a link between the class and the IEnumerable. How do I add a link between IEnumerable and Order? – w0051977 Dec 20 '17 at 22:41
  • @w0051977, Thanks, that is **Realization** relationship in UML between an Interface and a Class. **Note that** any class that implements an interface, SHOULD implements all methods of interface. – Gholamali Irani Dec 20 '17 at 22:45
  • How do I add the Realization relationship on the class diagram? I have already written the code. – w0051977 Dec 20 '17 at 22:47
  • You second question (Relationship between `IEnumerable` and `Order`) is very good question. please see [this reference](http://www.eclipse.org/modeling/mdt/uml2/docs/articles/Defining_Generics_with_UML_Templates/article.html) – Gholamali Irani Dec 20 '17 at 22:51
  • Which Tools are you using? Visual Studio Class Designer?? – Gholamali Irani Dec 20 '17 at 22:52
  • To add Realization see [this reference](https://msdn.microsoft.com/en-us/library/dd409416.aspx) It's so complete guidelines – Gholamali Irani Dec 20 '17 at 22:55
  • Yes, I am using Visual Studio Class Designer. – w0051977 Dec 20 '17 at 22:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161653/discussion-between-gholamali-irani-and-w0051977). – Gholamali Irani Dec 20 '17 at 23:02
  • Please have a look at my other question here if you get chance: https://stackoverflow.com/questions/47923225/showing-the-weakest-form-of-association-on-a-visual-studio-class-diagram – w0051977 Dec 21 '17 at 10:45
  • @Gholamali-Irani **"Realization relationship in UML between an Interface and a Class."** How do you think the relationship between `template class Foo{}` and `Foo`? – John Apr 20 '22 at 08:58