ユーザ用ツール

サイト用ツール


サイドバー

dotnet:リポジトリパターン

リポジトリパターン

"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();
        ・・・

↓おそらくProgram.csの
builder.Services.AddDbContext<RepoContext>(options ⇒
options.UseSqlServer(builder.Configuration.GetConnectionString(“RepoContext”) ?? throw new InvalidOperationException(“Connection string 'RepoContext' not found.”)));
によりコンテキストの接続文字列がGetConnectionString(“RepoContext”)になっている。

"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!;
    }
}
"appsettings.json"
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "RepoContext": "Server=(localdb)\\mssqllocaldb;Database=Repo.Data;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}
"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; }
    }
}
"DAL/IRepo.cs"
using System;
using System.Collections.Generic;
using Repo.Models;
 
namespace Repo.DAL
{
    public interface IRepo
    {
        IEnumerable<Movie> GetMovies();
    }
}
"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();
        }
    }
}

↓おそらくProgram.csの
builder.Services.AddScoped<IRepo, MovieRepo>();
によりコンストラクターの引数にはMovieRepo が渡されている。

"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());
        }
        ・・・
dotnet/リポジトリパターン.txt · 最終更新: 2022/09/01 13:19 by ips