AstreCode.Backend.Shared.Domain
9.0.0.3
dotnet add package AstreCode.Backend.Shared.Domain --version 9.0.0.3
NuGet\Install-Package AstreCode.Backend.Shared.Domain -Version 9.0.0.3
<PackageReference Include="AstreCode.Backend.Shared.Domain" Version="9.0.0.3" />
<PackageVersion Include="AstreCode.Backend.Shared.Domain" Version="9.0.0.3" />
<PackageReference Include="AstreCode.Backend.Shared.Domain" />
paket add AstreCode.Backend.Shared.Domain --version 9.0.0.3
#r "nuget: AstreCode.Backend.Shared.Domain, 9.0.0.3"
#:package AstreCode.Backend.Shared.Domain@9.0.0.3
#addin nuget:?package=AstreCode.Backend.Shared.Domain&version=9.0.0.3
#tool nuget:?package=AstreCode.Backend.Shared.Domain&version=9.0.0.3
AstreCode.Backend.Shared.Domain
Domain layer components for AstreCode microservices.
Description
The AstreCode.Backend.Shared.Domain
package provides essential domain layer components for building robust and scalable .NET 8.0 backend services. This package includes domain entities, business rules, validation utilities, and core domain interfaces.
Installation
To install this package, use the .NET CLI:
dotnet add package AstreCode.Backend.Shared.Domain
Or via Package Manager Console:
Install-Package AstreCode.Backend.Shared.Domain
Features
🏗️ Domain Entities
- Base Entity Classes: Common entity properties and behaviors
- Global Entity: Global identifier and tenant support
- Audit Entity: Created and modified tracking
- Full Audit Entity: Complete audit trail with user tracking
- Entity Framework Integration: Optimized for EF Core
🔧 Repository Pattern
- IBaseRepository Interface: Generic repository contract
- CRUD Operations: Create, Read, Update, Delete operations
- Query Support: Flexible querying capabilities
- Async Operations: Full async/await support
🛡️ Domain Exceptions
- NotFoundException: Resource not found scenarios
- UserFriendlyException: User-friendly error messages
- ForbiddenException: Access denied scenarios
- HTTP Status Integration: Proper HTTP status code mapping
✅ Validation Framework
- Check Utility Class: Common validation methods
- Phone Number Validation: International and Saudi phone validation
- Email Validation: Comprehensive email format validation
- Custom Validation: Extensible validation framework
👤 Current User Context
- ICurrentUser Interface: Current user information contract
- CurrentUser Implementation: Concrete user context implementation
- Permission Checking: Role and permission validation
- Tenant Support: Multi-tenant user context
📊 Data Transfer Objects
- PaginatedList: Pagination support for collections
- PagedInputDto: Input parameters for pagination
- Common DTOs: Shared data transfer objects
🌐 Localization
- JSON String Localizer: JSON-based localization
- Resource Management: Localized resource handling
- Culture Support: Multi-culture support
🔍 Framework Enums
- Common Enums: Shared enumeration types
- Status Enums: Common status values
- Type Enums: Common type definitions
Example Usage
Domain Entity
using Shared.Domain.Entities;
public class User : FullAuditEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public bool IsActive { get; set; }
}
Repository Pattern
using Shared.Domain.Entities;
public interface IUserRepository : IBaseRepository<User>
{
Task<User> GetByEmailAsync(string email);
Task<List<User>> GetActiveUsersAsync();
}
public class UserRepository : BaseRepository<User>, IUserRepository
{
public UserRepository(DbContext context) : base(context)
{
}
public async Task<User> GetByEmailAsync(string email)
{
return await GetQueryable()
.FirstOrDefaultAsync(u => u.Email == email);
}
public async Task<List<User>> GetActiveUsersAsync()
{
return await GetQueryable()
.Where(u => u.IsActive)
.ToListAsync();
}
}
Domain Exceptions
using Shared.Domain.Exceptions;
public class UserService
{
public async Task<User> GetUserAsync(Guid userId)
{
var user = await _userRepository.GetByIdAsync(userId);
if (user == null)
{
throw new NotFoundException("User not found");
}
return user;
}
public async Task DeleteUserAsync(Guid userId)
{
if (!_currentUser.HasPermission("Delete:User"))
{
throw new ForbiddenException("Insufficient permissions");
}
var user = await GetUserAsync(userId);
await _userRepository.DeleteAsync(user);
}
}
Validation
using Shared.Domain.Validation;
public class UserValidator
{
public void ValidateUser(User user)
{
if (string.IsNullOrEmpty(user.Email))
{
throw new UserFriendlyException("Email is required");
}
if (!Check.IsValidEmail(user.Email))
{
throw new UserFriendlyException("Invalid email format");
}
if (!string.IsNullOrEmpty(user.PhoneNumber))
{
if (!Check.SaudiPhoneNumber(user.PhoneNumber))
{
throw new UserFriendlyException("Invalid Saudi phone number");
}
}
}
}
Current User Context
using Shared.Domain.CommonData;
public class UserService
{
private readonly ICurrentUser _currentUser;
public async Task<List<User>> GetUsersAsync()
{
if (!_currentUser.HasPermission("Read:Users"))
{
throw new ForbiddenException("Access denied");
}
// Get users based on current user's tenant
return await _userRepository.GetQueryable()
.Where(u => u.TenantId == _currentUser.TenantId)
.ToListAsync();
}
}
Pagination
using Shared.Domain.Dtos;
public class UserService
{
public async Task<PaginatedList<User>> GetUsersAsync(PagedInputDto input)
{
var query = _userRepository.GetQueryable();
var totalCount = await query.CountAsync();
var users = await query
.Skip((input.PageNumber - 1) * input.PageSize)
.Take(input.PageSize)
.ToListAsync();
return new PaginatedList<User>(users, totalCount, input.PageNumber, input.PageSize);
}
}
Configuration
Entity Framework Configuration
// In your DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configure base entities
modelBuilder.Entity<User>().HasKey(u => u.Id);
modelBuilder.Entity<User>().Property(u => u.Id).ValueGeneratedOnAdd();
// Configure audit properties
modelBuilder.Entity<User>().Property(u => u.CreatedAt).IsRequired();
modelBuilder.Entity<User>().Property(u => u.ModifiedAt).IsRequired();
}
Localization Configuration
// In Program.cs
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "en-US", "ar-SA" };
options.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
});
Dependencies
This package depends on the following NuGet packages:
- Microsoft.EntityFrameworkCore (8.0.7) - Data access and ORM
- Microsoft.Extensions.Localization (8.0.7) - Localization support
- System.Linq.Dynamic.Core (1.4.6) - Dynamic LINQ queries
- JetBrains.Annotations (2024.2.0) - Code annotations
- Scrutor (4.2.2) - Dependency injection scanning
Requirements
- .NET 8.0 or later
- Entity Framework Core 8.0 or later
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
Support
For support and questions, please contact the AstreCode development team.
Changelog
See CHANGELOG.md for version history and changes.
AstreCode.Backend.Shared.Domain - Version 8.0.0
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- JetBrains.Annotations (>= 2024.2.0)
- Microsoft.EntityFrameworkCore (>= 9.0.8)
- Microsoft.Extensions.Localization (>= 9.0.8)
- Scrutor (>= 4.2.2)
- System.Linq.Dynamic.Core (>= 1.4.6)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on AstreCode.Backend.Shared.Domain:
Package | Downloads |
---|---|
AstreCode.Backend.Shared.Application
The shared application project for AstreCode backend |
|
AstreCode.Backend.Shared.UnitTest
The shared UnitTest project for AstreCode backend |
GitHub repositories
This package is not used by any popular GitHub repositories.