c# - Asp.net Core Mvc DI
问题描述
如果我有接口,我想了解它是如何工作的
public interface IProductRepository
{
IQueryable<Product> Products { get; }
}
然后我有一个实现这个接口的类
public class EFProductRepository : IProductRepository
{
public EFProductRepository(ApplicationDBContext ctx)
{
context = ctx;
}
private ApplicationDBContext context;
public IQueryable<Product> Products => context.Products;
}
ApplicationDbContext 类是
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
ConfigurationServices 方法是
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDBContext>(options =>
{
options.UseSqlServer(Configuration["Data:SportStoreProducts:Connectionstring"]);
});
services.AddTransient<IProductRepository, EFProductRepository>();
services.AddMvc();
}
在搜索了它的工作原理后,我发现当使用services.AddTransient<IProductRepository, EFProductRepository>();
应用程序中使用接口的组件时,它们会在创建时IProductRepository
收到一个对象。EFProductRepository
但是我想了解当EFProductRepository
通过此服务创建类对象时,构造函数EFProductRepository
需要ApplicationDBContext
类对象在这种情况下使用什么技术?
解决方案
推荐阅读
- for-loop - 对于目录中的每个文件,将文件名和子字符串打印到 csv,
- .net - 为什么我们有两个用于 JWT 令牌 JwtSecurityTokenHandler 和 JsonWebTokenHandler 的类?
- vb.net - OpenXML 按名称获取工作表 VB.Net
- python - 基于现有数值列在数据框中创建新列,字符串列表作为列名,元组列表作为值
- c# - 如何在 JSON 文件中添加、删除、更新、只读一个条目?
- asp.net - 如何将工作簿(.aspx 文件)从 html 链接复制到当前工作簿
- c# - 外键属性设置不正确
- url - 如何通过“curl -O”引用同一url中的变量
- neo4j - 如何汇总分数
- azure-batch - “ResourceContainerAccessDenied”作为 CloudTask.ExecutionInformation.FailureInformation.Code 的值返回,但不在 TaskFailureInformationCodes 中