简体   繁体   English

类型实现另一个接口的通用接口

[英]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 .看起来IFetchServiceOneIFetchServiceTwo (及其实现)应该是实现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将是CrucialDataFetchServiceOne (即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.

相关问题 具有通用类型的基类,该基类实现具有通用类型的接口 - Base class with a generic type which implements an interface with a generic type 如何实现实现另一个接口的通用接口? - How to implement a generic interface which implements another interface? 实例化实现接口C#的通用类型 - Instantiate a generic type which implements an Interface C# 获取类实现的泛型接口的类型参数 - Getting type arguments of generic interfaces that a class implements 如何使用反射来查找开放泛型类型上的哪个开放泛型参数实现泛型接口? - How can I use reflection to find which open generic argument on an open generic type implements a generic interface? .NET Core Cast与具有实现接口的类型的通用类型参数的接口 - .NET Core Cast to interface with generic type parameter of type which implements interface 确定 class 是否实现通用接口,然后调用接口方法 - Determine if a class implements a generic interface and then call a the interfaces method 找出类型是否实现了通用接口 - Finding out if a type implements a generic interface 在实现通用接口方法的方法中专门化类型 - Specialize type in method that implements a generic interface method 有什么方法可以将通用接口限制为实现它的类型? - Any way to constrain a generic interface to the type that implements it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM