0

Can some one tell me how can i map aggregation and composition in c#,considering the two classes STUDENT and LECTURER.

my question may be unclear.As an example,I used VS class designer to generate the Association relationship between classes.consider the 2 classes LECTURER and STUDENT_SOCIETY.Class Designer generates the following code. Blockquote

public class Lecturer { private string Name; private string Specialization; public StudentSociety StudentSociety { get { } set { } } } public class StudentSociety { private int Name; private string NoOfMembers; } 

So same way how can i model the aggregation and composition

3
  • 1
    Question still isn't clear. Are you asking: (1) what would a UML model look like for composition/aggregation? (2) what would the resulting code look like if the modelled relationship was composition/aggregation? Or something else? If it's (2) see @FixerMark's solution below. Commented Sep 16, 2010 at 19:31
  • As it stands there you have an aggregation relationship as StudentSociety is external to Lecturer which contains a facility to assign a Studentsociety object dynamically. To change it to a composite relationship you would need to make the StudentSociety member along with it's get/set function private and arguably, make the StudentSociety class a subclass of lecturer. (You should also call it something other than StudentSociety to avoid confusion/clashes with the Class of the same name) Commented Sep 16, 2010 at 19:46
  • The VS class designer is not a UML tool. It does not support aggregation or composition. Commented Oct 7, 2010 at 9:39

2 Answers 2

1

Typically composition is permanent and representated by wholly containing one class within another whereas aggregation involves some sort of assignment or link.

So, assuming the lecturer is responsible for a number of students composition would be something like:

Public class Lecturer { public class Student { string mName = ""; ... etc. 

Whereas aggregation could be:

Public class Lecturer { ... Some code ... } Public class Lecturer { Student mStudent; public void setStudent(Student theStudent) { mStudent = theStudent } ... etc. 

Let me know if I get a good mark for your homework... ;)

Sign up to request clarification or add additional context in comments.

2 Comments

typo - 1st line of aggregation example should be Public class Student (not Lecturer). Answer otherwise good. However - from a Domain perspective - I'd still question the notion of a lecturer "containing" or aggregating students (per comments above).
Quite right it should have been student - that'll teach me to cut and paste quickly.
1

More information needed. Do you want to take your domain model and map it into a UML diagram? Usually it happens the other way (diagram first, then generate the model as code objects). There isn't anything built into C# or "vanilla" Visual Studio to do this; however, MS Visio (part of "Professional"-level Office installs) can go from diagram to code and back, and integrates with VS to do this. Last I worked with Visio, it wasn't "pure" UML; the diagram symbols borrowed from a combination of diagramming schemes. However, it'll get you a readable graphic representation of your model, and changes you make there can be re-emitted as code objects.

As far as actually determining the relationship between these, it's generally many-to-many, with the unifying context for a useable one-to-many relationship being a "class" or "section" of a "course" or "subject". A single course has many sections. Each section has one lecturer and many students. The same student can attend multiple sections, and the same teacher can teach multiple sections. That means that both the lecturer and student can have a list of sections they teach/attend (if you need a "two-way" domain model where you can navigate in any direction around the object graph).

2 Comments

Visual Studio can generate Class Diagrams from code. They're similar to UML static diagrams.
Agree with @Keith. You don't mention the nature of the relationship(s) you're trying to capture - which is fundamentally important. Assuming it's along the lines of 'Lecturer teaches Student': (1) it's generally many:many (with some intermediate) and so (2) Aggregation/Composition aren't generally useful for this type of relationship.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.