ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.53k stars 363 forks source link

DI on base class? #683

Open eviathan opened 3 years ago

eviathan commented 3 years ago

I wonder if this is as designed and if it is can you explain a work around please? I am trying to DI my services into a base class and I want the derived implementations to also have those services injected.

Is this possible? Sorry for the wording its late and its been a busy week,

I have tried it as virtual and non virtual. I have tried calling the base init and not. Nothing seems to call the Init Method and the services are always null.

Base class:

public abstract class RenderTask : MonoBehaviour, IRenderTask
{
    private ICastleLogger _logger;
    protected ICastleLogger Logger => _logger;

    private FileService _fileService;
    protected FileService FileService => _fileService;

    private PipelineService _pipelineService;
    protected PipelineService PipelineService => _pipelineService;

    [Inject]
    public virtual void Init(ICastleLogger logger, FileService fileService, PipelineService pipelineService)
    {
        _logger = logger;
        _fileService = fileService;
        _pipelineService = pipelineService;
    }
}

Derived class

public class Split360 : RenderTask
{
    [Inject]
    public override void Init(ICastleLogger logger, FileService fileService, PipelineService pipelineService)
    {
        base.Init(logger, fileService, pipelineService);
    }
}