映射相同的接口与StructureMap不同ConcreteCl

映射相同的接口与StructureMap不同ConcreteCl

本文介绍了如何映射相同的接口与StructureMap不同ConcreteClasses?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当正在创建控制器1,我想这IService将被映射到ConcreteService1和IPageService到ConcretePageService1



和创建控制器2的时候,我想这IService将被映射到ConcreteService2和IPageService到ConcretePageService2



我怎么能初始化的ObjectFactory使上面的将工作?



平均值,而我initialezed的ObjectFactory是这样的:

  ObjectFactory.Initialize(X => 
{
x.For< ; IService>()使用<。ConcreteService1>();
x.For&所述; IPageService>()使用<。ConcretePageService1>();
});



但是,这始终映射的 ConcreteService1 以IService和 ConcretePageService1 以IPageService无论控制器类型

 公共类控制器1:控制器
{
公共控制器1(IService服务,IPageService pageService)
{
}
}

公共类控制器2:控制器
{
公共控制器2(IService服务,IPageService pageService )
{
}
}

公共接口IService
{
}
公共类ConcreteService1:IService
{
}
公共类ConcreteService2:IService
{
}

公共接口IPageService
{
}
公共类ConcretePageService1:IPageService
{
}
公共类ConcretePageService2:IPageService
{
}


解决方案

如果它只是一个孤立的注册,您可以使用命名实例映射一个特定的实例到每个控制器。

 对少于IService>()添加< ConcreteService1>()命名(服务1); 
对少于IService方式>()添加< ConcreteService2>()命名(服务2);
对少于IPageService方式>()添加< PageService1>()命名(pageService1);
对少于IPageService方式>()添加< PageService2>()命名(pageService2);
。对于<控制器1>()使用<控制器1>()
.Ctor< IService方式>()是(C => c.GetNamedInstance< IService>(服务1))
.Ctor&所述; IPageService方式>()是(
C => c.GetNamedInstance&下; IPageService>(pageService1));
。对于<控制器2>()使用<控制器2>()
.Ctor< IService方式>()是(
C => c.GetNamedInstance< IService>(服务2))
.Ctor&所述; IPageService方式>()是(
C => c.GetNamedInstance&下; IPageService>(pageService2));

如果这是在应用重复的模式,你应该使用的以类型,以避免这一切的重复映射。



添加按类型命名的类型名称是可以使用内置的约定

 扫描(X => 
{
的X. AssembliesFromApplicationBaseDirectory();
x.AddAllTypesOf< IService>()NameBy(类型=> type.Name);
x.AddAllTypesOf< IPageService>()NameBy(类型=方式> type.Name );
x.WithDefaultConventions();
});


When Controller1 is being created, I want that IService will be mapped to ConcreteService1 and IPageService to ConcretePageService1

And when Controller2 is created, I want that IService will be mapped to ConcreteService2 and IPageService to ConcretePageService2

How I can initialize ObjectFactory so that the above will work?

Mean while I initialezed ObjectFactory this way:

 ObjectFactory.Initialize(x =>
    {
         x.For<IService>().Use<ConcreteService1>();
         x.For<IPageService>().Use<ConcretePageService1>();
    });

But this ALWAYS maps ConcreteService1 to IService and ConcretePageService1 to IPageService regardless of controller type

public class Controller1 : Controller
{
    public Controller1(IService service, IPageService pageService)
    {
    }
}

public class Controller2 : Controller
{
     public Controller2(IService service, IPageService pageService)
     {
     }
}

public interface IService
{
}
public class ConcreteService1:IService
{
}
public class ConcreteService2:IService
{
}

public interface IPageService
{
}
public class ConcretePageService1:IPageService
{
}
public class ConcretePageService2:IPageService
{
}
解决方案

If it's just an isolated registration you can use named instances to map a specific instance to each controller.

For<IService>().Add<ConcreteService1>().Named("service1");
For<IService>().Add<ConcreteService2>().Named("service2");
For<IPageService>().Add<PageService1>().Named("pageService1");
For<IPageService>().Add<PageService2>().Named("pageService2");
For<Controller1>().Use<Controller1>()
  .Ctor<IService>().Is(c => c.GetNamedInstance<IService>("service1"))
  .Ctor<IPageService>().Is(
    c => c.GetNamedInstance<IPageService>("pageService1"));
For<Controller2>().Use<Controller2>()
  .Ctor<IService>().Is(
    c => c.GetNamedInstance<IService>("service2"))
  .Ctor<IPageService>().Is(
    c => c.GetNamedInstance<IPageService>("pageService2"));

If this is a pattern that's repeated in the application you should use a convention to map the types in order to avoid all this duplication.

Adding types named by type name is possible using a built in convention.

Scan(x =>
  {
    x.AssembliesFromApplicationBaseDirectory();
    x.AddAllTypesOf<IService>().NameBy(type => type.Name);
    x.AddAllTypesOf<IPageService>().NameBy(type => type.Name);
    x.WithDefaultConventions();
  });

这篇关于如何映射相同的接口与StructureMap不同ConcreteClasses?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:17