我有两个服务 GooodsService,ProductService
我在Startup 中分别做了注册代码如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<GoodsContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));//添加Mysql支持
services.AddUnitOfWork<GoodsContext>();//添加UnitOfWork支持
services.AddScoped(typeof(IGooodsService), typeof(GooodsService));//用ASP.NET Core自带依赖注入(DI)注入使用的类
services.AddDbContext<ProductContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));//添加Mysql支持
services.AddUnitOfWork<ProductContext>();//添加UnitOfWork支持
services.AddScoped(typeof(IProductService), typeof(ProductService));//用ASP.NET Core自带依赖注入(DI)注入使用的类
services.AddMvc();
}
可以我在Controller 服务依赖的没有问题,可是服务的所绑定的IUnitOfWork 却是有问题的
ProductService 代码:
public class ProductService : IProductService
{
private readonly IUnitOfWork _unitOfWork;
public ProductService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public string Query()......略
}}
GooodsService 代码:
public class GooodsService : IGooodsService
{
private readonly IUnitOfWork _unitOfWork;
public GooodsService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}.....略
}