Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

c# - How to map same interface to different ConcreteClasses with StructureMap?

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 
{
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...