The Most Commonly Used Design Patterns in .NET Development | Main- CQRS : Mastering
Tags: designpatterns, dotnet, backenddevelopment, programming
Published: 2024-02-29 18:17:30

**Design patterns in ASP.NET Core**, like in any software development, can be classified into different types based on their purpose and usage. Here are some common types of design patterns used in ASP.NET Core:

i.) **Creational Patterns**: These patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. Examples include:

- **Singleton**: Ensures a class has only one instance and provides a global point of access to it.
- **Factory Method**: Defines an interface for creating objects, but lets subclasses alter the type of objects that will be created.
- **Abstract Factor**y: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- **Builder Pattern**: Constructs complex objects step by step. It separates the construction of a complex object from its representation, allowing the same construction process to create different representations.



ii.) **Structural Patterns**: These patterns deal with object composition, i.e., they help in building large structures of objects. Examples include:

- **Adapter**: Allows objects with incompatible interfaces to collaborate.
- **Decorator**: Adds behavior to objects dynamically.
- **Facade**: Provides a simplified interface to a complex subsystem.
- **Bridge Pattern**: Decouples an abstraction from its implementation, allowing both to vary independently. It is often used when both the abstraction and its implementation need to be extensible.


iii.) **Behavioral Patterns**: These patterns are concerned with algorithms and the assignment of responsibilities between objects. Examples include:

- **Observer**: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- **Strategy**: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
- **Chain of Responsibility**: Avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
- **Command Pattern**: Encapsulates a request as an object, allowing for parameterization of clients with different requests, queuing of requests, and logging of requests.
- **State Pattern**: Allows an object to alter its behavior when its internal state changes, providing a more object-oriented way to manage state-dependent behavior.
- **Memento Pattern**: Captures and externalizes an object's internal state so that the object can be restored to this state later without violating encapsulation.

iV.) **Architectural Patterns**: These patterns are at a higher level of abstraction and deal with overall application architecture. Examples include:

- **Model-View-Controller (MVC)**: Separates an application into three main components: Model, View, and Controller.
- **Model-View-ViewModel (MVVM)**: Similar to MVC, but tailored for data binding in client-side applications.
- **Dependency Injection (DI)**: A technique where an object receives its dependencies from an external source rather than creating them itself, promoting loose coupling and easier testing.

- **[CQRS (Command Query Responsibility Segregation)](https://learn.microsoft.com/en-us/azure/architecture/patterns/cqrs)**: Separates the read and write operations of a data store into separate paths, improving scalability and performance.
Repository Pattern: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
Advantage:
- Independent Scaling.
- Optimizing Data Transfer object provides separation of 
 concern.
- High scalability.
- MediaTr pattern- Mediatr pattern is a behavioral pattern 
  that let us reduce dependencies between the object by 
  restricting direct communication between the object and the 
  forces them to collaborate only via a mediatr object.

- **Repository Pattern**: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. It provides a more object-oriented way to access data, decoupling the application from the specifics of the data storage implementation.
-The repository of the service used to access the data from 
 the database table.


v.) **Other Patterns:** 

- **Circuit Breaker Pattern**: Helps in handling faults that might take a variable amount of time to fix when connecting to a remote service or resource, preventing an application from repeatedly trying to execute an operation that's likely to fail.
- **Options Pattern**: Provides a way to access a set of related configuration settings, allowing for strongly typed access to configuration settings and simplifying the management of configuration data.