我有两个wcf休息服务如下
using StructureMap;
namespace x.services
{
public class Global : System.Web.HttpApplication
{
ObjectFactory.Configure(x =>
{
/* How do I configure Structure map to Use
x.services.service1.Validator for constructing
x.services.service1.Service1 Instances and
x.services.service2.Validator for constructing
x.services.service2.Service2 Instances ** */
}
}
public interface IValidator
{
/*...*/
}
}
namespace x.services.service1
{
public class Service1 : IService1
{
public Service1(IValidator validator)
{
/*...*/
}
}
public class Validator : IValidator
{
/*...*/
}
}
namespace x.services.service2
{
public class Service2 : IService2
{
public Service2(IValidator validator)
{
/*....*/
}
}
public class Validator : IValidator
{
/*...*/
}
}
问题也在这里
如何配置Structure map以使用x.services.service1.Validator构建x.services.service1.Service1 Instances和x.services.service2.Validator以构建x.services.service2.Service2实例< /强>
答案 0 :(得分:2)
我们可以使用显式映射:
// As precise IService1 mapping as possible
x.For<IService1>()
.Use<x.services.service1.Service1>()
.Ctor<IValidator>("validator")
.Is<x.services.service1.Validator>();
// IService2
x.For<IService2>()
.Use<x.services.service2.Service2>()
.Ctor<IValidator>("validator")
.Is<x.services.service2.Validator>();
编辑:制作一些类型的单身人士
x.For<x.services.service2.Validator>()
.Singleton()
.Use<x.services.service2.Validator>();