【发布时间】:2011-09-02 08:40:47
【问题描述】:
在 mvc asp.net 中,我可以重写一个工厂来创建我的控制器,所以在这里放一个对我的 IOC 的引用。我的控制器的构造函数需要的每个接口都将由我的 IOC 提供。
是否有一些使用 Silverlight 的常用方法? 目前我只发现到处都在使用 Ninject 的内核:
public partial class MyUserControlSL
{
public MyUserControlSL()
{
DataContext = new MyViewModel(Kernel.Get<IMyRepository>());
InitializeComponent();
}
}
例如使用 StructureMap 和 MVC:
public class ControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(
RequestContext requestContext, Type controllerType)
{
IController result = null;
try
{
if (controllerType != null)
{
result = ObjectFactory.GetInstance(controllerType)
as Controller;
}
else
{
return base.GetControllerInstance(
requestContext, controllerType);
}
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(
ObjectFactory.WhatDoIHave());
throw;
}
return result;
}
}
public AController(IServiceA serviceA)
{
if (serviceA == null)
{
throw new Exception("IServiceA cannot be null");
}
_ServiceA = serviceA;
}
public ServiceA(IRepositoryA repository)
{
if (repository == null)
{
throw new Exception(
"the repository IRepositoryA cannot be null");
}
_Repository = repository;
}
感谢您的帮助,如果不清楚请询问..
【问题讨论】:
-
您是否按照 PRISM 指南工作?
-
刚开始使用 silverlight amd MVVM,我看到了 Prism,但我打算在第 2 阶段进入它。
标签: c# .net silverlight dependency-injection inversion-of-control