首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将DI存储库转换为类型类?

如何将DI存储库转换为类型类?
EN

Stack Overflow用户
提问于 2018-06-01 12:33:31
回答 1查看 564关注 0票数 1

这里需要帮助..。

我正在查看示例"graphql-dotnet/server“,其中公开的对象只包含普通属性。但是,如果我需要解析一个属性并从存储库中获取数据,我如何才能获得Type类中的respository类?

示例:该示例有一个公开“消息”的ChatQuery。

代码语言:javascript
复制
public ChatQuery(IChat chat)
    {
        Field<ListGraphType<MessageType>>("messages", resolve: context => chat.AllMessages.Take(100));
    }

实例"chat“是此处的存储库,它通过chat.AllMessages为数据(消息)提供服务。

比方说,一条消息有一个观众列表。然后,我需要从存储库中解析该列表。这是在另一个示例"graphql-dotnet/examples“中完成的,其中"StarWars/Types/StarWarsCharacter.cs”有一个好友列表,"StarWars/Types/HumanType“在构造函数中插入了存储库(StarWarsData),可以在”朋友“的解析方法中使用:

代码语言:javascript
复制
public class HumanType : ObjectGraphType<Human>
{
    public HumanType(StarWarsData data)
    {
        Name = "Human";

        Field(h => h.Id).Description("The id of the human.");
        Field(h => h.Name, nullable: true).Description("The name of the human.");

        Field<ListGraphType<CharacterInterface>>(
            "friends",
            resolve: context => data.GetFriends(context.Source)
        );
        Field<ListGraphType<EpisodeEnum>>("appearsIn", "Which movie they appear in.");

        Field(h => h.HomePlanet, nullable: true).Description("The home planet of the human.");

        Interface<CharacterInterface>();
    }
}

但是,在服务器示例中做同样的事情是行不通的。

代码语言:javascript
复制
public class MessageType : ObjectGraphType<Message>
{
    public MessageType(IChat chat)
    {
        Field(o => o.Content);
        Field(o => o.SentAt);
        Field(o => o.From, false, typeof(MessageFromType)).Resolve(ResolveFrom);
        Field<ListGraphType<Viewer>>(
            "viewers",
            resolve: context => chat.GetViewers(context.Source)
        );
    }

    private MessageFrom ResolveFrom(ResolveFieldContext<Message> context)
    {
        var message = context.Source;
        return message.From;
    }
}

当我将聊天存储库添加到MessageType中的构造函数时,它会失败。

--我在这里显然遗漏了一些东西;为什么依赖注入没有将聊天实例注入到“graphql/server”项目中的MessageType类中?但它适用于“graphql/examples”项目.

最好,马格纳斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-03 15:01:11

要使用DI,需要在Schema类的构造函数中传递依赖项解析器。默认的解析器使用Activator.CreateInstance,因此您必须教它您正在使用的容器。

代码语言:javascript
复制
services.AddSingleton<IDependencyResolver>(
  s => new FuncDependencyResolver(s.GetRequiredService));

IDependecyResolver是graphql项目中的一个接口。

代码语言:javascript
复制
public class StarWarsSchema : Schema
{
    public StarWarsSchema(IDependencyResolver resolver)
        : base(resolver)
    {
        Query = resolver.Resolve<StarWarsQuery>();
        Mutation = resolver.Resolve<StarWarsMutation>();
    }
}

https://github.com/graphql-dotnet/examples/blob/bcf46c5c502f8ce75022c50b9b23792e5146f6d2/src/AspNetCore/Example/Startup.cs#L20

https://github.com/graphql-dotnet/examples/blob/bcf46c5c502f8ce75022c50b9b23792e5146f6d2/src/StarWars/StarWarsSchema.cs#L6-L14

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50643549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档