Core modules provide essential functionality and utilities for ABP vNext applications.
DotCommon ABP adapter that provides Snowflake ID generation and other utilities.
dotnet add package SharpAbp.Abp.DotCommonAdd the module dependency to your module class:
[DependsOn(typeof(AbpDotCommonModule))]
public class YourModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// Module is automatically configured
}
}public class YourService
{
private readonly ISnowflakeIdGenerator _snowflakeIdGenerator;
public YourService(ISnowflakeIdGenerator snowflakeIdGenerator)
{
_snowflakeIdGenerator = snowflakeIdGenerator;
}
public long GenerateId()
{
return _snowflakeIdGenerator.NextId();
}
}Distributed unique ID generator for generating globally unique IDs.
dotnet add package SharpAbp.Abp.SnowflakesConfigure in your appsettings.json:
{
"Snowflakes": {
"WorkerId": 1,
"DataCenterId": 1
}
}Add the module dependency:
[DependsOn(typeof(AbpSnowflakesModule))]
public class YourModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
Configure<SnowflakesOptions>(options =>
{
options.WorkerId = configuration.GetValue<int>("Snowflakes:WorkerId");
options.DataCenterId = configuration.GetValue<int>("Snowflakes:DataCenterId");
});
}
}public class YourService
{
private readonly ISnowflakeIdGenerator _idGenerator;
public YourService(ISnowflakeIdGenerator idGenerator)
{
_idGenerator = idGenerator;
}
public long CreateUniqueId()
{
return _idGenerator.NextId();
}
public string CreateUniqueStringId()
{
return _idGenerator.NextId().ToString();
}
}Encryption and decryption module with SM2/AES support for data security.
dotnet add package SharpAbp.Abp.CryptoConfigure encryption settings in appsettings.json:
{
"Crypto": {
"DefaultAlgorithm": "AES",
"AES": {
"Key": "your-32-character-secret-key-here",
"IV": "your-16-char-iv"
}
}
}Add the module dependency:
[DependsOn(typeof(AbpCryptoModule))]
public class YourModule : AbpModule
{
}public class YourService
{
private readonly ICryptoService _cryptoService;
public YourService(ICryptoService cryptoService)
{
_cryptoService = cryptoService;
}
public string EncryptData(string plainText)
{
return _cryptoService.Encrypt(plainText);
}
public string DecryptData(string cipherText)
{
return _cryptoService.Decrypt(cipherText);
}
}FreeRedis cache library ABP adapter for Redis integration.
dotnet add package SharpAbp.Abp.FreeRedisConfigure Redis connection in appsettings.json:
{
"Redis": {
"Configuration": "127.0.0.1:6379,defaultDatabase=0,poolsize=50"
}
}Add the module dependency:
[DependsOn(typeof(AbpFreeRedisModule))]
public class YourModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
var redis = configuration["Redis:Configuration"];
Configure<FreeRedisOptions>(options =>
{
options.Configuration = redis;
});
}
}public class YourService
{
private readonly IDistributedCache<MyCacheItem> _cache;
public YourService(IDistributedCache<MyCacheItem> cache)
{
_cache = cache;
}
public async Task<MyCacheItem> GetOrCreateAsync(string key)
{
return await _cache.GetOrAddAsync(
key,
async () => await CreateItemAsync(),
() => new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30)
}
);
}
private async Task<MyCacheItem> CreateItemAsync()
{
// Create and return item
return new MyCacheItem();
}
}Data validation module for input validation and business rule validation.
dotnet add package SharpAbp.Abp.ValidationAdd the module dependency:
[DependsOn(typeof(AbpValidationModule))]
public class YourModule : AbpModule
{
}public class CreateUserDto : IValidatableObject
{
[Required]
[StringLength(50)]
public string UserName { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Range(18, 120)]
public int Age { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (UserName == "admin" && Email.EndsWith("@test.com"))
{
yield return new ValidationResult(
"Admin username cannot use test email domain",
new[] { nameof(UserName), nameof(Email) }
);
}
}
}AspNetCore integration extensions for ABP framework.
dotnet add package SharpAbp.Abp.AspNetCoreAdd the module dependency:
[DependsOn(
typeof(AbpAspNetCoreModule),
typeof(AbpAspNetCoreMvcModule)
)]
public class YourWebModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// Configure MVC
Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(YourApplicationModule).Assembly);
});
}
}// Auto API Controller
public class ProductAppService : ApplicationService, IProductAppService
{
// Automatically exposed as API: /api/app/product
public async Task<ProductDto> GetAsync(Guid id)
{
// Implementation
}
}Swagger/Swashbuckle API documentation support.
dotnet add package SharpAbp.Abp.SwashbuckleConfigure in your Web module:
[DependsOn(typeof(AbpSwashbuckleModule))]
public class YourWebModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Your API",
Version = "v1"
});
options.DocInclusionPredicate((docName, description) => true);
options.CustomSchemaIds(type => type.FullName);
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API v1");
});
}
}API versioning management for Swagger documentation.
dotnet add package SharpAbp.Abp.Swashbuckle.Versioning[DependsOn(typeof(AbpSwashbuckleVersioningModule))]
public class YourWebModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpSwaggerGenWithOAuth(
authority: "https://your-auth-server",
scopes: new Dictionary<string, string>
{
{"YourAPI", "Your API"}
},
options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Your API", Version = "v1" });
options.SwaggerDoc("v2", new OpenApiInfo { Title = "Your API", Version = "v2" });
}
);
}
}[ApiVersion("1.0")]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ProductController : AbpController
{
[HttpGet]
[MapToApiVersion("1.0")]
public async Task<ProductDtoV1> GetV1(Guid id)
{
// V1 implementation
}
[HttpGet]
[MapToApiVersion("2.0")]
public async Task<ProductDtoV2> GetV2(Guid id)
{
// V2 implementation with more features
}
}