我最近编写了一个程序,使用存储库模式来涵盖 CRUD 操作。现在,我的问题是如何进一步优化它以及如何将它与依赖注入集成。
到目前为止,我只能使用一种不优雅的方法(在静态类中初始化服务)来做到这一点。
IBaseService
界面:
namespace MyProject.DB.Services
{
public interface IBaseServices<T>
{
Task CreateAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(T entity);
Task DeleteByIDAsync(int id);
Task<T> GetByIDAsync(int id);
Task<List<T>> GetAllAsync();
}
}
BaseService
班级:
using Microsoft.EntityFrameworkCore;
using System.CodeDom;
using MyProject.DB.Entitys;
namespace MyProject.DB.Services
{
public class BaseServices<T> : IBaseServices<T> where T : BaseModel
{
private readonly DataContext _context;
public BaseServices(DataContext context)
{
_context = context;
}
public async Task CreateAsync(T entity)
{
await _context.Set<T>().AddAsync(entity);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(T entity)
{
T t = _context.Set<T>().Where(x => x.ID == entity.ID).First();
if (t != null)
{
_context.Set<T>().Update(entity);
await _context.SaveChangesAsync();
}
}
public async Task DeleteAsync(T entity)
{
T t = _context.Set<T>().Find(entity);
if (t != null)
{
_context.Set<T>().Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task DeleteByIDAsync(int id)
{
T t = _context.Set<T>().Where(x => x.ID == id).First();
if (t != null)
{
_context.Set<T>().Remove(t);
await _context.SaveChangesAsync();
}
}
public async Task<List<T>> GetAllAsync()
{
return await _context.Set<T>().ToListAsync();
}
public async Task<T> GetByIDAsync(int id)
{
return await _context.Set<T>().Where(x => x.ID == id).FirstOrDefaultAsync();
}
}
}
根据班级提供服务:
using MyProject.DB.Entitys;
namespace MyProject.DB.Services
{
public class PriceServices : BaseServices<Price>
{
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<Car> Cars { get; set; }
public DbSet<Type> Types { get; set; }
public DbSet<Price> 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<Car> carServices = new CarServices(context);
public static readonly IBaseServices<Type> typeServices = new TypeServices(context);
public static readonly IBaseServices<Price> priceServices = new PriceServices(context);
}
}
我想替换静态类,我用它来通过依赖注入初始化服务,我该怎么做?
因此,为了开始依赖注入,您需要使用
ServiceCollection
类配置您的服务。
对于 EF
DbContext
已有方法 AddDbContext
您可以使用。
对于自定义类,有很多选项,具体取决于您想要使用的生命周期,例如
AddTransient
。
这是示例控制台应用程序示例,其中包含依赖项注入的基本设置:
// Sample setup
public interface IInterface;
public class Concrete : IInterface;
public class DataContext : DbContext;
// Sample application
var builder = Host.CreateDefaultBuilder();
builder.ConfigureServices(servicesCollection =>
{
servicesCollection.AddTransient<IInterface, Concrete>();
servicesCollection.AddDbContext<DataContext>();
});
var app = builder.Build();
var concrete = app.Services.GetService<IInterface>();
Console.WriteLine(concrete.GetType().Name);