この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
mvc:リポジトリパターン [2022/07/31 17:23] ips |
— (現在) | ||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== リポジトリパターン ====== | ||
- | スキャフォールディングで作成したControllerを修正。 | ||
- | 直接contextを使用していたものを、 | ||
- | IRepositoryを実装したRepository経由でのcontext操作に書き換えた。 | ||
- | |||
- | 以下はcontextに依存しているているので間違っている。 | ||
- | |||
- | <code c# "Data/MvcMovieContext.cs"> | ||
- | using System; | ||
- | using System.Collections.Generic; | ||
- | using System.Linq; | ||
- | using System.Threading.Tasks; | ||
- | using Microsoft.EntityFrameworkCore; | ||
- | using MvcMovie.Models; | ||
- | |||
- | namespace MvcMovie.Data | ||
- | { | ||
- | public class MvcMovieContext : DbContext | ||
- | { | ||
- | |||
- | |||
- | public MvcMovieContext (DbContextOptions<MvcMovieContext> options) | ||
- | : base(options) | ||
- | { | ||
- | } | ||
- | |||
- | public MvcMovieContext(DbContextOptionsBuilder contextOptions) | ||
- | { | ||
- | } | ||
- | |||
- | public DbSet<MvcMovie.Models.Movie> Movie { get; set; } = default!; | ||
- | |||
- | public DbSet<MvcMovie.Models.Publish> Publish { get; set; } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | <code c# "Program.cs"> | ||
- | ・・・ | ||
- | var builder = WebApplication.CreateBuilder(args); | ||
- | builder.Services.AddDbContext<MvcMovieContext>(options => | ||
- | options.UseSqlServer(builder.Configuration.GetConnectionString("MvcMovieContext") ?? throw new InvalidOperationException("Connection string 'MvcMovieContext' not found."))); | ||
- | ・・・ | ||
- | </code> | ||
- | |||
- | <code c# "Models/Pulish.cs"> | ||
- | namespace MvcMovie.Models | ||
- | { | ||
- | public class Publish | ||
- | { | ||
- | public int Id { get; set; } | ||
- | public string Title { get; set; } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | <code c# "Models/IPublishRepository.cs"> | ||
- | using Microsoft.EntityFrameworkCore; | ||
- | |||
- | namespace MvcMovie.Models | ||
- | { | ||
- | public interface IPublishRepository | ||
- | { | ||
- | public DbSet<Publish> Publish(); | ||
- | Task<List<Publish>> ToListAsync(); | ||
- | |||
- | void Add(Publish publish); | ||
- | |||
- | void Update(Publish publish); | ||
- | Task<int> SaveChangesAsync(); | ||
- | |||
- | void Remove(Publish publish); | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | <code c# "Models/PubRepository.cs"> | ||
- | using Microsoft.AspNetCore.Mvc; | ||
- | using Microsoft.EntityFrameworkCore; | ||
- | using MvcMovie.Data; | ||
- | |||
- | |||
- | namespace MvcMovie.Models | ||
- | { | ||
- | public class PubRepository : IPublishRepository | ||
- | { | ||
- | private MvcMovieContext _context; | ||
- | public DbSet<Publish> Publish() | ||
- | { | ||
- | return _context.Publish; | ||
- | } | ||
- | |||
- | public PubRepository(MvcMovieContext context) | ||
- | { | ||
- | _context = context; | ||
- | } | ||
- | |||
- | public async Task<List<Publish>> ToListAsync() | ||
- | { | ||
- | return (List<Publish>)await _context.Publish.ToListAsync(); | ||
- | } | ||
- | |||
- | public void Add(Publish publish) | ||
- | { | ||
- | _context.Add(publish); | ||
- | } | ||
- | |||
- | public void Update(Publish publish) | ||
- | { | ||
- | _context.Update(publish); | ||
- | } | ||
- | |||
- | public void Remove(Publish publish) | ||
- | { | ||
- | _context.Publish.Remove(publish); | ||
- | } | ||
- | |||
- | |||
- | |||
- | public Task<int> SaveChangesAsync() | ||
- | { | ||
- | return _context.SaveChangesAsync(); | ||
- | } | ||
- | |||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | <code c# "Controllers/PublishesController.cs"> | ||
- | using Microsoft.AspNetCore.Mvc; | ||
- | using Microsoft.EntityFrameworkCore; | ||
- | using Microsoft.EntityFrameworkCore.Infrastructure; | ||
- | using Microsoft.Extensions.Options; | ||
- | using MvcMovie.Data; | ||
- | using MvcMovie.Models; | ||
- | |||
- | namespace MvcMovie.Controllers | ||
- | { | ||
- | public class PublishesController : Controller | ||
- | { | ||
- | |||
- | private IPublishRepository pubrepository; | ||
- | private MvcMovieContext _context; | ||
- | |||
- | public PublishesController(MvcMovieContext context) | ||
- | { | ||
- | pubrepository = new PubRepository(context); | ||
- | } | ||
- | |||
- | public async Task<IActionResult> Index() | ||
- | { | ||
- | return View(await pubrepository.ToListAsync()); | ||
- | } | ||
- | |||
- | // GET: Publishes/Create | ||
- | public IActionResult Create() | ||
- | { | ||
- | return View(); | ||
- | } | ||
- | |||
- | // POST: Publishes/Create | ||
- | // To protect from overposting attacks, enable the specific properties you want to bind to. | ||
- | // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. | ||
- | [HttpPost] | ||
- | [ValidateAntiForgeryToken] | ||
- | public async Task<IActionResult> Create([Bind("Id,Title")] Publish publish) | ||
- | { | ||
- | if (ModelState.IsValid) | ||
- | { | ||
- | pubrepository.Add(publish); | ||
- | await pubrepository.SaveChangesAsync(); | ||
- | return RedirectToAction(nameof(Index)); | ||
- | } | ||
- | return View(publish); | ||
- | } | ||
- | |||
- | // GET: Publishes/Edit/5 | ||
- | public async Task<IActionResult> Edit(int? id) | ||
- | { | ||
- | if (id == null || pubrepository.Publish() == null) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | |||
- | var publish = await pubrepository.Publish().FindAsync(id); | ||
- | if (publish == null) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | return View(publish); | ||
- | } | ||
- | |||
- | // POST: Publishes/Edit/5 | ||
- | // To protect from overposting attacks, enable the specific properties you want to bind to. | ||
- | // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. | ||
- | [HttpPost] | ||
- | [ValidateAntiForgeryToken] | ||
- | public async Task<IActionResult> Edit(int id, [Bind("Id,Title")] Publish publish) | ||
- | { | ||
- | if (id != publish.Id) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | |||
- | if (ModelState.IsValid) | ||
- | { | ||
- | try | ||
- | { | ||
- | pubrepository.Update(publish); | ||
- | await pubrepository.SaveChangesAsync(); | ||
- | } | ||
- | catch (DbUpdateConcurrencyException) | ||
- | { | ||
- | if (!PublishExists(publish.Id)) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | else | ||
- | { | ||
- | throw; | ||
- | } | ||
- | } | ||
- | return RedirectToAction(nameof(Index)); | ||
- | } | ||
- | return View(publish); | ||
- | |||
- | } | ||
- | private bool PublishExists(int id) | ||
- | { | ||
- | return pubrepository.Publish().Any(e => e.Id == id); | ||
- | } | ||
- | |||
- | // GET: Publishes/Details/5 | ||
- | public async Task<IActionResult> Details(int? id) | ||
- | { | ||
- | if (id == null || pubrepository.Publish() == null) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | |||
- | var publish = await pubrepository.Publish() | ||
- | .FirstOrDefaultAsync(m => m.Id == id); | ||
- | if (publish == null) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | |||
- | return View(publish); | ||
- | } | ||
- | |||
- | // GET: Publishes/Delete/5 | ||
- | public async Task<IActionResult> Delete(int? id) | ||
- | { | ||
- | if (id == null || pubrepository.Publish() == null) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | |||
- | var publish = await pubrepository.Publish() | ||
- | .FirstOrDefaultAsync(m => m.Id == id); | ||
- | if (publish == null) | ||
- | { | ||
- | return NotFound(); | ||
- | } | ||
- | |||
- | return View(publish); | ||
- | } | ||
- | |||
- | // POST: Publishes/Delete/5 | ||
- | [HttpPost, ActionName("Delete")] | ||
- | [ValidateAntiForgeryToken] | ||
- | public async Task<IActionResult> DeleteConfirmed(int id) | ||
- | { | ||
- | if (pubrepository.Publish() == null) | ||
- | { | ||
- | return Problem("Entity set 'MvcMovieContext.Publish' is null."); | ||
- | } | ||
- | var publish = await pubrepository.Publish().FindAsync(id); | ||
- | if (publish != null) | ||
- | { | ||
- | pubrepository.Remove(publish); | ||
- | } | ||
- | |||
- | await pubrepository.SaveChangesAsync(); | ||
- | return RedirectToAction(nameof(Index)); | ||
- | } | ||
- | } | ||
- | } | ||
- | |||
- | </code> |