0
0

MyDarlingRepository DI

This commit is contained in:
Sergey Chebotar 2023-02-16 07:23:48 +03:00
parent 3e8118838b
commit af72b18ba0
5 changed files with 54 additions and 11 deletions

View File

@ -6,14 +6,14 @@ namespace MyDarling.Controllers
{
public class HomeController : Controller
{
private DataContext context;
public HomeController(DataContext context)
private IRepository repository;
public HomeController(IRepository repository)
{
this.context = context;
this.repository = repository;
}
public IActionResult Index()
{
return View(context.UnderwearBundles.Include(bundle => bundle.Figures).Where(x => x.Price != 0));
return View(repository);
}
}
}

10
Models/IRepository.cs Normal file
View File

@ -0,0 +1,10 @@
namespace MyDarling.Models
{
public interface IRepository
{
public IQueryable<UnderwearBundle> Bundles { get; }
public void Add(UnderwearBundle b);
public void Remove(UnderwearBundle p);
public void Save();
}
}

View File

@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
namespace MyDarling.Models
{
public class MyDarlingRepository : IRepository
{
private DataContext context;
public MyDarlingRepository(IServiceProvider provider)
{
context = provider.CreateScope().ServiceProvider.GetRequiredService<DataContext>();
}
public IQueryable<UnderwearBundle> Bundles => context.UnderwearBundles.Include(b => b.Figures);
public void Add(UnderwearBundle b)
{
context.UnderwearBundles.Add(b);
context.SaveChanges();
}
public void Remove(UnderwearBundle b)
{
context.UnderwearBundles.Remove(b);
context.SaveChanges();
}
public void Save()
{
context.SaveChanges();
}
}
}

View File

@ -3,11 +3,12 @@ using MyDarling.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<DataContext>(opts =>
builder.Services.AddDbContext<DataContext>(opts =>
{
opts.UseSqlite(builder.Configuration["ConnectionStrings:MyDarlingDb"]);
opts.EnableSensitiveDataLogging(true);
opts.UseSqlite(builder.Configuration["ConnectionStrings:MyDarlingDb"]);
opts.EnableSensitiveDataLogging(true);
});
builder.Services.AddSingleton<IRepository, MyDarlingRepository>();
builder.Services.AddControllersWithViews();
var app = builder.Build();
@ -16,7 +17,7 @@ app.UseStaticFiles();
app.MapControllers();
app.MapDefaultControllerRoute();
var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>();
SeedData.SeedDatabase(context);
// var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<DataContext>();
// SeedData.SeedDatabase(context);
app.Run();

View File

@ -1,10 +1,10 @@
@model IQueryable<MyDarling.Models.UnderwearBundle>;
@model MyDarling.Models.IRepository;
@using System.Globalization;
<section class="projects-section bg-light" id="projects">
<div class="container px-3 px-lg-4 mt-4">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
@foreach (var bundle in @Model)
@foreach (var bundle in @Model.Bundles.Where(b => b.Price != 0))
{
<div class="col mb-5">
<div class="card h-100">