Dec 11 2023
public class UserRegistration { public void RegisterUser(string username, string password) { if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) { throw new ArgumentException("Username and password are required."); } if (password.Length < 8) { throw new ArgumentException("Password must be at least 8 characters long."); } // Proceed with registration } }
public class UserRegistration { public Result RegisterUser(string username, string password) { if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) { return Result.Failure("Username and password are required."); } if (password.Length < 8) { return Result.Failure("Password must be at least 8 characters long."); } // Proceed with registration return Result.Success(); } }
public class Result { public bool IsSuccess { get; private set; } public bool IsFailure { get; private set; } => !IsSuccess public string ErrorMessage { get; private set; } protected Result(bool isSuccess, string errorMessage) { IsSuccess = isSuccess; ErrorMessage = errorMessage; } public static Result Success() => new Result(true, null); public static Result Failure(string message) => new Result(false, message); }
public record Error(string Type, string Description) { public static readonly Error None = new(string.Empty, string.Empty); }
public static class RegistrationErrors { public static readonly Error UsernameAndPasswordRequired = new Error( "Registration.UsernameAndPasswordRequired", "Username and password are required."); public static readonly Error PasswordTooShort = new Error( "Registration.PasswordTooShort", "Password must be at least 8 characters long."); }
return Result.Failure(RegistrationErrors.PasswordTooShort);
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 18,000+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 18,000+ subscribers gaining practical tips and resources to enhance your .NET expertise.
Powered by EmailOctopus