2011-04-28 8 views
1

내 컨트롤러 생성자 중 몇 개가 인터페이스 (IPetInterface)를 사용한다고 가정 해 봅시다. IPetInterface의 세 가지 구체적인 구현이 있습니다.MVC3 & StructureMap, 컨트롤러에 기반한 구체적인 클래스 삽입하기

구조체가 필요한 컨트롤러를 기반으로 구체적인 구현 중 하나를 제공하도록 구조체를 구성하는 방법은 무엇입니까?

원유 예 ....

class DogStuff: IPetInterface{} 

class CatStuff: IPetInterface{} 

class GiraffeStuff: IPetInterface{} 

class DogController : Controller 
{ 
    DogController(IPetInterface petStuff) 

    // some other stuff that is very unique to dogs 
} 

class CatController : Controller 
{ 
    CatController(IPetInterface petStuff) 

    // some other stuff that is very unquie to cats 
} 
+0

저는 StructureMap으로 할 수 있다고 확신합니다. (저는 Unity 자신보다 더 많은 사람입니다. 그래서 StructureMap으로 정확히 어떻게할지는 모르겠지만) 디자인이 맞습니까? 귀하의 설명을 바탕으로, 그것은 인터페이스가 ~ 일반 수 있습니다 것 같습니다 .... – BFree

+0

@ BFree : 아마도 너무 일반적입니다. 현재 각각의 애완 동물을위한 분리 된 인터페이스를 가지고 있지만 이것이 동일하므로 궁금해합니다. – Dve

답변

5

을이 등록 할 것 :

For<DogController>().Use<DogController>() 
    .Ctor<IPetInterface>("petStuff").Is<DogStuff>(); 
For<CatController>().Use<CatController>() 
    .Ctor<IPetInterface>("petStuff").Is<CatStuff>(); 
For<GiraffeController>().Use<GiraffeController>() 
    .Ctor<IPetInterface>("petStuff").Is<GiraffeStuff>(); 

하는 경우 이것은 내가 규칙을 기반으로하는 등록을 사용하여 조사 할 것 인 동일한 패턴을 가진 3 개의 등록 이상으로 증가한다. ead는 네이밍을 기반으로 각 컨트롤러에 해당하는 "stuff"를 자동으로 등록합니다. 이것은 using an IRegistrationConvention이 될 수 있습니다.

+0

감사합니다. – Dve

2

이 시도 : 질문에 제공되는 클래스 및 인터페이스와

class Stuff<T> : IPetInterface<T> where T : IPet { ... } 

interface IPetInterface<T> where T : IPet { ... } 

abstract class PetController<T> : Controller where T : IPet 
{ 
    protected PetController<T>(IPetInterface<T> stuff) 
    { ... } 
} 

class CatController : PetController<Cat> 
{ 
    public CatController(IPetInterface<Cat> stuff) : base(stuff) {} 

    ... 
} 

class DogController : PetController<Dog> { ... } 
관련 문제