この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
mvc:リポジトリパターン [2022/08/01 22:53] 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> | ||
- | |||
- | <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# "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> | ||
- | |||
- | <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) | ||
- | { | ||
- | _repo = repo; | ||
- | } | ||
- | |||
- | public IActionResult Index() | ||
- | { | ||
- | return View(_repo.GetMovies()); | ||
- | } | ||
- | ・・・ | ||
- | </code> |