继承的asp.net核心构造函数注入 [英] asp.net core constructor injection with inheritance

查看:85
本文介绍了继承的asp.net核心构造函数注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的asp.net核心应用程序中,我具有依赖项类,这些依赖项类注入了几乎所有服务.因此,我想构建一个基础服务类来获取对属性的这些依赖关系,而我的服务将继承该基础服务类.

 公共抽象类BaseService{受保护的Foo Foo {get;放;}受保护的酒吧Bar {get;放;}公共BaseService(Foo foo,Bar bar){Foo = foo;酒吧=酒吧;}}公共类服务:BaseService{公共服务(IOtherDependency otherDependency){}公共无效方法(){var value = Bar.value;Foo.Do(值);}} 

因此,使用给定的代码,它警告我使用提供的参数调用基本构造函数,但是它们是将在运行时注入的参数,我不希望这样做.如果添加无参数构造函数,它将不会调用我需要的参数化构造函数.

我不想在继承的服务中调用或定义任何在基本服务( Foo Bar )中注入的类,该怎么做?/p>

通过 Foo Bar 类以单例方式注入到容器中,以防它们的生命周期很重要.

解决方案

这是我一直在寻找的./p>


如上所述,我为我的控制器库修改了代码.

对于我在服务中提出的问题;因为它们不像内置的 Controller 基类那样使用 HttpContext ,所以我只将 IServiceProvider 类注入到BaseService中,所以无论我需要什么在我所有的服务中,我都通过 provider.GetService()将其添加到媒体资源.

 公共抽象类BaseService{受保护的Foo Foo {get;放;}受保护的酒吧Bar {get;放;}公共BaseService(IServiceProvider提供程序){Foo = provider.GetService< Foo>();Bar = provider.GetService< Bar>();}}公共类服务:BaseService,IService{公共服务(IOtherDependency otherDependency,IServiceProvider提供者):base(提供者){}公共无效方法(){var value = Bar.value;Foo.Do(值);}}公共类SomeController:BaseController{私有只读IService _service;公共SomeController(IService服务){_service =服务;}公共IActionResult Index(){//调用方法_service.Method();}} 

In my asp.net core application I have dependency classes which are injected to almost all services. So I want to build a base service class to get these dependencies to properties and my services inherit this base service class.

public abstract class BaseService
{
    protected Foo Foo { get; set; }
    protected Bar Bar { get; set; }

    public BaseService(Foo foo, Bar bar)
    {
        Foo = foo;
        Bar = bar;
    }
}
public class Service : BaseService
{
    public Service(IOtherDependency otherDependency) { }

    public void Method()
    {
        var value = Bar.value;
        Foo.Do(value);
    }
}

So with the given code it warns me to call base constructor with supplied parameters, however they are the parameters that will be injected on runtime, I don't want it. If I add a parameterless constructor it will not call my parameterized constructor which I need.

I don't want to call or define any class that injected in base service(Foo and Bar) inside my inherited service, how can I do that ?

By the way Foo and Bar classes are injected as singleton to container in case their lifetime are important.

解决方案

This is what I was looking for.


I modified my code for my controller base as indicated above post.

For my service side which I was asking in my question; as they do not make use of HttpContext like built-in Controller base class, I only inject IServiceProvider class to my BaseService, so whatever I need in all my services, I get it to property via provider.GetService().

public abstract class BaseService
{
    protected Foo Foo { get; set; }
    protected Bar Bar { get; set; }

    public BaseService(IServiceProvider provider)
    {
        Foo = provider.GetService<Foo>();
        Bar = provider.GetService<Bar>();
    }
}
public class Service : BaseService, IService
{
    public Service(IOtherDependency otherDependency, IServiceProvider provider) : base(provider) { }

    public void Method()
    {
        var value = Bar.value;
        Foo.Do(value);
    }
}

public class SomeController : BaseController
{
    private readonly IService _service;

    public SomeController(IService service)
    {
        _service = service;
    }

    public IActionResult Index()
    {
        //call method
        _service.Method();
    }
}

这篇关于继承的asp.net核心构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆