https://github.com/mreshboboyev/decorator-pattern
A C# repository demonstrating the Decorator Pattern in .NET, showcasing its use for extending functionality dynamically, including examples of in-memory operations, logging, and service registration using both manual techniques and Scrutor.
https://github.com/mreshboboyev/decorator-pattern
c-sharp clean-code decorator-pattern dependency-injection design-patterns dotnet-core in-memory logging oop scrutor software-architecture
Last synced: about 1 year ago
JSON representation
A C# repository demonstrating the Decorator Pattern in .NET, showcasing its use for extending functionality dynamically, including examples of in-memory operations, logging, and service registration using both manual techniques and Scrutor.
- Host: GitHub
- URL: https://github.com/mreshboboyev/decorator-pattern
- Owner: MrEshboboyev
- Created: 2025-01-13T08:54:32.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-02T07:58:44.000Z (over 1 year ago)
- Last Synced: 2025-03-02T08:27:50.353Z (over 1 year ago)
- Topics: c-sharp, clean-code, decorator-pattern, dependency-injection, design-patterns, dotnet-core, in-memory, logging, oop, scrutor, software-architecture
- Language: C#
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π¨ Decorator Pattern in .NET
This repository provides an in-depth implementation of the **Decorator Pattern** in **.NET C#**. The Decorator Pattern is a structural design pattern that allows behavior to be added to an individual object dynamically, without affecting the behavior of other objects of the same class.
In this project, you'll explore:
- Manual and **Scrutor**-based service registration for decorators.
- Practical examples including **logging** and **in-memory operations**.
- Clean and maintainable code adhering to **OOP principles**.
## π Features
### Core Concepts
- **Decorator Pattern**: Dynamically add functionality to objects without altering their structure.
- **Manual and Scrutor-based Registration**: Learn two approaches for setting up decorators in .NET.
- **Practical Use Cases**: Logging, caching, and in-memory operations.
### Tools and Libraries
- **Scrutor**: A library for assembly scanning and automatic decorator registration in .NET.
- **Dependency Injection (DI)**: Utilize .NETβs built-in DI container to manage service lifetimes and decorators.
## π Repository Structure
```
π¦ src
β£ π DecoratorPattern # Core implementation of the pattern
β£ π WithoutDecoratorPattern # Core implementation without pattern
```
## π Getting Started
### Prerequisites
Ensure you have the following installed:
- .NET Core SDK
- A modern C# IDE (e.g., Visual Studio or JetBrains Rider)
### Step 1: Clone the Repository
```bash
git clone https://github.com/MrEshboboyev/decorator-pattern.git
cd decorator-pattern
```
### Step 2: Run the Project
```bash
dotnet run --project src/DecoratorPattern
```
### Step 3: Explore the Code
Navigate through the codebase to see the decorator pattern in action with logging, caching, and service registrations.
## π Code Highlights
### Manual Registration Example
```csharp
services.AddTransient();
services.AddTransient(provider =>
{
var original = provider.GetRequiredService();
return new LoggingDecorator(original);
});
```
### Scrutor-Based Registration Example
```csharp
services.Scan(scan => scan
.FromAssemblyOf()
.AddClasses(classes => classes.AssignableTo())
.AsImplementedInterfaces()
.WithTransientLifetime()
.Decorate());
```
### Decorator Example: Logging
```csharp
public class LoggingDecorator : IService
{
private readonly IService _innerService;
public LoggingDecorator(IService innerService)
{
_innerService = innerService;
}
public void PerformOperation()
{
Console.WriteLine("Logging: Before operation.");
_innerService.PerformOperation();
Console.WriteLine("Logging: After operation.");
}
}
```
## π Use Cases
### 1. Logging Decorator
Logs messages before and after an operation is performed.
### 2. Caching Decorator
Stores results of expensive operations in memory for faster subsequent access.
### 3. In-Memory Data Processing
Demonstrates in-memory operations to showcase how decorators can extend functionality dynamically.
## π Why This Project?
1. **Learn Design Patterns**: Understand the practical use of the decorator pattern in modern software.
2. **Clean and Extensible Code**: Build solutions that adhere to OOP principles and are easy to extend.
3. **Real-World Examples**: Explore logging and caching as practical applications of decorators.
4. **Modern Tools**: Learn how to leverage **Scrutor** for simplified registration.
## π About the Author
This project was developed by [MrEshboboyev](https://github.com/MrEshboboyev), a software developer passionate about clean code, design patterns, and scalable architectures.
## π License
This project is licensed under the MIT License. Use it to enhance your projects or learn more about design patterns in .NET.
## π Tags
C#, .NET, Decorator Pattern, Design Patterns, Scrutor, Dependency Injection, Logging, In-Memory, Software Architecture, OOP, Clean Code
---
Feel free to suggest new features, raise issues, or fork the project to contribute! π