UnityContainer.BuildUp()-仅当这些实例为null时,我才能使其将新实例注入属性吗? [英] UnityContainer.BuildUp() - Can I make it inject new instances into properties only if these are null?

查看:169
本文介绍了UnityContainer.BuildUp()-仅当这些实例为null时,我才能使其将新实例注入属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在反序列化此类课程

I'm deserializing a class like this one

class AClass{
    [Dependency]
    AnotherClass Property{ get; set; }
}

当我随后对对象进行BuildUp()处理时,我希望Unity仅在属性为null的情况下创建AnotherClass的新实例,否则仅对其执行BuildUp. 有没有简单的方法可以做到这一点?

When I then BuildUp() the object I'd like Unity to create a new instance of AnotherClass only if the property is null and otherwise just perform a BuildUp on it. Is there a simple way to achieve this?

我正在用wpf做mvvm.这些类是视图模型,我将它们序列化,因为我希望在两次运行之间保留一些属性,并且它们还具有一些要统一注入的依赖项.因此,在反序列化之后,嵌套模型已经设置了属性,因此我不希望统一用新实例覆盖它,但我仍然希望它在其上调用InjectionMethods并在程序第一次运行时正常解析它,并且嵌套模型为空.

I'm doing mvvm with wpf. The classes are view-models, I serialize them as there are some properties I want to preserve between runs and they also have some dependencies I want unity to inject. So after deserialization the nested model is already there with properties set so I don't want unity to override it with a new instance but I still want it to call InjectionMethods on it and also to resolve it normally the first time the program is run and the nested models are null.

推荐答案

我最终写了一个unity扩展. 我找不到有关如何执行此操作的大量文档,因此我不确定结果如何. 似乎可行,但我有种感觉,我可能写的太恐怖了. 无论如何,这里是代码,欢迎进行改进:

I ended up writing a unity extension. I could not find much documentation on how to do this so I'm not really sure about the result. It seems to work but I have this feeling I might have written something horrible.. Anyway here's the code, improvements are welcome:

public class RecursiveBuildUpContainerExtension : UnityContainerExtension {
        protected override void Initialize(){
            Context.Strategies.Add( new RecursiveBuildUpBuilderStrategy( Context.Container ), UnityBuildStage.PreCreation );
        }
    }

    public class RecursiveBuildUpBuilderStrategy : BuilderStrategy {
        readonly IUnityContainer container;
        public RecursiveBuildUpBuilderStrategy( IUnityContainer container ) {
            this.container = container;
        }

        public override void PreBuildUp( IBuilderContext context ) {

            if( context.Existing == null ) return;

            foreach( var prop in context.Existing.GetType( ).GetProperties( ) ) {

                if( ContainsType<DependencyAttribute>( prop.GetCustomAttributes( true ) ) ) {

                    if( prop.GetValue( context.Existing, null ) == null ) {
                        var value = container.Resolve( prop.PropertyType );
                        prop.GetSetMethod( ).Invoke( context.Existing, new[] { value } );
                    }
                    else {
                        var value = container.BuildUp( prop.PropertyType, prop.GetValue( context.Existing, null ) );
                        prop.GetSetMethod( ).Invoke( context.Existing, new[] { value } );
                    }
                }
            }

            foreach (var method in context.Existing.GetType().GetMethods() ){
                if( ContainsType<InjectionMethodAttribute>( method.GetCustomAttributes( true ))){
                    var argsInfo = method.GetParameters( );
                    var args = new object[argsInfo.Length];

                    for( int i = 0; i < argsInfo.Length; i++ ) {
                        args[i] = container.Resolve( argsInfo[i].ParameterType );
                    }

                    method.Invoke( context.Existing, args );
                }
            }

            context.BuildComplete = true;
        }

        private static bool ContainsType<T>( IEnumerable<object> objects ){
            foreach (var o in objects){
                if( o is T ) return true;
            }
            return false;
        }

    }

这篇关于UnityContainer.BuildUp()-仅当这些实例为null时,我才能使其将新实例注入属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆