内容へ移動
猫型iPS細胞研究所
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
INDEX
»
dotnet
»
リポジトリパターン
トレース:
dotnet:リポジトリパターン
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== リポジトリパターン ====== <code c# "Program.cs"> using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Repo.Data; using Repo.DAL; var builder = WebApplication.CreateBuilder(args); builder.Services.AddDbContext<RepoContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("RepoContext") ?? throw new InvalidOperationException("Connection string 'RepoContext' not found."))); builder.Services.AddScoped<IRepo, MovieRepo>(); //★IPespoインターフェースにMoivieRepoを挿入! // Add services to the container. builder.Services.AddControllersWithViews(); // ★Buildの前にサービスを登録する var app = builder.Build(); ・・・ </code> ↓おそらくProgram.csの builder.Services.AddDbContext<RepoContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("RepoContext") ?? throw new InvalidOperationException("Connection string 'RepoContext' not found."))); によりコンテキストの接続文字列がGetConnectionString("RepoContext")になっている。 <code c# "Data/RepoContext.cs"> using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Repo.Models; namespace Repo.Data { public class RepoContext : DbContext { public RepoContext (DbContextOptions<RepoContext> options) : base(options) { } public DbSet<Repo.Models.Movie> Movie { get; set; } = default!; } } </code> <code C# "appsettings.json"> { "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "RepoContext": "Server=(localdb)\\mssqllocaldb;Database=Repo.Data;Trusted_Connection=True;MultipleActiveResultSets=true" } } </code> <code c# "Models/Movie.cs"> using System.ComponentModel.DataAnnotations; namespace Repo.Models { public class Movie { public int Id { get; set; } public string? Title { get; set; } [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } public string? Genre { get; set; } public decimal Price { get; set; } } } </code> <code c# "DAL/IRepo.cs"> using System; using System.Collections.Generic; using Repo.Models; namespace Repo.DAL { public interface IRepo { IEnumerable<Movie> GetMovies(); } } </code> <code c# "DAL/MovieRepo.cs"> using Repo.Data; using Repo.Models; namespace Repo.DAL { public class MovieRepo : IRepo { private RepoContext _context; public MovieRepo() { } public MovieRepo(RepoContext context) { _context = context; } public IEnumerable<Movie> GetMovies() { return _context.Movie.ToList(); } } } </code> ↓おそらくProgram.csの builder.Services.AddScoped<IRepo, MovieRepo>(); によりコンストラクターの引数にはMovieRepo が渡されている。 <code c# "Controllers/MoviesController.cs"> using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Repo.Data; using Repo.Models; using Repo.DAL; namespace Repo.Controllers { public class MoviesController : Controller { private IRepo _repo; public MoviesController(IRepo repo) //★←DI { _repo = repo; } public IActionResult Index() { return View(_repo.GetMovies()); } ・・・ </code>
dotnet/リポジトリパターン.txt
· 最終更新: 2022/09/01 13:19 by
ips
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ