До сих пор мне удавалось сделать это только с помощью неэлегантного метода (инициализации сервисов). в статическом классе).
Код: Выделить всё
IBaseService
Код: Выделить всё
namespace MyProject.DB.Services
{
public interface IBaseServices
{
Task CreateAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(T entity);
Task DeleteByIDAsync(int id);
Task GetByIDAsync(int id);
Task GetAllAsync();
}
}
Код: Выделить всё
BaseService
Код: Выделить всё
using Microsoft.EntityFrameworkCore;
using System.CodeDom;
using MyProject.DB.Entitys;
namespace MyProject.DB.Services
{
public class BaseServices : IBaseServices where T : BaseModel
{
private readonly DataContext _context;
public BaseServices(DataContext context)
{
_context = context;
}
public async Task CreateAsync(T entity)
{
await _context.Set().AddAsync(entity);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(T entity)
{
T t = _context.Set().Where(x => x.ID == entity.ID).First();
if (t != null)
{
_context.Set().Update(entity);
await _context.SaveChangesAsync();
}
}
public async Task DeleteAsync(T entity)
{
T t = _context.Set().Find(entity);
if (t != null)
{
_context.Set().Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task DeleteByIDAsync(int id)
{
T t = _context.Set().Where(x => x.ID == id).First();
if (t != null)
{
_context.Set().Remove(t);
await _context.SaveChangesAsync();
}
}
public async Task GetAllAsync()
{
return await _context.Set().ToListAsync();
}
public async Task GetByIDAsync(int id)
{
return await _context.Set().Where(x => x.ID == id).FirstOrDefaultAsync();
}
}
}
Код: Выделить всё
using MyProject.DB.Entitys;
namespace MyProject.DB.Services
{
public class PriceServices : BaseServices
{
private readonly DataContext _context;
public PriceServices(DataContext context) : base(context)
{
_context = context;
}
}
}
Код: Выделить всё
using System.ComponentModel.DataAnnotations;
namespace MyProject.DB.Entitys
{
public class BaseModel
{
[Key]
public int ID { get; set; }
}
}
Код: Выделить всё
DataContext
Код: Выделить всё
using Microsoft.EntityFrameworkCore;
using MyProject.DB.Entitys;
namespace MyProject.DB
{
public class DataContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Vehicles;Integrated Security=True;");
}
public DbSet Cars { get; set; }
public DbSet Types { get; set; }
public DbSet
Prices { get; set; }
}
}
Код: Выделить всё
using MyProject.Business;
using MyProject.DB.Entitys;
namespace MyProject.DB.Services
{
public static class MyServices
{
public static readonly DataContext context = new DataContext();
public static readonly IBaseServices carServices = new CarServices(context);
public static readonly IBaseServices typeServices = new TypeServices(context);
public static readonly IBaseServices
priceServices = new PriceServices(context);
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... ry-pattern