首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带unity的Caliburn.micro

带unity的Caliburn.micro
EN

Stack Overflow用户
提问于 2013-04-01 03:43:31
回答 2查看 2.5K关注 0票数 4

目前,我正在学习更多关于caliburn.micro的知识,它令人惊叹。

在我的带有一些导航的小演示项目中,有MEF和EventAggregator。

因为我想在一个已经在使用unity的项目中使用caliburn.micro,所以我想使用Unity for DI。

如何将引导程序设置为使用Unity?

除了MindScape教程和codeplex页面上的教程之外,任何好的教程都是非常受欢迎的。(除了我没有看到正确的人来处理这个案例)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-01 03:51:15

在使用Boostrapper<T>时,您必须在引导程序中重写4个方法来连接IoC容器:

  1. Configure()

这通常是初始化容器并注册所有dependencies.

  • GetInstance(string, Type)的地方

按类型和键检索对象-据我所知,框架通常用它来检索视图模型、常规依赖关系等。因此,在这里,您的容器必须以某种方式获得一个基于类型和/或Type (通常是类型,当您使用视图优先绑定将模型连接到视图时使用字符串)的实例。通常容器都有类似的内置方法,它们开箱即用,或者只需要一点adjustment.

  • GetAllInstances(Type)

对象集合的检索(仅按类型)-同样,根据我的经验,这通常用于views.

  • BuildUp(object)的Caliburn.Micro

可以对对象进行属性注入的方法。

如果你感兴趣,我有一个使用SimpleInjector (或Autofac)的样本,不幸的是我没有使用Unity的经验。

编辑

示例时间!这一个使用的是SimpleInjector。

代码语言:javascript
复制
public class MainViewModel
{
//...
}

public class ApplicationBootstrapper : Bootstrapper<MainViewModel>
{
    private Container container;

    protected override void Configure()
    {
        container = new Container();

        container.Register<IWindowManager, WindowManager>();
      //for Unity, that would probably be something like:
      //container.RegisterType<IWindowManager, WindowManager>();
        container.RegisterSingle<IEventAggregator, EventAggregator>();

        container.Verify();
    }

    protected override object GetInstance(string key, Type service)
    {
        // Now, for example, you can't resolve dependency by key in SimpleInjector, so you have to
        // create the type out of the string (if the 'service' parameter is missing)
        var serviceType = service;
        if(serviceType == null)
        {
            var typeName = Assembly.GetExecutingAssembly().DefinedTypes.Where(x => x.Name == key).Select(x => x.FullName).FirstOrDefault();
            if(typeName == null)
                throw new InvalidOperationException("No matching type found");

            serviceType = Type.GetType(typeName);
        }

        return container.GetInstance(serviceType);
        //Unity: container.Resolve(serviceType) or Resolve(serviceType, name)...?

    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return container.GetAllInstances(service);
        //Unity: No idea here.
    }

    protected override void BuildUp(object instance)
    {
        container.InjectProperties(instance);
        //Unity: No idea here.
    }
}
票数 8
EN

Stack Overflow用户

发布于 2013-06-29 15:05:16

我发现Wiki for CM非常有用,而且来自Unity/CSL背景的Bootstrapper<TRootModel>方法名称和签名都很直观。

我想分享我自己的UnityBootstrapper<TRootModel>类,它利用了Unity3,它很小,干净,做了你想要的事情。

代码语言:javascript
复制
/// <summary>
/// <para>Unity Bootstrapper for Caliburn.Micro</para>
/// <para>You can subclass this just as you would Caliburn's Bootstrapper</para>
/// <para>http://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper</para>
/// </summary>
/// <typeparam name="TRootModel">Root ViewModel</typeparam>
public class UnityBootstrapper<TRootModel>
    : Bootstrapper<TRootModel>
    where TRootModel : class, new()
{
    protected UnityContainer Container
    {
        get { return _container; }
        set { _container = value; }
    }

    private UnityContainer _container = new UnityContainer();

    protected override void Configure()
    {
        if (!Container.IsRegistered<IWindowManager>())
        {
            Container.RegisterInstance<IWindowManager>(new WindowManager());
        }
        if (!Container.IsRegistered<IEventAggregator>())
        {
            Container.RegisterInstance<IEventAggregator>(new EventAggregator());
        }
        base.Configure();
    }

    protected override void BuildUp(object instance)
    {
        instance = Container.BuildUp(instance);
        base.BuildUp(instance);
    }

    protected override IEnumerable<object> GetAllInstances(Type type)
    {
        return Container.ResolveAll(type);
    }

    protected override object GetInstance(Type type, string name)
    {
        var result = default(object);
        if (name != null)
        {
            result = Container.Resolve(type, name);
        }
        else
        {
            result = Container.Resolve(type);
        }
        return result;
    }
}

您可以直接实例化它,提供有效的泛型类型参数,或者您可以子类化和自定义诸如Lifetime Managers之类的东西:

代码语言:javascript
复制
public class AppBootstrapper
    : UnityBootstrapper<ViewModels.AppViewModel>
{
    protected override void Configure()
    {
        // register a 'singleton' instance of the app view model
        base.Container.RegisterInstance(
            new ViewModels.AppViewModel(), 
            new ContainerControlledLifetimeManager());

        // finish configuring for Caliburn
        base.Configure();
    }
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15733713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档