在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