在mvc asp.net中,我可以重写一个工厂来创建我的控制器,因此在这里提供一个对我的IOC的引用。这样做我的控制器的构造函数所需的每个接口都将由我的IOC提供。IOC和Silverlight
有没有一些常用的方法来使用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指南? – mcabral
刚开始使用Silverlight和MVVM时,我看到了Prism,但我打算在阶段2中进入它。 – Arthis