使用StructureMap配置具体类的属性

时间:2018-06-26 21:42:55

标签: c# dependency-injection structuremap

我在Application实例中使用了一组通用的电子邮件类。它们都共享相同的接口,但是我需要(可选)“填充”另一个“公共”类型的实例作为属性。

从此处开始:鉴于此起点...
注意,MyApplication从所有3个属性开始。而且,由于DistributionListProvider是唯一配置的IInstanceProvider,因此不会出现任何问题。

public class MyApplication : IMyApplication
{       
    #region notifications

    [SetterProperty]
    public IEmailNotification RequestToFlowEmailNotification { get; set; }

    [SetterProperty]
    public IEmailNotification ApprovalToFlowEmailNotification{ get; set; }

    #endregion

    #region instance providers

    [SetterProperty]
    public IInstanceProvider DistributionListProvider { get; set; }

    #endregion
}

public class RequestToFlowEmailNotification : IEmailNotification
{
    #region <Properties>

    [SetterProperty]
    public IInstanceProvider DistributionListProvider { get; set; }

    #endregion
}

public class ApprovalToFlowEmailNotification : IEmailNotification
{

}

public class DistributionListProvider : ComponentProviderBase, IInstanceProvider
{

}

允许添加:现在,让我们创建另一个 IInstanceProvider 并将其添加到APPLICATION ...
但是首先,请注意IEmailNotification和IInstanceProvider是常见类型,而不是“通用类型” ...意味着它们不是EmailNotificationFor<T>类型。这样就改变了您在注册表中配置它们的方式。

// NOW...LETS ADD THIS !!
public class AuthorizationToFlowMeterDocumentRecallProvider : ComponentProviderBase, IInstanceProvider
{

}

public class MyApplication : IMyApplication
{       
    #region notifications

    [SetterProperty]
    public IEmailNotification RequestToFlowEmailNotification { get; set; }

    [SetterProperty]
    public IEmailNotification ApprovalToFlowEmailNotification{ get; set; }

    #endregion

    #region instance providers

    [SetterProperty]
    public IInstanceProvider DistributionListProvider { get; set; }

    [SetterProperty]
    public IInstanceProvider AuthorizationToFlowMeterDocumentRecallProvider{ get; set; }

    #endregion
}

这时StructureMap不再需要了解如何填写...

  • RequestToFlowEmailNotification.DistributionListProvider

问:如何更新注册表以强制Structure正确填充?
下面是我的整个注册表...

注意:
我不想设置默认值...是否可以在不设置默认值的情况下执行此操作?

public ContainerRegistry()
{
    Scan(
        scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.LookForRegistries();
            scan.SingleImplementationsOfInterface();
        });

    // ------------
    // UNIT OF WORK
    // ------------
    // DbContext
    For<DbContext>().Use<MeasurementContractsDbContext>();

    // UnitOfWork
    For<IMeasurementContractsUnitOfWork>().Use<MeasurementContractsUnitOfWork>();

    // GenericRepository
    For(typeof(ICompositeRepository<>)).Use(typeof(GenericRepository<>));

    // --------
    // HELPERS
    // --------
    For<IWindowsIdentityHelper>().Use<WindowsIdentityHelper>();

    For<ISmtpClientHelper>().Use<SmtpClientHelper>()
        .Ctor<ISmtpClient>().Is(new SmtpClient());

    For<IPdfConverterHelper>().Use<PdfConverterHelper>()
        .Ctor<IPdfConverterClient>().Is(new SelectPdfUrlConverterClient());

    // WARNING: Do not remove without replacing it with "some kind of" IConstructorSelector, so that, Unit Testing can be done
    For<IDataServiceFor<EmployeeData>>()
        .Use(x => new EmployeeDataService(new ODataProxyV4())); //<-- uses this constructor

    // --------
    // WORKFLOW
    // ---------
    For<IWorkflowProvider>().Use<WorkflowProvider>()
        .Ctor<Assembly>().Is(Assembly.GetExecutingAssembly());

    // --------
    // MANAGERS
    // --------
    For<IManager<Device>>().Use<DeviceManager>();
    For<IManager<Favorite>>().Use<FavoritesManager>();
    For<IManager<User>>().Use<UserManager>();

    // --------
    // DEPENDENCY MANAGERS
    // --------
    For<IDocumentDependency>().Use<DeviceAffinityProvider>();

    // --------
    // TRANSFORMERS
    // --------
    For<IXmlTransformerFor<AuthorizationToFlowMeterDocumentXmlDataSet>>().Use<AuthorizationToFlowMeterDocumentXmlTransformer>();
    For<IXmlTransformerFor<FirstDeliveryNoticeDocumentXmlDataSet>>().Use<FirstDeliveryNoticeDocumentXmlTransformer>();
    For<IXmlTransformerFor<RequestToFlowMeterDocumentXmlDataSet>>().Use<RequestToFlowMeterDocumentXmlTransformer>();

    // --------
    // PROVIDERS: DataItem Providers
    // --------
    For<IDataItemProviderFor<EmployeeData>>().Use<EmployeeDataProvider>();

    // --------
    // PROVIDERS: Document Providers
    // --------
    For<IDataItemProviderFor<RequestToFlowMeterDocument>>().Use<RequestToFlowMeterDocumentProvider>();
    For<IDataItemProviderFor<AuthorizationToFlowMeterDocument>>().Use<AuthorizationToFlowMeterDocumentProvider>();
    For<IDataItemProviderFor<FirstDeliveryNoticeDocument>>().Use<FirstDeliveryNoticeDocumentProvider>();

    // --------
    // COMMANDS
    // --------
    For<IAdminUpdateCommandFor<AuthorizationToFlowMeterDocument>>().Use<AuthorizationToFlowMeterDocumentAdminUpdateCommand>();

    // -----------
    // APPLICATION
    // -----------
    For<IMyApplication>().Use<MyApplication>()

        // Instance Provider
        .Setter(x => x.DistributionListProvider).Is<DistributionListProvider>()
        .Setter(x => x.AuthorizationToFlowMeterDocumentRecallProvider).Is<AuthorizationToFlowMeterDocumentRecallProvider>()

        // Email Notifications
        .Setter(x => x.ApprovalToFlowEmailNotification).Is<ApprovalToFlowEmailNotification>()
        .Setter(x => x.DenialToFlowEmailNotification).Is<DenialToFlowEmailNotification>()
        .Setter(x => x.ApprovalToFlowRecalledEmailNotification).Is<ApprovalToFlowRecalledEmailNotification>()
        .Setter(x => x.RequestToFlowEmailNotification).Is<RequestToFlowEmailNotification>()
        .Setter(x => x.FirstDeliveryNoticeEmailNotification).Is<FirstDeliveryNoticeEmailNotification>();
}

1 个答案:

答案 0 :(得分:2)

使用[SetterProperty]属性,StructureMap将尝试为该对象的属性构建并附加一个值。

但是,setter属性需要默认实现,因为当存在多个实现时,或者尤其是未添加默认实现时,它无法确定要使用哪个实现。

对于RequestToFlowEmailNotification.DistributionListProvider,容器不知道要使用哪个实现。

要覆盖此行为,请为RequestToFlowEmailNotification.DistributionListProvider使用内联设置器配置

//...
.Setter(x => x.RequestToFlowEmailNotification)
    .Is<RequestToFlowEmailNotification>(_ => 
        _.Setter(d => d.DistributionListProvider)
            .Is<DistributionListProvider>()); //<<-- change to which ever one is needed

请注意,Setter配置的链接是为了建立RequestToFlowEmailNotification,然后是内部DistributionListProvider