告诉StructureMap使用特定的构造 [英] Tell StructureMap to use a specific constructor

查看:241
本文介绍了告诉StructureMap使用特定的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要的XPathDocument 两项服务。我希望能够定义 XPathDocumnet 命名实例在两个服务的配置中使用。我也希望能够告诉StuctureMap使用哪个的XPathDocument 的构造函数。当我试图让的XPathDocument 的一个实例,它告诉我,它无法找到的XmlReader 堵塞的类型。我想使用需要的XML文件中的字符串URI的构造。我似乎无法得到这个工作。这里是StructureMap的配置代码



 公共类服务1:IService1 {
公共服务1(XPathDocument的文档){}
}
公共类服务2:IService2 {
公共服务2(XPathDocument的文档){}
}

公共类Registry1:注册表{
ForRequestedType< ; IService1方式>()TheDefault.Is.OfConcreteType&所述;服务1>()
.CtorDependency&所述; XPathDocument的>()
。就是(X => x.TheInstanceNamed(XPathDocument1));
ForRequestedType< IService2>()TheDefault.Is.OfConcreteType<客服2>()
.CtorDependency< XPathDocument的>()
。是(X => x.TheInstanceNamed(XPathDocument2 ));

ForRequestedType&所述; XPathDocument的方式>()AddInstances(X => {
x.OfConcreteType&所述; XPathDocument的>()
.WithCtorArg(URI)EqualToAppSetting(XmlFile1。 )
.WithName(XPathDocument1);
x.OfConcreteType<&的XPathDocument GT;()
.WithCtorArg()。EqualToAppSetting(URI XmlFile2)
.WithName (XPathDocument2);
});
}


解决方案

看的this 。总之,你需要修改 OfConcreteType<服务1>() ConstructedBy(()=>新建服务1());


I have two services that require an XPathDocument. I want to be able to define named instances of XPathDocumnet to use in the configuration of the two services. I also want to be able to tell StuctureMap which constructor of XPathDocument to use. When I try to get an instance of XPathDocument it tells me that it can't find the plugged type for XmlReader. I want to use the constructor that requires a string uri for the xml file. I cannot seem to get this to work. Here is the StructureMap configuration code.

public class Service1 : IService1 {
    public Service1(XPathDocument document) {}
}
public class Service2 : IService2 {
    public Service2(XPathDocument document) {}
}

public class Registry1 : Registry {
    ForRequestedType<IService1>().TheDefault.Is.OfConcreteType<Service1>()
        .CtorDependency<XPathDocument>()
        .Is(x => x.TheInstanceNamed("XPathDocument1"));
    ForRequestedType<IService2>().TheDefault.Is.OfConcreteType<Service2>()
        .CtorDependency<XPathDocument>()
        .Is(x => x.TheInstanceNamed("XPathDocument2"));

    ForRequestedType<XPathDocument>().AddInstances(x => {
        x.OfConcreteType<XPathDocument>()
            .WithCtorArg("uri").EqualToAppSetting("XmlFile1")
            .WithName("XPathDocument1");
        x.OfConcreteType<XPathDocument>()
            .WithCtorArg("uri").EqualToAppSetting("XmlFile2")
            .WithName("XPathDocument2");
    });
}

解决方案

Look at this. In short, you need to change OfConcreteType<Service1>() to ConstructedBy(() => new Service1());.

这篇关于告诉StructureMap使用特定的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆