f7q / learning

NET Core learning Github
Other
0 stars 1 forks source link

ASP.NET Coreの学習 #46

Open f7q opened 7 years ago

f7q commented 7 years ago

基礎技術の強化

f7q commented 6 years ago

ASP.NET CoreはMVCパターンであり、MVVMパターンは適応し難い

追記

f7q commented 6 years ago

CancellationTokenとasync TaskでWeb APIはベストプラクティスらしい。

f7q commented 6 years ago

WPFのDataGridと連携するにはこれが使えそう

f7q commented 6 years ago

IHttpContextAccessorのモックの作り方

以下のような感じで使用する。

using Moq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Authentication;

{
  var authenticationManagerMock = new Mock<AuthenticationManager>();
  var httpContextMock = new Mock<HttpContext>();
  httpContextMock.Setup(x => x.Authentication).Returns(authenticationManagerMock.Object);
  var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
  httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContextMock.Object);
  httpContextAccessorMock.Setup(x => x.HttpContext.User.Identity.Name).Returns("Siddhartha");
  var httpContextMockObject = httpContextAccessorMock.Object;
  // services.AddScoped(x => httpContextAccessorMock);
}
f7q commented 6 years ago

Program#Main

アプリケーション本体への処理

StartUp

StartUp#ConfigureServicesメソッド

DIコンテナの注入を行う箇所 デフォルトのコンテナだと上書きなので、トラブルになることは少ない。

service.AddDBContext();
service.AddMvc();

など、必要になるクラスの注入を行う。

StartUp#Configureメソッド

ミドルウェア層の処理を定義する箇所 app.UseMvc(); など パイプラインの流れを意識して定義しないと、上手く機能しない。 各バージョンでも仕様は大きく変更されていないため、ASP.NET Coreの中核となる仕様として押さえるべき箇所。