我有一个从抽象类派生的派生类,抽象类实现了接口。我想注入抽象类来使用抽象类和接口类中的成员。我的类看起来像:
public interface IStorageTableQueryHelper
{
IStorageTableQueryHelper Connect(string storageconnectionString, string entityName);
Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncStruct<T>(Expression<Func<TableEntity, bool>> expression,
List<string> selectField) where T : struct;
Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncStruct<T>(string query, List<string> selectField) where T : struct;
}
我的抽象类
public abstract class StorageTableQueryAbstractHelper : IStorageTableQueryHelper
{
public abstract IStorageTableQueryHelper Connect(string storageconnectionString, string entityName);
public abstract Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncStruct<T>(
Expression<Func<TableEntity, bool>> expression, List<string> selectField) where T : struct;
public abstract Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncClass<T>(Expression<Func<TableEntity, bool>> expression, List<string> selectField) where T : class, new();
public abstract Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncStruct<T>(string query,
List<string> selectField) where T : struct;
public abstract Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncClass<T>(string query,
List<string> selectField) where T : class, new();
}
派生类
public class StorageTableQueryHelper : StorageTableQueryAbstractHelper
{
private readonly ITableService _tableService;
private string connectionString { get; set; }
private string entityName { get; set; }
public StorageTableQueryHelper(ITableService tableService)
{
_tableService = tableService;
}
public override IStorageTableQueryHelper Connect(string storageConnectionString, string entityTableName)
{
this.connectionString = storageConnectionString;
this.entityName = entityTableName;
return this;
}
public override async Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncStruct<T>(Expression<Func<TableEntity, bool>> expression, List<string> selectField) where T : struct
{
//var result= await _tableService.Connect(this.connectionString,this.entityName).ExecuteQuery<T>(expression, selectField);
return null;
}
public override async Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncClass<T>(Expression<Func<TableEntity, bool>> expression, List<string> selectField) where T : class
{
// var result = await _tableService.Connect(this.connectionString, this.entityName).ExecuteQuery<T>(expression, selectField);
return null;
}
public override async Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncStruct<T>(string query, List<string> selectField) where T:struct
{
//var result = await _tableService.Connect(this.connectionString, this.entityName).ExecuteQuery<T>(query, selectField);
return null;
}
public override async Task<ContinuationTokenPageResult<T>> ExecuteTableQueryAsyncClass<T>(string query, List<string> selectField) where T : class
{
// var result = await _tableService.Connect(this.connectionString, this.entityName).ExecuteQuery<T>(query, selectField);
return null;
}
}
我想注入基类的类也使用接口实现
public class EShopBusinessLogicProcessor : IEShopBusinessLogicProcessor
{
private readonly IStorageTableQueryHelper _storageTableQueryHelper;
private EShopHttpRequestPayload _eShopHttpRequestPayload;
public EShopBusinessLogicProcessor(IStorageTableQueryHelper storageTableQueryHelper)
{
_storageTableQueryHelper = storageTableQueryHelper;
}
}
我正在尝试使用。net Core DI,它看起来像
services.AddScoped<IStorageTableQueryHelper, StorageTableQueryAbstractHelper>();
services.AddScoped<StorageTableQueryHelper>();
,但我不能注入它,因为它说接口不包含定义,即使我尝试使用抽象类注入
public class EShopBusinessLogicProcessor : IEShopBusinessLogicProcessor
{
private readonly StorageTableQueryAbstractHelper _storageTableQueryHelper;
private EShopHttpRequestPayload _eShopHttpRequestPayload;
public EShopBusinessLogicProcessor(StorageTableQueryAbstractHelper storageTableQueryHelper)
{
_storageTableQueryHelper = storageTableQueryHelper;
}
}
如何将抽象类注入到消费者类中?
您可以用回调注册接口并解析其中的抽象类。但是你必须首先将抽象类映射到实现。
// map the abstract class to an implementation
services.AddScoped<StorageTableQueryAbstractHelper, StorageTableQueryHelper>();
// then map the interface to abstract class
services.AddScoped<IStorageTableQueryHelper>(sp => sp.GetRequiredService<StorageTableQueryAbstractHelper>());
// or register it directly with the implementation
// services.AddScoped<IStorageTableQueryHelper, StorageTableQueryHelper>();
您需要将具体类注册为IStorageTableQueryHelper
实现,而不是抽象类。
services.AddScoped<IStorageTableQueryHelper, StorageTableQueryHelper>();
DI容器将只解析你注册的类型。它不会尝试查找其中一个注册类型是否从另一个类型继承或实现接口。如果有2或3个类实现该接口呢?
这就是为什么你不能仅仅注册一个具体类型并注入它实现的接口。
抽象类根本没有注册,因此无法解析。下面的构造函数总是会失败:
public EShopBusinessLogicProcessor(StorageTableQueryAbstractHelper)
要使此工作,需要以下注册:
services.AddScoped<StorageTableQueryAbstractHelper, StorageTableQueryHelper>();