IoC安装和继承类的问题 [英] IoC setup and issue with inherited class

查看:267
本文介绍了IoC安装和继承类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于IoC模式来说相当新鲜,我遇到了以下问题:

I'm fairly new to the IoC pattern and I've hit an issue on the way the following should be setup.

我有一个服务类它具有以下构造函数:

I've got a Service class which has the following constructor:

public BookingService(IBookingRepository bookingRepository, IUnitRepository   unitRepository, IRateRepository rateRepository, IDiscountRepository discountRepository, IUnitOfWork unitOfWork)
    {
        this.bookingRepository = bookingRepository;
        this.unitRepository = unitRepository;
        this.rateRepository = rateRepository;
        this.discountRepository = discountRepository;
        this.unitOfWork = unitOfWork;
    }

现在我已经有这样使用我的控制器:

Now I've got this working with my controllers like so:

private IBookingService _bookingService;

    public AdminBookingSurfaceController(IBookingService bookingService)
    {
        _bookingService = bookingService;
    }

在我继承的类中使用BookingService时,第三方框架(Umbraco)。

Where I've got stuck is when using the BookingService in an inherited class from a Third Party framework (Umbraco).

这是当前的构造函数:

public class Freedom2BookTree : umbraco.cms.presentation.Trees.BaseTree
{
    public Freedom2BookTree(string application)
        : base(application)
    {
    }

我不知道IoC如何处理这个问题,我试过这样,但没有工作:

I wasn't sure how IoC would work with this, I tried like this but it didn't work:

当我添加附加参数时,构造函数永远不会被命中/调用

As in, when I add the additional parameter the constructor never gets hit/called

public class Freedom2BookTree : umbraco.cms.presentation.Trees.BaseTree
{
    private IBookingService _bookingService;
    public Freedom2BookTree(string application, IBookingService bookingService)
        : base(application)
    {
        _bookingService = bookingService;
    }

如果有人可以借鉴一些建议,我们以错误的方式看待它,那很棒:)

If anyone could lend some advice on how this should be done or if I'm looking at it in the wrong way, that would great :)

很多谢谢,

p>

推荐答案

也许该框架只执行具有特定参数的构造函数。


您可以在 Freedom2BookTree 上指定 IBookingService 它在构造函数之外。

Maybe that framework only executes a constructor with specific parameters.
You can make the IBookingService a property on the Freedom2BookTree and assign it outside of the constructor.

这篇关于IoC安装和继承类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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