문제

I need to write a custom base template which needs to have a constructor parameter injected.

For RazorEngine v2 this illustrates very clearly how has to be done.

How should custom base template DI be done in v3? I mean is there still a way to express activator through Func<>?

도움이 되었습니까?

해결책

This is still very possible, the method by which you configure your template service has changed as I wanted to centralise all configuration into one interface. The following url (https://github.com/Antaris/RazorEngine/blob/release-3.0/src/Core/Tests/RazorEngine.Core.Tests/ActivatorTestFixture.cs) is an example of a unit test I've built that demonstrates using DI with Unity, this one specifically is using an activator.

A DelegateActivator does exist in the RazorEngine.Templating namespace that will allow you to achieve what you want. I'm looking at ways at trying to simplifying this. You could also configure it using the Fluent configuration API:

var config = new FluentTemplateServiceConfiguration(c =>
    c.ActivateUsing(context => {your code here}));

var service = new TemplateService(config);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top