Enum-Centric Status Management: A Modern Solution for Centralized Error and Status Handling

Simplify your software’s error and status handling with a centralized, open-source solution. Learn how Enum-Centric Status Management enhances maintainability and reduces code redundancy.

Kürşat Solmaz
4 min readDec 8, 2024
Transform the way you handle errors and statuses: Simplify, centralize, and scale your software solutions.
Transform the way you handle errors and statuses: Simplify, centralize, and scale your software solutions.

Handling errors and statuses across different layers of a software project can be challenging. Repeated messages, inconsistent management, and complex structures often lead to code duplication and maintenance headaches.

Enum-Centric Status Management addresses these challenges effectively. By centralizing error and status messages using enums enriched with attributes, this approach enables developers to:

  • Standardize and centralize error and status messages.
  • Reduce code duplication across layers.
  • Maintain clean, scalable, and readable code.

This article explores the core concepts of Enum-Centric Status Management, provides real-world examples, and demonstrates how to integrate it into your projects using its open-source NuGet package.

Why Choose Enum-Centric Status Management?

This method introduces several key advantages:

  1. Centralized Management: Manage all error and status messages in a single enum structure, ensuring consistency across layers.
  2. Simplified Integration: Easily integrate this method into your projects with its ready-to-use NuGet package.
  3. Open-Source and Community-Driven: Hosted on GitHub, this project encourages contributions and collaborative improvements.
  4. Cleaner Codebase: Eliminate repetitive code while enhancing maintainability and readability.

Getting Started: Installing the NuGet Package

To get started, install the NuGet package via the .NET CLI:

dotnet add package EnumCentricStatusManagement  

Or, using the Package Manager Console:

PM> Install-Package EnumCentricStatusManagement

Real-World Use Case: Blog Management System

Let’s explore a practical example where Enum-Centric Status Management simplifies handling statuses in a blog management system.

Step 1: Define the Status Enum

Define an enum to centralize the messages for blog operations:

public enum BlogStatus
{
[Status("Blog successfully added.", StatusType.Success)]
Added = 0,

[Status("Blog successfully updated.", StatusType.Success)]
Updated = 1,

[Status("Main topic not found.", StatusType.Error)]
ParentTopicNotFound = 2
}

Each status code is paired with a message and a type (e.g., Success, Error), enabling structured result handling.

Step 2: Create a Detailed Stored Procedure

Define a SQL Stored Procedure that handles both database creation and blog management logic:

USE master;

-- Close open connections and delete the database
IF DB_ID('TestBlogDB') IS NOT NULL
BEGIN
ALTER DATABASE TestBlogDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE TestBlogDB;
END;

-- Create new database
CREATE DATABASE TestBlogDB;
GO

-- Use the new database
USE TestBlogDB;

-- Table of Topical Headings
CREATE TABLE MainTopics
(
Id INT PRIMARY KEY IDENTITY(1,1),
Title NVARCHAR(255) NOT NULL
);
GO

-- Blog Posts Table
CREATE TABLE BlogPosts
(
Id INT PRIMARY KEY IDENTITY(1,1),
Title NVARCHAR(255) NOT NULL,
Content NVARCHAR(MAX) NOT NULL,
MainTopicId INT NOT NULL,
FOREIGN KEY (MainTopicId) REFERENCES MainTopics(Id)
);
GO

-- Blog addition Stored Procedure
CREATE PROCEDURE InserOrUpdateBlogPost
@Id INT,
@Title NVARCHAR(255),
@Content NVARCHAR(MAX),
@MainTopicId INT,
@StatusCode INT OUTPUT
AS
BEGIN
-- MainTopicId validation
IF NOT EXISTS (SELECT * FROM MainTopics WITH(NOLOCK) WHERE Id = @MainTopicId)
BEGIN
SET @StatusCode = 2; -- No parent topic title found
RETURN;
END;

IF EXISTS (SELECT * FROM BlogPosts WITH(NOLOCK) WHERE Id = @Id)
BEGIN
-- Updated a blog post
UPDATE BlogPosts
SET Title = @Title, Content = @Content, MainTopicId = @MainTopicId
WHERE Id = @Id;

SET @StatusCode = 1; -- Blog successfully Updated
RETURN;
END
ELSE
BEGIN
-- Adding a blog post
INSERT INTO BlogPosts (Title, Content, MainTopicId)
VALUES (@Title, @Content, @MainTopicId);

SET @StatusCode = 0; -- Blog successfully added
RETURN;
END;
END;
GO
-- Add sample data
INSERT INTO MainTopics (Title) VALUES ('Technology');
INSERT INTO MainTopics (Title) VALUES ('Lifestyle');
GO

Step 3: Map Stored Procedure Results to Enum

Use the status codes returned by the stored procedure to map them to the corresponding enum values:

public class BlogService
{
private readonly SqlConnection _connection;

public BlogService(SqlConnection connection)
{
_connection = connection;
}

public BlogStatus AddOrUpdateBlogPost(int id, string title, string content, int mainTopicId)
{
var command = new SqlCommand("AddOrUpdateBlogPost", _connection)
{
CommandType = CommandType.StoredProcedure
};

command.Parameters.AddWithValue("@Id", id);
command.Parameters.AddWithValue("@Title", title);
command.Parameters.AddWithValue("@Content", content);
command.Parameters.AddWithValue("@MainTopicId", mainTopicId);

var statusCodeParam = new SqlParameter("@StatusCode", SqlDbType.Int) { Direction = ParameterDirection.Output };
command.Parameters.Add(statusCodeParam);

_connection.Open();
command.ExecuteNonQuery();
_connection.Close();

return (BlogStatus)(int)statusCodeParam.Value;
}
}

Step 4: Centralize Logging

Log all status messages using the centralized enum structure:

public void LogStatus(BlogStatus status)
{
var attribute = status.GetStatusAttribute();
Console.WriteLine($"[{DateTime.Now}] {attribute.Type}: {attribute.Message}");
}

This approach ensures consistent logging while keeping the messages manageable and extensible.

Key Benefits

  • Centralized Management: Reduces redundancy and simplifies maintenance.
  • Clean Code: Improves readability and standardizes error handling across the project.
  • Scalable and Extensible: Adapts easily to new use cases and evolving requirements.

Get Started Today

  • NuGet Package: Install the package to enable centralized error and status handling in your projects.
    🔗 EnumCentricStatusManagement on NuGet
  • GitHub Repository: Access the source code, contribute to the project, or explore detailed examples and documentation.
    🔗 GitHub Repository

Discover how Enum-Centric Status Management can simplify error handling, reduce redundancy, and improve your software’s maintainability. Try it in your next project and share your feedback or ideas to help improve this open-source solution! 🚀

--

--

No responses yet