IOC 和 Silverlight

发布于 2024-12-02 18:06:08 字数 1599 浏览 0 评论 0 原文

在 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;
}

感谢您的帮助,如果不清楚,请询问..

In mvc asp.net, I can override a factory to create my controllers, and so put a reference to my IOC just here. Doing So every interface needed by the constructor of my controllers will be feeded by my IOC .

Is there some common way to do it using Silverlight?
At the moment I only found to use the kernel of Ninject everywhere :

public partial class MyUserControlSL   
{
    public MyUserControlSL()
    {
        DataContext = new MyViewModel(Kernel.Get<IMyRepository>());
        InitializeComponent();
    }
}

eg using StructureMap and 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;
}

Thanks for your help, please ask if it is not clear..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人如初 2024-12-09 18:06:08

在 Silverlight 中,您应该在组合根使用引导程序来连接整个对象图。它可能是应用程序类 app.xml.cs ,看起来类似于

public partial class App : Application
{
   private void Application_Startup(object sender, StartupEventArgs e)
   {
       Bootstrapper bootstrapper = new Bootstrapper();
       bootstrapper.Run();
   }
}

一般来说这应该足够了,但如果您的视图需要一个单独的工厂类,请查看
保留 DI 容器的使用情况Silverlight 和 MVVM 的组合根源

In Silverlight you should use a bootstrapper at composition root to wire up your entire object graph. It could be the Application class app.xml.cs and look similar to

public partial class App : Application
{
   private void Application_Startup(object sender, StartupEventArgs e)
   {
       Bootstrapper bootstrapper = new Bootstrapper();
       bootstrapper.Run();
   }
}

In general this should be sufficant but if you need a separate Factory class for your views, take a look at
Keeping the DI-container usage in the composition root in Silverlight and MVVM.

梦途 2024-12-09 18:06:08

对于 Silverlight,您可以使用带有自定义 IoC 容器的 PRISM 框架。

For Silverlight you may use PRISM framework with custom IoC container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文