Exemplo n.º 1
0
        public override IDecorationOf <ICondition> ApplyThisDecorationTo(ICondition thing)
        {
            this.Logger.LogVerbose("ApplyThisDecorationTo started", thing);

            IDecorationOf <ICondition> rv = null;

            try
            {
                rv = new LoggingConditionDecoration(thing, this.Logger);
            }
            catch (Exception ex)
            {
                this.Logger.LogError(ex.Message, null, ex);
                throw;
            }
            finally
            {
                this.Logger.LogVerbose("ApplyThisDecorationTo completed", null);
            }

            return(rv);
        }
Exemplo n.º 2
0
        /// <summary>
        /// sets the Decorated property.  If null, kacks
        /// </summary>
        /// <param name="decorated"></param>
        protected void SetDecorated(T decorated)
        {
            if (decorated == null)
            {
                throw new InvalidOperationException("null decoration injection");
            }

            if (decorated is ISealedDecoration)
            {
                throw new InvalidOperationException("Cannot decorate a SealedDecoration");
            }

            //if decorated is a decoration, we must ensure that none of the decoration layers are equal to this
            //or we'll get a circ reference situation
            var decorationList = decorated.GetAllDecorations();

            //remove the first decoration because it is equivalent to "this"

            if (decorationList != null)
            {
                foreach (var each in decorationList)
                {
                    if (object.ReferenceEquals(each, this))
                    {
                        throw new InvalidOperationException("circular reference");
                    }
                }
            }

            this._Decorated = decorated;

            if (decorated is IDecorationOf <T> )
            {
                IDecorationOf <T> dec = decorated as IDecorationOf <T>;
                this._Core = dec.Core;
            }
            else
            {
                this._Core = decorated;
            }

            //set the decorator backreference
            if (decorated.IsADecoratorAwareDecoration())
            {
                (decorated as IDecoratorAwareDecoration).Decorator = this;
            }

            //if a cake constraint (aka IHasDecoration) is declared anywhere in the stack
            //we validate the current cake supports the constraint.  this is a topdown walk
            var cake = this.Cake;

            cake.WithEach(layer =>
            {
                if (layer is IHasDecoration)
                {
                    //get all the interfaces it has, that derive from IHasDecoration
                    var layerType = layer.GetType();

                    var interfaces = layerType.GetInterfaces();
                    foreach (var interfaceType in interfaces)
                    {
                        if (!interfaceType.Name.Contains("IHasDecoration`"))
                        {
                            continue;
                        }

                        var requiredDecorations = interfaceType.GetGenericArguments();

                        foreach (Type each in requiredDecorations)
                        {
                            if (this.As(each, false) == null)
                            {
                                throw new InvalidOperationException(string.Format("required decoration {0} not found in cake", each.Name));
                            }
                        }
                    }
                }
            });
        }