IoC setup and issue with inherited class

点点圈 提交于 2019-12-12 06:30:39

问题


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:


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.



来源:https://stackoverflow.com/questions/16488538/ioc-setup-and-issue-with-inherited-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!