この文書の現在のバージョンと選択したバージョンの差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
|
mvc:リポジトリパターン [2022/08/01 23:00] ips |
— (現在) | ||
|---|---|---|---|
| ライン 1: | ライン 1: | ||
| - | ====== リポジトリパターン ====== | ||
| - | |||
| - | <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> | ||