日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现

發(fā)布時間:2023/12/4 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

《在ASP.NET Core中使用Apworks快速開發(fā)數(shù)據(jù)服務》一文中,我介紹了如何使用Apworks框架的數(shù)據(jù)服務來快速構建用于查詢和管理數(shù)據(jù)模型的RESTful API,通過該文的介紹,你會看到,使用Apworks框架開發(fā)數(shù)據(jù)服務是何等簡單快捷,提供的功能也非常多,比如對Hypermedia的支持,以及提供豐富的異常信息和調用棧信息。另外,Apworks數(shù)據(jù)服務可以支持各種類型的倉儲(Repository)實現(xiàn),在該文的案例中,我使用了MongoDB作為倉儲的實現(xiàn),這是為了快速方便地演示數(shù)據(jù)服務的搭建過程。如果你所使用的是關系型數(shù)據(jù)庫,也沒有關系(意思是不要緊,不是說數(shù)據(jù)沒有關系。。。-_-!!!),基于Entity Framework Core的倉儲實現(xiàn)能夠滿足你的需求。

需要注意的一個問題

在以前老版本的Apworks中,倉儲的接口是支持饑餓加載的,也就是說,在延遲加載被啟用的時候,倉儲允許通過顯式指定一系列的對象屬性,當主對象被返回時,這些屬性所指向的子對象也會同時返回。這樣的設計在當時的場景下是合理的,因為是否需要加載某些屬性是可以在程序中指定的,對于類似MongoDB的倉儲實現(xiàn),它沒有延遲加載的概念,因此可以忽略這個參數(shù)。在Apworks數(shù)據(jù)服務中,由于倉儲的操作會直接被DataServiceController調用,而相關的查詢條件都是來自于RESTful API的,因此,很難在API的層面來確定某些聚合的對象屬性是否需要饑餓加載(Eager Loading)。另一方面,禁用延遲加載又會產(chǎn)生性能問題,因此,在當前版本的實現(xiàn)中,我還沒有考慮好用何種方式來解決這個問題。或許可以通過HTTP Header來指定需要饑餓加載的屬性路徑,但這是另一個問題。總之,在接下來的案例中,你將看到,雖然數(shù)據(jù)已經(jīng)添加成功,但在返回的結果里,被聚合的子對象將無法返回。我會設法解決這個問題。

案例:Customer Service

假設我們需要使用Entity Framework快速構建一個支持增刪改查操作的數(shù)據(jù)服務(Data Service),并希望該服務能夠在容器中運行,我們可以首先新建一個ASP.NET Core的應用程序,然后依照下面的步驟進行:

  • 向ASP.NET Core應用程序添加以下NuGet包引用:

    • Apworks.Integration.AspNetCore

    • Apworks.Repositories.EntityFramework

    • Microsoft.EntityFrameworkCore.SqlServer(在本案例中我們使用SQL Server,當然也可以使用PostgreSQL或者MySQL等)

    • Microsoft.EntityFrameworkCore.Tools

  • 新建領域模型,向ASP.NET Core應用程序中添加Customer和Address類:?


    public? class? Address { ???? public? Guid Id { get ; set ; } ???? public? string? Country { get ; set ; } ???? public? string? State { get ; set ; } ???? public? string? City { get ; set ; } ???? public? string? Street { get ; set ; } ???? public? string? ZipCode { get ; set ; } } public? class? Customer : IAggregateRoot<Guid> { ???? public? Guid Id { get ; set ; } ???? public? string? Name { get ; set ; } ???? public? string? Email { get ; set ; } ???? public? Address ContactAddress { get ; set ; } } <font face= "Calibri" ></font>
  • 新建一個DbContext類,用于指定數(shù)據(jù)庫的訪問方式,以及對模型對象/數(shù)據(jù)表結構進行映射:?


    public? class? CustomerDbContext : DbContext { ???? public? DbSet<Customer> Customers { get ; set ; } ???? protected? override? void? OnModelCreating(ModelBuilder modelBuilder) ???? { ???????? modelBuilder.Entity<Customer>() ???????????? .ToTable( "Customers" ) ???????????? .HasKey(x => x.Id); ???????? modelBuilder.Entity<Customer>() ???????????? .Property(x => x.Id) ???????????? .ForSqlServerHasDefaultValueSql( "newid()" ); ???????? modelBuilder.Entity<Customer>() ???????????? .Property(x => x.Name) ???????????? .IsUnicode() ???????????? .IsRequired() ???????????? .HasMaxLength(20); ???????? modelBuilder.Entity<Customer>() ???????????? .Property(x => x.Email) ???????????? .IsUnicode() ???????????? .IsRequired() ???????????? .HasMaxLength(50); ???????? modelBuilder.Entity<Address>() ???????????? .ToTable( "Addresses" ) ???????????? .HasKey(x => x.Id); ???????? modelBuilder.Entity<Address>() ???????????? .Property(x => x.Id) ???????????? .ForSqlServerHasDefaultValueSql( "newid()" ); ???????? base .OnModelCreating(modelBuilder); ???? } ???? protected? override? void? OnConfiguring(DbContextOptionsBuilder optionsBuilder) ???? { ???????? optionsBuilder.UseSqlServer( @"Server=localhost\sqlexpress; Database=CustomerService; Integrated Security=SSPI;" ); ???? } } <font face= "Calibri" ></font>
  • 打開Package Manager Console,并在解決方案資源管理器中將當前ASP.NET Core項目設置為啟動項目(注意:這一點非常重要)。然后依次執(zhí)行:?


    Add-Migration InitialCreate Update-Database

    成功完成這一步驟后,我們的數(shù)據(jù)庫就已經(jīng)準備好了。事實上,以上步驟都是開發(fā)一個Entity Framework Core應用程序所必須經(jīng)歷的標準步驟,目前還沒有用到Apworks的功能(當然,將Customer定義成聚合根除外)。接下來,我們開始實現(xiàn)并配置Apworks數(shù)據(jù)服務,接下來的步驟跟基于MongoDB的實現(xiàn)非常類似。?

  • 在ASP.NET Core應用程序的Controllers文件夾下,新建一個CustomersController,從DataServiceController繼承:?


    public? class? CustomersController : DataServiceController<Guid, Customer> { ???? public? CustomersController(IRepositoryContext repositoryContext) : base (repositoryContext) ???? { ???? } }


  • 打開Startup.cs文件,分別修改ConfigureServices和Configure方法,如下:?


    // This method gets called by the runtime. Use this method to add services to the container. public? void? ConfigureServices(IServiceCollection services) { ???? // Add framework services. ???? services.AddMvc(); ???? services.AddScoped<CustomerDbContext>(); ???? services.AddApworks() ???????? .WithDataServiceSupport( new? DataServiceConfigurationOptions(sp => ???????????? new? EntityFrameworkRepositoryContext(sp.GetService<CustomerDbContext>()))) ???????? .Configure(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public? void? Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ???? loggerFactory.AddConsole(Configuration.GetSection( "Logging" )); ???? loggerFactory.AddDebug(); ???? app.EnrichDataServiceExceptionResponse(); ???? app.UseMvc(); }


    與MongoDB實現(xiàn)不同的是,在使用EntityFrameworkRepositoryContext之前,我們需要通過services.AddScoped方法,將CustomerDbContext以Scoped生命周期注冊到IoC容器中,而在初始化EntityFrameworkRepositoryContext時,使用Service Provider的GetService方法進行Resolve,這樣就能確保每次HTTP請求完成時,資源都能成功釋放。?

  • 下面,我們來測試一下這個Apworks數(shù)據(jù)服務。在Visual Studio 2017中按下Ctrl+F5,直接運行ASP.NET Core應用程序,使用你喜歡的RESTful客戶端軟件,向/api/Customers進行POST操作,可以看到,Customer可以被成功創(chuàng)建,Customer Id即刻返回:

    讓我們再GET一下試試(注意:返回的ContactAddress是null,而事實上數(shù)據(jù)庫里是有值的。這里返回null的原因是因為我們沒有在Entity Framework中通過Include調用進行饑餓加載(Eager Loading),接下來會嘗試解決這個問題):

    除了ContactAddress在GET請求中返回為null之外,其它各種行為,包括數(shù)據(jù)服務所支持的API接口、調用方式等,都與之前MongoDB的實現(xiàn)完全相同。

    源代碼

    本文案例中的源代碼可以在Apworks Examples開源項目中找到。本案例的源代碼在Apworks.Examples.CustomerService.EntityFramework目錄下。

    總結

    本文帶領著大家一起預覽了Apworks數(shù)據(jù)服務對Entity Framework Core的支持,使得Apworks數(shù)據(jù)服務不僅可以使用MongoDB等NoSQL存儲方案,也可以使用關系型數(shù)據(jù)庫存儲方案,而且編程體驗也是幾乎相同的。這對于不同應用場景下微服務的實現(xiàn)是非常有幫助的。雖然在Entity Framework Core的實現(xiàn)中,目前有些瑕疵,但我會盡快解決這個問題。

    相關文章:?

    • 使用Angular 4、Bootstrap 4、TypeScript和ASP.NET Core開發(fā)的Apworks框架案例應用

    • 在ASP.NET Core中使用Apworks開發(fā)數(shù)據(jù)服務:對HAL的支持

    • 在ASP.NET Core中使用Apworks快速開發(fā)數(shù)據(jù)服務

    • NET Core中使用Irony實現(xiàn)自己的查詢語言語法解析器

    原文地址:http://www.cnblogs.com/daxnet/p/7157896.html


    .NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

    總結

    以上是生活随笔為你收集整理的在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。