Rom.Domain
1.0.2
dotnet add package Rom.Domain --version 1.0.2
NuGet\Install-Package Rom.Domain -Version 1.0.2
<PackageReference Include="Rom.Domain" Version="1.0.2" />
<PackageVersion Include="Rom.Domain" Version="1.0.2" />
<PackageReference Include="Rom.Domain" />
paket add Rom.Domain --version 1.0.2
#r "nuget: Rom.Domain, 1.0.2"
#:package Rom.Domain@1.0.2
#addin nuget:?package=Rom.Domain&version=1.0.2
#tool nuget:?package=Rom.Domain&version=1.0.2
🧠 RomBaseDomain – Your Validatable Domain Base Class
Welcome, dev! 👋
This package helps you create cleaner and more robust domain models by handling validation in a smart way. It combines the power of C# DataAnnotations
with your own custom validation rules. The result? A single place to manage and check all domain-related validations. No more clutter. 🚀
💡 What’s This For?
RomBaseDomain
is an abstract base class designed for your domain models. By inheriting from it, you automatically get:
- ✅ Validation using
[Required]
,[Range]
, and other DataAnnotations - 🧠 Support for custom validation logic
- 📋 Friendly, serialized validation error lists for frontends or APIs
- 🛡️ Internal handling that won’t expose methods or internal validation when converting to JSON
It’s built with a naming convention:
- Prefixes like
List
for anything that's a list - Suffix
Domain
for domain models - Suffix
DataTransfer
for DTOs
🚀 How To Use It
1. Inherit From RomBaseDomain
You define your domain model like this:
public class UserDomain : RomBaseDomain
{
[Required]
public string Name { get; set; }
[Range(18, 120)]
public int Age { get; set; }
[EmailAddress]
public string Email { get; set; }
[StringLength(12, MinimumLength = 6)]
public string Username { get; set; }
[Phone]
public string PhoneNumber { get; set; }
[Url]
public string Website { get; set; }
[Compare("Password")]
public string ConfirmPassword { get; set; }
public string Password { get; set; }
}
2. Validate The Entity
var user = new UserDomain
{
Name = "", // Required field is empty
Age = 15, // Too young!
Email = "not-an-email", // Invalid format
Username = "usr", // Too short
PhoneNumber = "abc", // Invalid phone
Website = "not-a-url", // Invalid URL
Password = "123456",
ConfirmPassword = "654321" // Doesn’t match
};
bool isValid = user.IsValidDomain; // false
var errors = user.ListValidationError;
foreach (var error in errors)
{
Console.WriteLine($"Field: {error.Field} | Message: {error.Message}");
}
This will output all the field-level and global errors in a nice, serializable format.
3. Custom Validation
You can also add your own validation logic by overriding the Validate
method:
protected override void Validate()
{
if (string.IsNullOrEmpty(Username))
{
AddValidationError("Username", "Username cannot be empty.");
}
if (Password != ConfirmPassword)
{
AddValidationError("ConfirmPassword", "Passwords do not match.");
}
}
This will add custom validation errors to the list, which can be serialized and returned to the client.
4. Serialization
The validation errors are serialized in a way that’s easy to consume by frontends or APIs. You can use them directly in your response models.
public class ApiResponse
{
public bool Success { get; set; }
public List<ValidationError> Errors { get; set; }
}
public class ValidationError
{
public string Field { get; set; }
public string Message { get; set; }
}
5. Example API Response
{
"Success": false,
"Errors": [
{
"Field": "Name",
"Message": "The Name field is required."
},
{
"Field": "Age",
"Message": "The field Age must be between 18 and 120."
},
{
"Field": "Email",
"Message": "The Email field is not a valid e-mail address."
},
{
"Field": "Username",
"Message": "The field Username must be a string or array type with a minimum length of '6'."
},
{
"Field": "PhoneNumber",
"Message": "The PhoneNumber field is not a valid phone number."
},
{
"Field": "Website",
"Message": "The Website field is not a valid fully-qualified http, https, or ftp URL."
},
{
"Field": "",
"Message": "'ConfirmPassword' and 'Password' do not match."
}
]
}
This response can be easily consumed by any frontend framework or API client.
6. Notes
- The
ListValidationError
property is a list of all validation errors, both field-level and global. - The
IsValidDomain
property returnstrue
if there are no validation errors, andfalse
otherwise. - The
Validate
method is called automatically when you access theIsValidDomain
property, so you don’t need to call it manually. - The
AddValidationError
method is used to add custom validation errors to the list. You can use it to add any validation errors that are not covered by the DataAnnotations. - The
ValidationError
class is used to represent a validation error. It has two properties:Field
andMessage
. TheField
property is the name of the field that caused the validation error, and theMessage
property is the error message. - The
ValidationError
class is serializable, so you can use it directly in your API responses.
7. Conclusion
RomBaseDomain
is a powerful tool for managing domain validation in your C# applications. By using this package, you can ensure that your domain models are clean, maintainable, and easy to validate. It’s a great way to keep your code organized and reduce the risk of validation errors in your application.
📦 Installation
You can install the RomBaseDomain
package via NuGet:
dotnet add package RomBaseDomain
Or by using the NuGet Package Manager in Visual Studio.
Install-Package RomBaseDomain
🛠️ Contributing
We welcome contributions! If you have suggestions, bug reports, or feature requests, please open an issue or submit a pull request.
📄 License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Romulo Ribeiro
instagram: @romuloar
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Rom.Domain.Abstractions (>= 1.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.