Ninject WithConstructorArgument 到简单的注入器实现

Ninject WithConstructorArgument to Simple Injector Implementation

从 Ninject 的 WithConstructorArgument 到简单注入器实现的直接实现是什么?

 var emailTemplates = new EmailTemplates
 {
      MasterPageTemplate = MVC.Email.Views._Layout
 };

 container.Register<IEmailService, EmailService>()
     .WithConstructorArgument("templates", emailTemplates);

Class

public EmailTemplatesService(
            EmailTemplates templates,
            IEventEmailTemplatesRepository eventEmailTemplatesRepository)

在 Simple Injector 中,您可以这样做:

container.Register<IEmailService>(() => new EmailService(
    emailTemplates,
    container.GetInstance<IEventEmailTemplatesRepository>()));

或者您可以这样做:

// Wrap the string value into a DTO and inject that
public EmailService(
    EmailServiceSettings settings, IEventEmailTemplatesRepository r) { ... }

container.RegisterInstance(new EmailServiceSettings(emailTemplates));
container.Register<IEmailService, EmailService>();

或者您可以覆盖参数注入行为,如here所述。