IsCoreBase 1.0.6
dotnet add package IsCoreBase --version 1.0.6
NuGet\Install-Package IsCoreBase -Version 1.0.6
<PackageReference Include="IsCoreBase" Version="1.0.6" />
<PackageVersion Include="IsCoreBase" Version="1.0.6" />
<PackageReference Include="IsCoreBase" />
paket add IsCoreBase --version 1.0.6
#r "nuget: IsCoreBase, 1.0.6"
#:package IsCoreBase@1.0.6
#addin nuget:?package=IsCoreBase&version=1.0.6
#tool nuget:?package=IsCoreBase&version=1.0.6
π Dependencies
- NET 8
- AutoMapper
- Entity Framework Core
Overview
This package provides a Class Library (DLL) that contains a ready-to-use Entities Layer and DbContext implementation, built on top of ASP.NET Core Identity and Entity Framework Core.
It is designed to help developers quickly set up user management, roles, OTP verification, and location entities (countries, cities, regions) without reinventing the wheel.
The library also provides base entities and contracts that can be reused or extended across different projects, making development faster and more consistent.
π§ What is included?
Db_BaseContext
A customizable database context that integrates with ASP.NET Core Identity and includes additional entities such as OTP, City, Country, and Region.Entities
- Users β
AppUser
,AppRole
,UserRole
,tbOTP
- Places β
tbCountry
,tbCity
,tbRegion
- Base Classes β
BaseEntity
,GuidBaseEntity
- Contracts β
IAppUser
,ISoftEntity
- Users β
Role Constants β Predefined roles through
Cnst_Roles
π― Key Features
- π Identity-ready: Built-in integration with ASP.NET Core Identity.
- π¦ Reusable Entities: Predefined models for users, roles, OTP, and locations.
- π§© Extensible: Easily extend
AppUser
,AppRole
, orBaseEntity
for custom business needs. - π Base Entities: Includes both
int
andGuid
primary key strategies. - π‘ Soft Delete Support: via
ISoftEntity
. - β‘ Faster Development: Eliminates repetitive setup for common entities and configurations.
π Use Cases
- Applications requiring user authentication and role management.
- Systems needing OTP verification (e.g., SMS/Email login confirmation).
- Apps that rely on location data (countries, cities, regions).
- Projects that want to standardize entities with base classes (
BaseEntity
,GuidBaseEntity
). - Multi-tenant or distributed systems needing GUID-based keys.
β Benefits
- Saves development time by providing ready-made entities and DbContext setup.
- Improves consistency across projects with shared contracts and base entities.
- Extremely flexible: you can use the entities as-is or inherit and extend them.
- Compatible with any ASP.NET Core project using EF Core.
Project Structure: iSCore
The iSCore
project is organized into several main layers.
Each layer has a specific responsibility and is designed to keep the solution modular, maintainable, and extensible.
π Project Tree
iSCore/
β
βββ DataBase
β βββ Entities # Core entity classes (User, Role, Country, City, Region, etc.)
β βββ Db_BaseContext.cs # Main EF Core DbContext with Identity integration
β βββ Seed.cs # Seeds initial data (roles, users, reference data)
β
βββ DAL
β βββ Interfaces
β β βββ IRepository.cs # Generic repository contract
β β βββ IUnitOfWork.cs # Unit of Work contract
β βββ GenericRepository.cs # Generic repository implementation
β βββ UnitOfWork.cs # Unit of Work implementation
β βββ UnitOfWorkResult.cs # Wrapper for repository operations result
β
βββ Services
β βββ BaseService.cs # Provides common CRUD and DTO mapping logic
β βββ MainServices.cs # Example of higher-level service
β βββ SrvResponse.cs # Standardized service response model
β β
β βββ DTO
β β βββ DtoBase.cs # Base class for Data Transfer Objects
β β
β βββ Interface # Service contracts
β β βββ IMainServices.cs
β β βββ IUsersBaseService.cs
β β
β βββ Mapper # AutoMapper integration
β β βββ AutoMapperService.cs
β β βββ MapConfig.cs
β β βββ MappingExtensions.cs
β β
β βββ Places # Services for location management
β β βββ Srv_Countries.cs
β β βββ Srv_Cities.cs
β β βββ Srv_Regions.cs
β β
β βββ Users # Services for user and role management
β βββ GSrv_Users.cs
β βββ UsersExt.cs
β
βββ Utils
β βββ Emails
β β βββ IEmailServices.cs # Email service contract
β β βββ EmailMessage.cs
β β βββ EmailResponse.cs
β β βββ SmtpEmailSender.cs
β β βββ SmtpSettings.cs
β β
β βββ Enums # System-wide enums
β β βββ FieldTypes.cs
β β βββ Gender.cs
β β βββ RequestType.cs
β β βββ ResponseCode.cs
β β βββ VisitStatus.cs
β β
β βββ SelectListHelper.cs # Helpers for dropdowns and lists
β βββ SetEntitesOpData # Utilities for setting default entity operation data
β
βββ ViewModels
βββ BasePage.cs # Base model for paginated responses
π’οΈ DataBase :
π Explanation of Db_BaseContext
The Db_BaseContext<TUser
, TRole
, TUserRole
> is a generic Entity Framework Core DbContext that extends the built-in IdentityDbContext.
It is designed to work with customized ASP.NET Core Identity models, allowing flexibility when defining your own User, Role, and UserRole classes.
πΉ Generic Version :
public class Db_BaseContext<TUser, TRole, TUserRole>:
IdentityDbContext<TUser, TRole, string,
IdentityUserClaim<string>,
TUserRole,
IdentityUserLogin<string>,
IdentityRoleClaim<string>,
IdentityUserToken<string>>
where TUser : IdentityUser
where TRole : IdentityRole
where TUserRole : IdentityUserRole<string>
β Key Points:
- Inherits from IdentityDbContext with a string as the primary key type.
- The generics (TUser, TRole, TUserRole) allow using custom Identity classes instead of the defaults.
- Provides flexibility for extending user, role, and relationship entities.
β Constructors
Both constructors allow dependency injection of DbContextOptions.
Supports configuration from the applicationβs startup (connection strings, providers, etc.).
public Db_BaseContext(DbContextOptions options) : base(options) { } public Db_BaseContext(DbContextOptions<Db_BaseContext<TUser, TRole, TUserRole>> options) : base(options) { }
β OnModelCreating
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserRole>(userRole =>
{
userRole.HasKey(ur => new { ur.UserId, ur.RoleId });
userRole.HasOne(ur => ur.Role)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
userRole.HasOne(ur => ur.User)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
}
- Overrides the default EF Core model building.
- Configures many-to-many relationships between User and Role through UserRole.
- Ensures:
- Composite key on (UserId, RoleId).
- Navigation properties (UserRoles) are properly mapped.
β DbSets
public DbSet<tbOTP> tbOTP { get; set; }
public DbSet<tbCity> tbCity { get; set; }
public DbSet<tbRegion> tbRegion { get; set; }
public DbSet<tbCountry> tbCountry { get; set; }
- These represent additional application-specific tables:
- tbOTP β Stores One-Time Passwords for authentication.
- tbCity, tbRegion, tbCountry β Store geographical data.
πΉ Non-Generic Version
public class Db_BaseContext : Db_BaseContext<AppUser, AppRole, UserRole>{
public Db_BaseContext(DbContextOptions options) : base(options) { }
public Db_BaseContext(DbContextOptions<Db_BaseContext> options) : base(options) { }
}
- This is a concrete implementation of the generic context.
- Uses specific entity classes:
- AppUser β Custom user entity.
- AppRole β Custom role entity.
- UserRole β Custom join entity for user-role relations.
- Allows using Db_BaseContext directly without specifying generic parameters.
β Summary
- Db_BaseContext<TUser, TRole, TUserRole> is a reusable base DbContext for ASP.NET Core Identity.
- Handles both Identity tables (users, roles, claims, logins, etc.) and custom tables (OTP, City, Region, Country).
- Provides generic flexibility for custom Identity implementations, and a ready-to-use non-generic version for standard cases.
Entities Layer Overview
The Entities Layer provides a set of pre-built models and contracts that developers can directly use or extend when building applications.
It is designed to cover core identity, user management, location data, and shared entity behaviors.
π Contracts
IAppUser
Defines the base contract for application users. It allows developers to extend their own user entity while maintaining consistency across the system.ISoftEntity
Provides a contract for entities that support soft delete. Instead of removing records from the database, entities can be marked as deleted while still being stored.
π Places
Entities representing geographical data that can be reused in any system needing location references.
- tbCity β Represents a city.
- tbCountry β Represents a country.
- tbRegion β Represents a region (linked to a country and possibly to cities).
These are useful for applications needing address management, regional settings, or location-based filtering.
π Users
Entities related to user and role management, fully compatible with ASP.NET Core Identity.
Const βΊ Cnst_Roles
Provides predefined constants for roles (e.g., Admin, User, Manager). Helps keep role names consistent across the system.AppRole
Extends the ASP.NET CoreIdentityRole
, allowing you to add custom role-related properties.AppUser
Extends the ASP.NET CoreIdentityUser
, allowing you to add custom user-related properties (e.g., FirstName, LastName).UserRole
Defines the relationship between users and roles (many-to-many). ExtendsIdentityUserRole<string>
.tbOTP
Represents a One-Time Password (OTP) entity for multi-factor authentication, email/SMS verification, or secure actions.
π Base Entities
BaseEntity
A base class for entities with common properties like:Id
(int)CreatedDate
UpdatedDate
IsDeleted
(if combined with ISoftEntity)
GuidBaseEntity
Similar toBaseEntity
, but uses a GUID as the primary key instead of an integer.
Useful for distributed systems or when a globally unique identifier is required.
π― Benefits
- Reusability: Developers can use the provided entities directly or extend them.
- Consistency: Shared contracts and base classes ensure uniform patterns across projects.
- Identity-ready: Full integration with ASP.NET Core Identity.
- Extendable: Flexible enough to allow customization for different project needs.
π Usage Example
1. Extending the AppUser
You can inherit from AppUser
to add custom user-related properties such as FirstName
and LastName
.
public class MyUser : AppUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
2. Creating an Entity from BaseEntity
For business-related tables, you can inherit from BaseEntity
to reuse common fields (Id
, CreatedDate
, UpdatedDate
).
public class Product : BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
3. Creating an Entity from GuidBaseEntity
For business-related tables, you can inherit from GuidBaseEntity
to reuse common fields (Id
, CreatedDate
, UpdatedDate
).
public class Product : GuidBaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
4. Using ISoftEntity
for Soft Delete
If you want soft delete support, implement ISoftEntity
.
public class Customer : BaseEntity, ISoftEntity
{
public string Email { get; set; }
public bool IsDeleted { get; set; }
}
DAl Layer Overview
IRepository<TEntity>
The IRepository<TEntity>
interface is a generic repository contract that provides a consistent way to perform CRUD operations, queries, and data access for any entity in the system.
It abstracts the database layer (via Entity Framework Core DbSet<TEntity>) and ensures a clean separation of concerns between the Data Access Layer (DAL) and the Business Layer.
π§© Key Features
- Works with any entity (
TEntity
) that is a class. - Provides CRUD operations (
Add
,Remove
,Update
). - Supports advanced querying with LINQ expressions.
- Allows pagination, sorting, and eager loading (
Include
). - Can execute raw SQL queries when needed.
π Members
π¦ DbSet
DbSet<TEntity> _db { get; }
Exposes the underlying EF CoreDbSet<TEntity>
for direct database operations.
β Add Operations
void Add(TEntity entity)
β Add a single entity.void AddRange(IEnumerable<TEntity> entities)
β Add multiple entities.
β Remove Operations
void Remove(int id)
β Remove an entity by integer ID.void Remove(TEntity entity)
β Remove an entity instance.void RemoveRange(IEnumerable<TEntity> entities)
β Remove multiple entities.
βοΈ Update Operations
void Update(TEntity entity, bool Noattach = true)
Updates an entity.Noattach=true
prevents EF from attaching the entity if already tracked.
π Get (Single Item)
TEntity Get(int id)
TEntity Get(string id)
TEntity Get(Guid id)
TEntity GetFirstOrDefault(predicate, includes)
β Fetches the first matching record.TEntity GetLastOrDefault(predicate, includes)
β Fetches the last matching record.TResult GetWithSelect(selector, predicate)
β Fetches a single projected result.
π Get All (Multiple Items)
IEnumerable<TEntity> GetAll()
β Returns all entities.IEnumerable<TEntity> GetAll(predicate, orderBy, includes)
β Filter, order, and include related entities.IEnumerable<TResult> GetAll(predicate, selector, orderBy, includes)
β Return projected results (DTOs, custom objects).
π Pagination
IEnumerable<TEntity> GetPage(page, pageSize, out count, predicate, orderBy, includes)
Returns paginated entities.IEnumerable<TResult> GetPage(selector, predicate, page, pageSize, out count, orderBy, includes)
Returns paginated and projected results.
β‘ Raw SQL
IQueryable<TEntity> GetFromSql(string sql, params object[] parameters)
Execute raw SQL queries for special cases.
π’ Count
int GetCount()
β Returns total count of entities.int GetCount(predicate)
β Returns filtered count.
β Benefits of Using IRepository
- Reusability β One repository works with all entities.
- Maintainability β Keeps data access logic consistent.
- Flexibility β Supports both LINQ queries and raw SQL.
- Testability β Easy to mock in unit tests.
- Clean Architecture β Business logic doesnβt directly depend on EF Core.
π‘ Example Usage
public class UserService
{
private readonly IRepository<AppUser> _userRepo;
public UserService(IRepository<AppUser> userRepo)
{
_userRepo = userRepo;
}
public IEnumerable<AppUser> GetActiveUsers()
{
return _userRepo.GetAll(u => u.IsActive);
}
public void AddUser(AppUser user)
{
_userRepo.Add(user);
}
public void DeleteUser(int id)
{
_userRepo.Remove(id);
}
}
UnitOfWork Overview
π IUnitOfWork Interface
β¨ Overview
The IUnitOfWork
interface represents the Unit of Work pattern.
It acts as a contract for managing Repositories and committing changes to the database in a consistent and organized way.
π Code
public interface IUnitOfWork : IDisposable
{
// Get repository for a specific entity
IRepository<T> repo<T>() where T : class;
// Commit changes synchronously
UnitOfWorkResult Save();
// Commit changes asynchronously
Task<UnitOfWorkResult> Saveasync();
}
π§© Members Explained
IDisposable Ensures proper release of resources (like the DbContext) after usage. Any class implementing this interface must implement the Dispose() method.
IRepository<T> repo<T>() where T : class
A generic method that returns the repository for the specified entity.
Example:
var userRepo = unitOfWork.repo<User>();
var productRepo = unitOfWork.repo<Product>();
UnitOfWorkResult
Save()
- Commits all pending changes to the database.
- Returns a UnitOfWorkResult object that contains:
- β Success status
- β Error message if something goes wrong
π UnitOfWorkResult
UnitOfWorkResult
is a simple result wrapper class that is used together with the IUnitOfWork
interface.
It provides feedback about the outcome of database operations such as Save()
or SaveAsync()
.
β¨ Properties
IsCompleted
(bool)
Indicates whether the operation was successful (true
) or failed (false
).Message
(string)
Holds an optional message, usually describing an error or additional information about the operation.innerObject
(object)
Can store the exception object or any additional details related to the failure.
π Relation with IUnitOfWork
When calling methods like:
var result = unitOfWork.Save();
- Task<UnitOfWorkResult> Saveasync()
- Asynchronous version of Save().
- Useful for high-load applications where async improves responsiveness and scalability.
Example:
var result = await unitOfWork.Saveasync();
π¦ Services Layer
Overview
The Services Layer provides a centralized place for implementing business logic and application workflows. It acts as a bridge between the data access layer (repositories & UnitOfWork) and the presentation layer (controllers, APIs, UI).
BaseService
The BaseService<TEntity, TDto,TKey>
is a generic service class that provides common CRUD operations and DTO mapping for any entity type.
public class BaseService<TEntity, TDto, TKey>
where TEntity : class, iSoftEntity<TKey>, new()
where TDto : class
Purpose
The BaseService class provides a generic service layer that simplifies CRUD (Create, Read, Update, Delete) operations.
It works with:
- Entities (TEntity)
- Data Transfer Objects (DTOs) (TDto)
- Primary Key type (TKey)
Key Features
- Abstracts repetitive CRUD logic.
- Uses IUnitOfWork + IRepository<TEntity> for data access.
- Integrates with AutoMapper (or custom mapping extensions).
- Returns responses wrapped in a consistent SrvResponse.
Main Methods
- GetItem(Guid id) β Retrieve a single entity and map to DTO.
- DeleteItem(Guid id, string userId) β Soft-delete or remove entity safely.
- SaveItem(TDto dto, string userId) β Add or update entity with audit info.
- GetAll() β Retrieve all non-deleted items.
- GetList(int PageIndex, int PageSize) β Paginated query.
SrvResponse
π¦ SrvResponse
The SrvResponse
class is a standardized response wrapper used across all services.
It ensures that service methods return consistent results regardless of success or failure, making error handling and data passing easier.
π Key Features
- Unified Response Format β Encapsulates data, messages, and status codes in one object.
- Error & Success Handling β Easily differentiate between successful and failed operations using the
IsOk
property. - Flexible Constructors β Supports multiple ways to initialize a response (with data, message, or status code).
- Additional Metadata β Allows attaching controller/action names, property names, or any extra object.
π οΈ Properties
Property | Type | Description |
---|---|---|
IsOk |
bool |
Indicates whether the response is successful (ResponseCode.OK ). |
_ResponseCode |
ResponseCode |
Status code of the operation (e.g., OK, InternalServerError, BadRequest). |
Message |
string |
A message describing the result (error or success). |
Controller |
string |
(Optional) Name of the controller handling the request. |
Actions |
string |
(Optional) Name of the action/method handling the request. |
Data |
object |
Main data returned by the operation (e.g., DTO, entity, list). |
AdditionalData |
object |
Extra metadata (e.g., total count for paged results). |
PropertyName |
string |
(Optional) The related property name, often used for validation errors. |
innerObject |
object |
(Optional) Holds additional internal objects for debugging or context. |
π Usage Examples
// Success response with data
var response = new SrvResponse(myDto);
// Error response with custom message
var response = new SrvResponse("An unexpected error occurred");
// Response with specific status code and message
var response = new SrvResponse(ResponseCode.BadRequest, "Invalid input provided");
// Response with property-specific error
var response = new SrvResponse(ResponseCode.BadRequest, "Email is required", "Email");
β Benefits
- Provides consistency across all service responses.
- Makes it easier to handle errors in a unified way.
- Supports flexible initialization for different scenarios.
- Works seamlessly with SrvResponseExt extension methods for Success, Error, and BadRequest.
π οΈ SrvResponseExt
The SrvResponseExt
class provides a set of extension methods for the SrvResponse
object.
It simplifies creating success, error, and bad request responses, and adds utility methods to retrieve data safely.
π Key Features
- Fluent API β Build responses in a clear and consistent way.
- Error Handling β Quickly generate error responses with messages and status codes.
- Data Extraction β Safely retrieve items, lists, or counts from a response.
- Null Safety β Ensures that a valid
SrvResponse
is always returned, even if the original object is null.
π οΈ Main Methods
Method | Description |
---|---|
Success() |
Returns a successful response (with optional data and additional metadata). |
Error(ResponseCode, string) |
Returns an error response with a custom code and message. |
Error(string, object?) |
Returns an error response with a message and optional inner object. |
BadRequest(string) |
Returns a bad request response with a message. |
_Return(ResponseCode, string, string?) |
Returns a response with custom code, message, and optional property name. |
IsOk() |
Checks if the response status is OK . |
GetItem<T>() |
Retrieves a single object if the response is successful. |
GetItems<T>() |
Retrieves a list of objects if the response is successful. |
GetItemsCount() |
Retrieves a count from AdditionalData if the response is successful. |
π Usage Examples
// Success with no data
var response = new SrvResponse().Success();
// Success with data
var response = new SrvResponse().Success(myDto);
// Success with data and count
var response = new SrvResponse().Success(myList, totalCount);
// Error with custom code
var response = new SrvResponse().Error(ResponseCode.NotFound, "Item not found");
// Error with default InternalServerError
var response = new SrvResponse().Error("Unexpected error occurred");
// Get a single item safely
var item = response.GetItem<MyDto>();
// Get a list of items safely
var items = response.GetItems<MyDto>();
// Get item with error message output
var item = response.GetItem<MyDto>(out string error);
β Benefits
Reduces repetitive boilerplate code when handling service responses.
Makes service results predictable and consistent across the application.
Provides safe casting for data retrieval.
Improves readability with a fluent API style.
Service Aggregator
IMainServices
Description
IMainServices
is a lightweight
service-aggregator interface that exposes domain services (Countries, Cities, Regions) as read-only properties.
It allows consumers (controllers, other services) to depend on a single entry point instead of injecting multiple domain services individually.
public interface IMainServices
{
Srv_Countries _SrvCountries { get; }
Srv_Cities _SrvCities { get; }
Srv_Regions _SrvRegions { get; }
}
β Properties
- _SrvCountries β Access to Srv_Countries (country-related business logic).
- _SrvCities β Access to Srv_Cities (city-related business logic).
- _SrvRegions β Access to Srv_Regions (region-related business logic).
Each property returns a service that encapsulates domain operations (CRUD, validation, queries) for its domain.
βοΈ Usage
You Can Inhert From It
public interface IMyMainService:IMainServices
{
// Add more domain services if needed
Srv_class _Srvclass { get; }
}
public class MyMainService : MainServices, IMyMainService
{
}
Use is
// DI registration (example)
services.AddScoped<IMyMainService, MyMainServices>();
// Inject and use in a controller
public class CountryController : ControllerBase
{
private readonly IMyMainService _mainServices;
public CountryController(IMainServices mainServices)
{
_mainServices = mainServices;
}
[HttpGet("countries")]
public IActionResult GetAllCountries()
{
var response = _mainServices._SrvCountries.GetAll();
return Ok(response);
}
}
Note: MainServices usually implements lazy initialization (creates each Srv_* only when first accessed), so injecting IMainServices is lightweight.
β¨ Benefits
- Simpler DI β inject one interface instead of many services.
- Cleaner constructors β reduces parameter clutter in controllers/services.
- Scalable β easy to add new domain services to the aggregator.
- Consistent access β centralizes how domain services are retrieved.
Identity User Management Service
π οΈ IUsersBaseService<TUser, TRole, TdtoUser>
The IUsersBaseService
interface defines a generic contract for managing users, roles, and authentication in an ASP.NET Core Identity-based application.
It centralizes all common user operations (create, update, authentication, password management, roles, etc.) while keeping them strongly typed with generics.
π Key Features
- User Management β Create, update, and fetch users by ID or username.
- Authentication β Sign in, sign out, and confirm emails.
- Roles Handling β Retrieve user roles or update them dynamically.
- Password Control β Change or force reset passwords.
- DTO Integration β Built-in mapping between entities and DTOs using
IMapper
. - Standardized Responses β Uses
SrvResponse
for consistent return values.
π οΈ Main Methods
Method | Description |
---|---|
EmailConfirmation(string) |
Confirms a user's email address. |
GetUser(ClaimsPrincipal) |
Retrieves the logged-in user from claims. |
GetUser(string) |
Retrieves a user by username. |
GetUserByID(string) |
Retrieves a user by unique ID. |
GetRoles(string) |
Returns a list of roles for the given user ID. |
GetRoles(ClaimsPrincipal) |
Returns roles for the current logged-in user. |
CreateUser(TdtoUser) |
Creates a new user from a DTO. |
GeTdtoUserById(string) |
Retrieves a DTO representation of a user by ID. |
GeTdtoUserByUserName(string) |
Retrieves a DTO representation of a user by username. |
SignInAsync(DtoUserLogin) |
Signs in a user with login credentials. |
SignOut() |
Signs out the current user. |
updateUser(TdtoUser) |
Updates user details. |
GetUsres(string, int, int) |
Retrieves paginated user results with optional search text. |
ChangePassword(Dto_ChangePassword) |
Changes the password for the current user. |
ForceChangePassword(string, string) |
Forces a password reset for a specific user. |
updateUserRole(string, string, string) |
Updates the role of a specific user. |
π Usage Example
public class UserController : ControllerBase
{
private readonly IUsersBaseService<AppUser, IdentityRole, DtoUser> _userService;
public UserController(IUsersBaseService<AppUser, IdentityRole, DtoUser> userService)
{
_userService = userService;
}
[HttpPost("create")]
public async Task<IActionResult> Create(DtoUser model)
{
var result = await _userService.CreateUser(model);
if (result.IsOk)
return Ok(result.Data);
return BadRequest(result.Message);
}
}
β Benefits
- Centralized user management with a single reusable contract.
- Works seamlessly with ASP.NET Core Identity.
- Reduces controller boilerplate by pushing logic to the service layer.
- Strongly typed generics allow customization for different user, role, and DTO implementations.
- Unified error/success handling using SrvResponse.
AutoMapper Integration
Overview
The AutoMapper
integration in the Services Layer provides a seamless way to map between Entities and Data Transfer Objects (DTOs).
It leverages the popular AutoMapper library to reduce boilerplate code and ensure consistent mapping logic across the application.
π οΈ AutoMapperService
The AutoMapperService
class provides a centralized static access point for the IMapper
instance.
It ensures that mapping operations across the application always use a single, consistent AutoMapper configuration.
π Key Features
- Global Mapper Access β Exposes a static
Mapper
property accessible anywhere in the application. - Centralized Initialization β Ensures
IMapper
is initialized once and reused everywhere. - Integration with Extensions β Works seamlessly with
MappingExtensions
to perform object-to-object mapping.
π οΈ Main Members
Member | Description |
---|---|
IMapper Mapper |
A static property that holds the globally available AutoMapper instance. |
Initialize(IMapper) |
Initializes the global AutoMapper instance with the provided configuration. |
π Usage Examples
// Initialize AutoMapper (e.g., during app startup)
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<User, UserDto>();
});
AutoMapperService.Initialize(mapperConfig.CreateMapper());
or if Using mapperCongig from MapConfig.cs
var mapper = app.Services.GetRequiredService<IMapper>();
AutoMapperService.Initialize(mapper);
// Access the global mapper anywhere
var dto = AutoMapperService.Mapper.Map<UserDto>(userEntity);
β Benefits
- Provides a single source of truth for AutoMapper usage.
- Simplifies dependency management by avoiding repeated mapper injection.
- Works as the backbone for mapping utilities like MappingExtensions.
π οΈ MappingExtensions
The MappingExtensions
class provides extension methods for mapping objects and collections using AutoMapper.
It simplifies object-to-object mapping by offering reusable methods for single items and lists.
π Key Features
- Single Item Mapping β Convert any object to a specified destination type.
- List Mapping β Convert a collection of objects into a list of the destination type.
- Validation β Throws an exception if the source object or list is
null
. - AutoMapper Integration β Uses a centralized
AutoMapperService.Mapper
instance.
π οΈ Main Methods
Method | Description |
---|---|
MapItem<TDestination>(this object) |
Maps a single object to the target type TDestination . |
MapList<TDestination>(this IEnumerable<object>) |
Maps a collection of objects to a list of TDestination . |
π Usage Examples
// Map a single object
var dto = userEntity.MapItem<UserDto>();
// Map a list of objects
var dtoList = userEntities.MapList<UserDto>();
β Benefits
- Reduces boilerplate mapping code.
- Makes use of AutoMapper in a clean and reusable way.
- Provides null-safety checks to avoid runtime issues.
- Enhances code readability and maintainability.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- AutoMapper (>= 15.0.0)
- Microsoft.AspNetCore.Identity (>= 2.3.1)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.