简体   繁体   English

c# 9中,如何声明一个灵活的接口?

[英]In c# 9, How To Declare a Flexible Interface?

I'm using Entity Framework Core and want to have two different implementations of SpeakerQueries that each use a different EF Core data context.我正在使用 Entity Framework Core,并希望有两个不同的 SpeakerQueries 实现,每个实现都使用不同的 EF Core 数据上下文。

My original class only included one data context, but I've extended such that it now includes two.我原来的 class 只包含一个数据上下文,但我已经扩展它现在包含两个。

Here is my c# interface defined as follows:这是我的 c# 接口定义如下:

public interface ISpeakerQueries
{
    IQueryable<Speaker> GetSpeakers(
        [ScopedService] ApplicationDbContext context, [ScopedService] ApplicationDbContextAlt contextAlt);
    ...

I really only want one data context to be called with each method but not sure how to define my interface to allow that to happen.我真的只想用每种方法调用一个数据上下文,但不确定如何定义我的接口以允许这种情况发生。

In other words, I want my concrete implementations of GetSpeakers to be换句话说,我希望 GetSpeakers 的具体实现是

public class SpeakerQueriesAlt : ISpeakerQueries
{
    [UseApplicationDbContextAlt]
    [UsePaging]
    public IQueryable<Speaker> GetSpeakers(
        [ScopedService] ApplicationDbContextAlt context)
    { ...

or或者

public class SpeakerQueries : ISpeakerQueries
{
    [UseApplicationDbContext]
    [UsePaging]
    public IQueryable<Speaker> GetSpeakers(
        [ScopedService] ApplicationDbContext context)
    { ...

I think I can do this, but not sure how I should declare the interface.我想我可以做到这一点,但不确定我应该如何声明接口。 The interface I declared at the top does not work because it expects two parameters and not one.我在顶部声明的接口不起作用,因为它需要两个参数而不是一个。

Are you looking into HotChocolate and their workshop sample?你在寻找 HotChocolate 和他们的车间样品吗? If not then ignore rest:)如果没有,则忽略 rest :)

Using method injections is the correct way of injecting services into the HotChocolate library for execution.使用方法注入是将服务注入 HotChocolate 库执行的正确方法。

I'm trying to understand what are trying to achieve?我试图了解要达到的目标是什么? Are you trying to (A) create two query endpoints that will use different dbcontext and only return data for that spesific context?您是否尝试 (A) 创建两个查询端点,它们将使用不同的 dbcontext 并且只返回该特定上下文的数据? Or are you trying to create (B) one query that will return all speakers for both dbcontexts?或者您是否尝试创建 (B) 一个查询,该查询将返回两个 dbcontexts 的所有发言者?

In case of A then I think you can easily extract the interface from that, but you will probably have to give them two different names.如果是 A,那么我认为您可以轻松地从中提取接口,但您可能必须给它们两个不同的名称。

If B then I would either create a service that injects the two different contexts and have the responsiblity to join the results together.如果是 B,那么我将创建一个注入两个不同上下文的服务,并负责将结果连接在一起。 Or you can create your own Middleware/pipeline, Or even better create 3 graphql servers where Server A returns data from Context1 and Server B returns data from context 2 and your server C which is public is using schema stitching.或者您可以创建自己的中间件/管道,或者甚至更好地创建 3 个 graphql 服务器,其中服务器 A 从上下文 1 返回数据,服务器 B 从上下文 2 返回数据,您的服务器 C 使用模式拼接。

As mentioned if this is not HC and Graphql just ignore my answer如前所述,如果这不是 HC 和 Graphql 就忽略我的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM