Inversion of Control - Dependency Injection - Dependency Inversion Principle

inversion of control, dependency injection and dependency inversion principle

  • Direct or traditional approach: Object creates its own dependencies.

  • Inverted approach (inversion of control): Dependencies are created outside the object. This responsibility is given to someone else.

Example 1 (Direct approach):

We have a service named EmailService which is an implementation of INotificationService.

public interface INotificationService
{
    void SendNotification();
}

public class EmailService : INotificationService
{
    public void SendNotification()
    {
        // code to send notification 
    }
}

OrderService need to use the INotificationService.

[Read More]

Understanding Aggregation: How It Differs from Composition

Aggregation in oops. Aggregation vs Composition.

Aggregation is when an object is made up of one or more objects, but those objects can live outside the main object. It defines the has-a relationship.

Note: In the aggregation, if main object is destroyed, its aggregated objects may still live.

Example #1: A Library is an aggregation of Books. Books can live without the library.

Example #2: A Department is an aggregation of Employees. If department closes, the Employees can exist on their own.

[Read More]

Composition: What, Why And When?

what is composition?

Composition is when your class is composed of one or more objects from other classes. A simple example is a Car composed of Engine and Wheel objects. Inheritance defines an “is-a” relationship, while composition defines a “has-a” relationship.

For example, Dog is-a Animal, and Circle is-a Shape. It defines inheritance.

This makes sense because you want to reuse all the features of Animal (eat, sleep, walk, makeNoise) in the Dog class. Cow is-a Animal too, so Cow can also inherit the Animal class. Since cows and dogs make different noises and eat different things, we have to redefine or override the makeNoise() and eat() methods in the Dog and Cow classes. The point is when we want to reuse the functionality of a class and also want subtype polymorphism (Dog and Cow are subtypes of Animal), then we must use inheritance.

[Read More]

Open Closed Principle (OCP)

open closed principle in c#

The Open-Closed Principle is one of the five SOLID principles. It states that a class should be open for extension but closed for modification. In other words, you should be able to add new functionality without changing existing code.

Once you have written your class, it should not be modified in the future. However, this doesn’t mean you cannot modify your class during early development stages. It means that once your application or software is in production and you want to introduce the new functionality, you should extend that class rather than modifying it.

[Read More]

Polymorphism in depth with C#

polymorphism in csharp

Polymorphism is one of the core concept of object oriented programming. Word polymorphism = poly (many) + morphism (forms). As its name suggesting polymorphism means, an entity can have multiple forms.

📢 Updated and refinded at : 21-feb-2025

Other oops core concepts :

Let’s learn polymorphism through mistakes.

Jim requires an entry system for his pet shop, which exclusively houses various breeds of dogs. This system will manage the entry and records of all dogs entering and exiting the premises.

[Read More]

Inheritance in C#

inheritance csharp

📢 Updated and refinded at : 21-feb-2025

Inheritance is a fundamental concept in OOPs that allows a class to inherit properties and behaviors of another class. We have two key terms are here base class/superclass and derived class/subclass.

  • Base class / super-class: Whose members and functionality are inherited (Giver).
  • Derived class / sub-class: Who is inheriting the members (Taker)

📺Other oops concepts:

Syntax of inheritance:

[Read More]

Encapsulation in C#

encapsulation in c#

📢 Updated and refinded at : 21-feb-2025

Bundling the data member and member function into a single unit is called encapsulation. Remember the term “capsule”. We put all the medicine inside a wrapper and call it capsule. Similarly, wrap the data members and member functions together is called encapsulation.

Now we need to understand few terms.

  • Data members : Attributes or properties (eg. name, age)
  • Member functions: Methods (PrintDetails)

👉 We wrap up the data members (attributes or properties) and member functions (methods) into a single unit (class).

[Read More]

Abstraction in C#

abstraction in oops c#

📢 Updated and refinded at : 21-feb-2025

Abstraction allows you to focus on the relevant details of an object while ignoring unnecessary complexities.
This is achieved by defining a simplified representation of an object that hides the implementation details and exposes only the necessary features.
In practical terms, abstraction is implemented through abstract classes and interfaces in languages like C#.

📺Other OOPs related articles :

Let’s implement abstraction by using interface. In the real world projects, we use interfaces more often.

[Read More]