In the last several tutorials we have gone through the process of identifying several classes that would facilitate a basic CRM application. During the identification phase, we determine business entities, create properties, and scope out methods. Next, we separate responsibilities with the goals of reducing coupling and making the code simpler. Now we reach the phase for establishing relationships between classes. The relationships define how objects work together to perform the work needed for the application to run.

Types of Relationships

There are many types of relationships in object-oriented programming. The first one we will look at is the collaboration relationship. In a collaboration relationship, you often refer to it as a “Uses A” relationship. This is because you can think of one class using another class. The next type of relationship we’ll look at is a composition relationship. A composition relationship can be referred to using a “Has A” relationship type. The idea of composition is that an object can be composed of other objects. An Order has a Customer. An Order also has a OrderItem. The last relationship type we will look at is Inheritance, or a “Is A” relationship. A CommercialCustomer is a Customer or a ConsumerCustomer is a Customer.

Collaboration

Here we have a diagram of a collaboration type relationship between classes. The OrderRepository “uses a” Order object to populate on a Retrieve and to serialize on a save. The same goes for the CustomerRepository “using a” Customer object and a ProductRepository “using a” Product object.

Composition

Composition is another key relationship type in object-oriented programming. A composition relationship exists when an object from one class, is made up of or composed of one or more objects from another class. It is also known as a “Has A” type relationship. In our CRM application this type of relationship exists between the Customer class and the Address class. A Customer “Has A” Address.

C# Relationships Between Classes
1.60 GEEK