Association Using C# and Other OO Concepts
Association Using C# and Other OO Concepts
We have two possible ways to create association lets you have two tables and want it to make
association using OOP concepts:
Associate Employees and Departments using objects. One easy concept in term of coding is:
One is to have a Department property in the Employee object and the different is to have a collection of Employee objects in
the Department object. You will also, at the minimal have both a series of all Department or all Employee objects.
The way you have it, if you have a series of all Department objects, which incorporate a series Employee objects, you can get all
Employee objects using LINQ.
What is LINQ: LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different
sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and
databases, as well as providing a single querying interface for different types of data sources.
Code sample
var allEmployees = Departments.SelectMany(d => d.EmployeeList);
Code sample
var employeeDepartment = Departments.FirstOrDefault(d => d.Employees.FirstOrDefault(e => e == employee) != null));
However, I think you will find it easier to have a Department property in the Employee class than to have a collection in the
Department class.
Note: Using id's for associations is more of a relational data concept, and you are in the object world with C#.
If you flesh out the above five-point requirement, we can easily visualize four relationships: -
Inheritance
Aggregation
Association
Composition
If you look at the first requirement (Manager is an worker of XYZ constrained corporation), it’s a Parent child relationship or
inheritance relationship. The sentence above specifies that Manager is a kind of employee, in other phrases we will have two
classes: parent class Employee, and a child class Manager which will inherit from the Employee class.
Note: The scope of this article is only restrained to aggregation, association, and composition.
Requirement 2: The Using relationship: Association
Is an interesting requirement (Manager uses a swipe card to enter XYZ premises)? In this requirement, the manager object and
the swipe card object use each other but they have their own object life time. In other words, they can exist without each
other. The most important point in this relationship is that there is no single owner.
The above design indicates how the SwipeCard classification uses the Manager class and the Manager type makes use of the
SwipeCard class. You can additionally see how we can create objects of the Manager class and SwipeCard type independently
and they can have their very own object lifestyles time and this relationship called the Association relationship
The child Worker objects can not belong to any other object. For instance, a Worker object cannot belong to a SwipeCard
object.
But… the Worker object can have its own life time which is completely disconnected from the Manager object. Looking from a
different perspective, it means that if the Manager object is deleted, the Worker object does not die.
This relationship is termed as an “Aggregation” relationship.
Requirements 4 and 5: The Death relationship: Composition
The last two requirements are actually logically one. If you read closely, the requirements are as follows:
Below is how the class formation will look like. You can also see that when I go to create the project object, it needs the
manager object.
This relationship is termed as the composition relationship. In this relationship, each objects are closely established on each
other. In different words, if one goes for garbage collection the different also has to be rubbish collected, or inserting from a
distinctive perspective, the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship.
This relationship is termed as the composition relationship. In this relationship, each objects are closely established on each
other. In different words, if one goes for garbage collection the different also has to be rubbish collected, or inserting from a
distinctive perspective, the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship.
Below is a visible representation of how the relationships have emerged from the requirements.