예제 #1
0
        public static void RegisterSurrogate(
            string signature,
            Type supportedType,
            Action <object, Action <bool> > applyBindingAction           = null,
            Func <object, string, object> PropertyGetter                 = null,
            Action <object, string, object> PropertySetter               = null,
            IList <SurrogateDependencyCalculation> calculateDependencies = null,
            SerializeSurrogate serializeEx              = null,
            DeserializeSurrogate deserializeEx          = null,
            IsValidDependency isValidDependency         = null,
            Action <BinaryWriter, object> writeComparer = null
            )
        {
            if (string.IsNullOrWhiteSpace(signature) || signature.Length > SignatureMaxlenght)
            {
                throw new ArgumentException(string.Format("Signature name must be a non null of maximum of {0} characters", SignatureMaxlenght));
            }
            signature = signature.PadLeft(SignatureMaxlenght);
            var surrogateInfo = new SurrogatesInfo()
            {
                SupportedType        = supportedType,
                ApplyBindingHandlers = applyBindingAction,
                PropertyGetter       = PropertyGetter,
                PropertySetter       = PropertySetter,
                // Begin dependency calculation.
                CalculateDependencies = calculateDependencies,
                SerializeEx           = serializeEx,
                DeSerializeEx         = deserializeEx,
                IsValidDependency     = isValidDependency,
                WriteComparer         = writeComparer,
                //End dependency calculation.
                Signature = signature
            };

            lock (_syncSurrogates)
            {
                SurrogatesInfo dummy;
                if (!TypeToSurrogate.TryGetValue(supportedType, out dummy))
                {
                    TypeToSurrogate.Add(supportedType, surrogateInfo);
                    SignatureToSurrogate.Add(signature, surrogateInfo);
                    if (applyBindingAction != null || PropertyGetter != null || PropertySetter != null)
                    {
                        if (!(applyBindingAction != null && PropertyGetter != null && PropertySetter != null))
                        {
                            throw new ArgumentException("Invalid surrogate registration. If an ApplyBindingAction is given, then a PropertyGetter and a PropertySetter must be given too");
                        }
                    }
                }
            }
        }
        public void RestoreDependency(string UniqueId, string comparer, IsValidDependency isValidDependency)
        {
            var isEvent = UniqueId.IndexOf(EventsSeparator);
            //Gets the dependency value from storage if any.
            var dependency = _stateManager.GetObject(UniqueId);
            var surrogate  = dependency as StateObjectSurrogate;

            if (String.IsNullOrWhiteSpace(UniqueId))
            {
                Dependencies.Add(null);
                return;
            }

            if (isEvent != -1)
            {
                var eventInfo = new SurrogateEventsInfo()
                {
                    EventId   = UniqueId.Substring(0, isEvent),
                    EventName = UniqueId.Substring(++isEvent)
                };

                Dependencies.Add(eventInfo);

                return;
            }
            if (dependency == null)
            {
                if (_stateManager.IsInElementsToRemove(UniqueId))
                {
                    //Its OK to return "null" if the object is in the elements to be removed,
                    //if not returned the dependency list gets inconsistent.
                    Dependencies.Add(null);
                }
                return;
            }

            //The dependency must be valid:
            //1. The dependency value can be retrieved from storage or cache.
            //2. If it is a surrogate the value cannot be null.
            //3. The dependency must be registered as a valid dependency for the surrogate instance.
            object dependencyValue    = (surrogate != null && surrogate.Value != null) ? surrogate.Value : dependency;
            var    verifiedDependency = (isValidDependency == null) ? true : isValidDependency(dependencyValue, comparer);

            if (verifiedDependency)
            {
                Dependencies.Add(dependencyValue);
            }
        }