ABP vNext 框架(ABP-234)是一個全棧企業級應用程序開發框架,由微軟 MVP 隊員尤雨溪和眾多志願開發者共同開發。該框架擁有全面而豐富的功能,並且易於使用和擴展。本文將從多個方面詳細闡述 ABP vNext 框架的特點和使用,包括模塊化設計、依賴注入、實體框架集成、身份認證和授權、前端開發支持等。
一、模塊化設計
ABP-234框架採用模塊化設計,將應用程序的各個部分封裝在不同的模塊中,便於開發、測試和維護。每個模塊都具有相應的命名空間、依賴關係和配置文件,使用者可以根據自己的需求自由地選擇和組合模塊。ABP-234框架內置了眾多的模塊,如日誌記錄模塊、應用程序設置模塊、身份認證模塊等等,用戶也可以自行開發和集成第三方模塊。此外,ABP-234框架還提供了豐富的工具和 API,使得開發者可以方便地管理和部署模塊。
// 示例:創建一個自定義模塊
[DependsOn(typeof(AbpEntityFrameworkCoreModule))]
public class MyModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure(options =>
{
options.UseSqlServer();
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
context.ServiceProvider.GetService<ILogger<MyModule>>().LogInformation("MyModule initialized.");
}
}
二、依賴注入
ABP-234框架內置了一個輕量級的依賴注入容器,可以方便地管理應用程序中的對象和服務。該容器可以自動解決對象的依賴關係,支持對象池、生命周期管理等高級配置。用戶可以在啟動時註冊依賴項、對象或服務,並在運行時獲取它們。
// 示例:註冊服務
public class MyService : ITransientDependency
{
public void DoSomething()
{
Console.WriteLine("Hello, World!");
}
}
public class MyController : Controller
{
private readonly MyService _myService;
public MyController(MyService myService)
{
_myService = myService;
}
public IActionResult Index()
{
_myService.DoSomething();
return View();
}
}
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
public class MyAspNetCoreModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddTransient<MyService>();
}
}
三、實體框架集成
ABP-234框架集成了 Entity Framework Core 來處理數據訪問層。該框架提供了強大的 CRUD 操作、查詢表達式、緩存等功能,並且支持多種資料庫引擎。用戶可以使用 LINQ 或者原始 SQL 來查詢數據。框架內置了各種數據過濾器,包括軟刪除支持、多租戶支持等等。
// 示例:實體框架集成
public class MyDbContext : AbpDbContext<Tenant, Role, User, MyDbContext>
{
public DbSet<TestEntity> TestEntities { get; set; }
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TestEntity>().Property(e => e.Name).HasMaxLength(64);
}
}
public class TestEntity : FullAuditedEntity
{
public string Name { get; set; }
}
[DependsOn(typeof(AbpEntityFrameworkCoreModule))]
public class MyModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
Configure<AbpDbContextOptions>(options =>
{
options.UseSqlServer(configuration.GetConnectionString("Default"));
});
}
}
四、身份認證和授權
ABP-234框架為用戶身份認證和授權提供了全面的支持,並且提供了簡單易用的身份認證 API。該框架支持多種身份認證方案,如基於 JWT、IdentityServer4、Google、Facebook 和 Microsoft 的身份認證方案。用戶可以使用預定義的身份驗證器或者自定義身份驗證器。該框架還支持授權策略和聲明,可以方便地控制用戶的訪問許可權。
// 示例:身份認證和授權
[Authorize]
public class MyController : Controller
{
private readonly IMyAppService _myAppService;
public MyController(IMyAppService myAppService)
{
_myAppService = myAppService;
}
[HttpGet("{id:int}")]
public async Task<TestEntity> GetTest(int id)
{
return await _myAppService.GetTestByIdAsync(id);
}
}
public class MyAppService : ApplicationService
{
private readonly IRepository<TestEntity> _testRepository;
public MyAppService(IRepository<TestEntity> testRepository)
{
_testRepository = testRepository;
}
public async Task<TestEntity> GetTestByIdAsync(int id)
{
var entity = await _testRepository.GetAsync(id);
return entity;
}
}
[DependsOn(
typeof(AbpIdentityServerDomainModule),
typeof(AbpAccountHttpApiModule),
typeof(MyModule)
)]
public class MyIdentityServerModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<IdentityServerAuthenticationOptions>(options =>
{
options.Authority = "https://localhost:5001";
options.ApiName = "myapi";
options.RequireHttpsMetadata = false;
});
PreConfigure<JwtBearerOptions>(options =>
{
options.Authority = "https://localhost:5001";
options.Audience = "myapi";
options.RequireHttpsMetadata = false;
});
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
Configure<AbpJwtSettings>(options =>
{
options.Issuer = configuration["Authentication:JwtBearer:Issuer"];
options.Audience = configuration["Authentication:JwtBearer:Audience"];
options.SigningKey = configuration["Authentication:JwtBearer:SigningKey"];
});
Configure<AbpIdentityServerOptions>(options =>
{
options.ConfigureClients = ConfigureClients;
options.ConfigureApiResources = ConfigureApiResources;
options.ConfigureIdentityResources = ConfigureIdentityResources;
});
}
private void ConfigureClients(List<IdentityServer4.Models.Client> clients)
{
clients.Add(new IdentityServer4.Models.Client
{
ClientId = "myclient",
ClientName = "My Client",
AccessTokenLifetime = 3600,
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris =
{
"https://localhost:5002/signin-oidc"
},
PostLogoutRedirectUris =
{
"https://localhost:5002/signout-callback-oidc"
},
AllowedScopes =
{
"openid",
"profile",
"email",
"myapi"
},
RequireConsent = false
});
}
private void ConfigureApiResources(List<IdentityServer4.Models.ApiResource> apis)
{
apis.Add(new IdentityServer4.Models.ApiResource("myapi", "My API"));
}
private void ConfigureIdentityResources(List<IdentityServer4.Models.IdentityResource> resources)
{
resources.AddRange(new[]
{
new IdentityServer4.Models.IdentityResources.OpenId(),
new IdentityServer4.Models.IdentityResources.Profile(),
new IdentityServer4.Models.IdentityResources.Email()
});
}
}
五、前端開發支持
ABP-234框架提供多種前端開發支持,包括 Razor 頁面、Angular、Vue 等框架。該框架集成了對開發人員友好的響應式設計和國際化支持。開發者可以使用本地化 API 快速創建多語言 UI。
// 示例:前端開發支持
<div>
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
</div>
[AutoValidateAntiforgeryToken]
public async Task<JsonResult> Create(CreateInputDto input)
{
await _testAppService.CreateAsync(input);
return Json(new { success = true });
}
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
public class MyAspNetCoreModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(MyModule).Assembly, setting =>
{
setting.RootPath = "api/MyApp";
});
});
}
}
通過以上闡述,我們可以看出,ABP vNext框架(ABP-234)具有很多優秀的功能和特點,無論是在模塊化設計、依賴注入、實體框架集成、身份認證和授權甚至是前端開發支持方面,都有著出色的表現。ABP vNext框架在實際開發中,能夠大幅提升開發效率和代碼質量,推薦使用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/152964.html