사전의 「아이」콘트롤러의리스트를 보관 유지하는 팩토리를 작성하려고하고 있습니다. 이러한 하위 컨트롤러는 다른 하위 컨트롤러에 액세스 할 수 있도록 팩토리를 포함해야합니다. 여기팩토리를 포함한 컨트롤러를 포함한 팩토리를 작성하는 방법
public interface IChildControllerFactory
{
IChildController GetChildController(object item);
}
public class ChildControllerFactory : IChildControllerFactory
{
private Dictionary<string, IChildController> _childControllerDictionary;
public ChildControllerFactory(IEnumerable<IChildController> childControllers)
{
var childControllerDictionary = new Dictionary<string, IChildController>();
//code to build dictionary
_childControllerDictionary = childControllerDictionary;
}
public IChildController GetChildController(object item)
{
return _childControllerDictionary.Where(x => x.Key == item.GetType().Name).Select(x => x.Value).First();
}
}
아이 컨트롤러의 예입니다 :
public class ChildController : Controller, IChildController<TViewModel, TModel>
{
private IChildControllerFactory _childControllerFactory;
public ChildController(IChildControllerFactory childControllerFactory) //this will cause a circular reference
{
_childControllerFactory = childControllerFactory;
}
public ActionResult GetChildViewModel(object child)
{
var model = _childControllerFactory.GetChildController(child).ToViewModel(survey);
return PartialView("Child.ascx", model);
}
}
그리고 상속 된 인터페이스 :
public interface IChildController<TViewModel, TModel>
{
int Create(object item);
object Save(object item);
object ToViewModel(object item);
}
아이 컨트롤러는 모든 등록 및 공장 여기에 공장입니다 구조 맵과 함께 싱글 톤으로 지정됩니다.
자식 컨트롤러에 팩토리가 필요하므로 종속성 삽입을 사용하여 생성자에서 팩터 리를 설정하면 순환 참조가 발생합니다. 처음 공장 초기화 될 때 모든 자식 컨트롤러가 초기화되고 각 자식 컨트롤러가 초기화하려고하므로 공장 (이미 실행 중).
어떻게하면 모든 하위 컨트롤러를 인스턴스화하고 해당 하위 컨트롤러에서 액세스 할 수 있도록 팩토리를 가져올 수 있습니까?
이것은 대부분의 문제를 해결하는 것 같지만 _childControllerFactory는 페이지의 요청이있을 때 어떻게 설정됩니까? 우리는 생성자에서 의존성 삽입을 사용할 수 없다는 것을 알고 있지만 초기화 메소드를 재정의하여 팩토리의 인스턴스를 가져오고 설정할 수 있다고 가정합니다. 이것이 권장 될까요? –
당신은 이것을 말하고 있습니까? http://bubblogging.wordpress.com/2012/06/04/mvc-controller-factory/ – dierre