Pergunta

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<>?

Foi útil?

Solução

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top