IoC setup and issue with inherited class

2019-09-17 11:10发布

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;
    }

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

This is the current constructor:

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

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 :)

Many Thanks,

Tom

1条回答
Root(大扎)
2楼-- · 2019-09-17 11:43

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.

查看更多
登录 后发表回答