[英]Generic interfaces which type implements another interface
Could somebody explain why it doesn't work.有人可以解释为什么它不起作用。
I have two db context.我有两个数据库上下文。 And one common method with different return types and different queries.
以及一种具有不同返回类型和不同查询的通用方法。
public interface IDataFetcher<T> where T : IMarker
{
public List<T> GetData();
}
public interface IFetchServiceOne<T> : IDataFetcher<T> where T : IMarker
{
//maybe some methods here
}
public interface IFetchServiceTwo<T> : IDataFetcher<T> where T : IMarker
{
//maybe some different methods here
}
Implementation:执行:
public class FetchServiceOne<T> : IFetchServiceOne<T> where T : IMarker
{
private readonly DBContext _dbContext;
public FetchServiceOne(DBContext dbContext) => _dbContext = dbContext;
public List<CrucialData> GetData()
{
var example = _dbContext.Test.ToList();
return example;
}
}
public class FetchServiceTwo<T> : IFetchServiceOne<T> where T : IMarker
{
private readonly DBContext _dbContext;
public FetchServiceTwo(DBContext dbContext) => _dbContext = dbContext;
public List<CrucialDataTwo> GetData()
{
var example = _dbContext.Test2.ToList();
return example;
}
}
public class CrucialData: IMarker
{
//some properries
}
public class CrucialDataTwo: IMarker
{
//another properries
}
In the output I'm getting compile error:在 output 中出现编译错误:
Error (active) CS0738 'FetchService' does not implement interface member 'IDataFetcher.GetData()'.
错误(活动)CS0738“FetchService”未实现接口成员“IDataFetcher.GetData()”。 'FetchService.GetData()' cannot implement 'IDataFetcher.GetData()' because it does not have the matching return type of 'List'.
“FetchService.GetData()”无法实现“IDataFetcher.GetData()”,因为它没有匹配的“List”返回类型。
It looks like IFetchServiceOne
and IFetchServiceTwo
(and their implementations) should be "concrete" interfaces which implement closed versione of IDataFetcher
.看起来
IFetchServiceOne
和IFetchServiceTwo
(及其实现)应该是实现IDataFetcher
封闭版本的“具体”接口。 Ie: IE:
public interface IFetchServiceOne : IDataFetcher<CrucialData>
{
//maybe some methods here
}
public class FetchServiceOne : IFetchServiceOne
{
// ...
public List<CrucialData> GetData()
{
// implementation ...
return default;
}
}
Otherwise you can't guarantee that T
requested by user will be CrucialData
for FetchServiceOne
(ie FetchServiceOne<CrucialDataTwo>.GetData()
...)否则你不能保证用户请求的
T
将是CrucialData
的FetchServiceOne
(即FetchServiceOne<CrucialDataTwo>.GetData()
...)
Or just:要不就:
public class FetchServiceOne : IDataFetcher<CrucialData>
{
// ...
}
Without need for introduction of the intermediate interface.无需引入中间接口。
Note that for EF you can create a generic implementation via Set
method if needed:请注意,对于 EF,您可以根据需要通过
Set
方法创建通用实现:
public class FetchServiceOne<T> : IDataFetcher<T> where T : IMarker
{
private readonly DBContext _dbContext;
public FetchServiceOne(DBContext dbContext) => _dbContext = dbContext;
public List<T> GetData()
{
var example = _dbContext.Set<T>.ToList();
return example;
}
}
But this will fail at runtime if there is no matching DbSet
property setup for provided T
type.但是,如果提供的
T
类型没有匹配DbSet
属性设置,这将在运行时失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.