Ninject WithConstructorArgument to Simple Injector Implementation

时间:2018-05-03 14:33:58

标签: c# dependency-injection ninject simple-injector

Ninject的WithConstructorArgument到Simple Injector实现的直接实现是什么?

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

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

public EmailTemplatesService(
            EmailTemplates templates,
            IEventEmailTemplatesRepository eventEmailTemplatesRepository)

1 个答案:

答案 0 :(得分:0)

在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所述。