深入了解 ABP vNext 框架(ABP-234)

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-13 06:07
下一篇 2024-11-13 06:07

相關推薦

  • Ojlat:一款快速開發Web應用程序的框架

    Ojlat是一款用於快速開發Web應用程序的框架。它的主要特點是高效、易用、可擴展且功能齊全。通過Ojlat,開發人員可以輕鬆地構建出高質量的Web應用程序。本文將從多個方面對Oj…

    編程 2025-04-29
  • Zlios——一個多功能的開發框架

    你是否在開發過程中常常遇到同樣的問題,需要不斷去尋找解決方案?你是否想要一個多功能、易於使用的開發框架來解決這些問題?那麼,Zlios就是你需要的框架。 一、簡介 Zlios是一個…

    編程 2025-04-29
  • agavi開發框架

    Agavi是一個基於MVC模式的Web應用程序開發框架,以REST和面向資源的設計為核心思想。本文章將從Agavi的概念、優點、使用方法和實例等方面進行詳細介紹。 一、概念 Aga…

    編程 2025-04-29
  • Python unittest框架用法介紹

    Python unittest框架是Python自帶的一種測試框架,可以用來編寫並運行測試用例。在本文中,我們將從以下幾個方面詳細介紹Python unittest框架的使用方法和…

    編程 2025-04-29
  • com.alipay.sofa.bolt框架

    com.alipay.sofa.bolt框架是一款高性能、輕量級、可擴展的RPC框架。其廣泛被應用於阿里集團內部服務以及阿里雲上的服務。該框架通過NIO支持高並發,同時還內置了多種…

    編程 2025-04-29
  • Django框架:從簡介到項目實戰

    本文將從Django的介紹,以及如何搭建Django環境開始,逐步深入到Django模型、視圖、模板、表單,最後通過一個小型項目實戰,進行綜合性的應用,讓讀者獲得更深入的學習。 一…

    編程 2025-04-28
  • LuaEP:一款強大的Lua開發框架

    LuaEP是一個集成了可以快速開發web應用程序所需的組件的Lua開發框架。它以Lua語言為基礎,提供了許多常用介面和庫,使得開發者不需要從頭開始編寫web應用程序,而是專註於業務…

    編程 2025-04-28
  • Java持久層框架的複合主鍵實現

    用Java持久層框架來操作資料庫時,複合主鍵是常見的需求。這篇文章將詳細闡述javax.persistence複合主鍵的實現方式,並提供完整的示例代碼。 一、複合主鍵的定義 複合主…

    編程 2025-04-27
  • AMTVV:一個全能的開發框架

    AMTVV是一個面向現代Web應用程序的全能開發框架,它可以讓你的工作更加高效。AMTVV能夠處理各種各樣的技術棧,包括但不限於React、Angular、Vue和TypeScri…

    編程 2025-04-27
  • Python語言的MVC框架

    本文將從以下幾個方面詳細闡述Python語言的MVC框架: 一、MVC框架的基本概念 一般而言,MVC框架被分為Model,View,Controller三部分。Model代表數據…

    編程 2025-04-27

發表回復

登錄後才能評論