Exemplo n.º 1
0
        /// <summary>
        /// Validate the configuration
        /// </summary>
        /// <returns></returns>
        public override Maybe<string> Validate()
        {
            Maybe<string> baseValidation = base.Validate();
            if (baseValidation.Item1) // assuming base passed validation....
            {
                // 98% of the time, the CoalesceProc will need to come from configuration,
                // but a user may be wiring up the rig programmatically, and we should
                // be prepared for that
                if (this.Coalesce == null)
                {
                    // Go look in Configuration for it using Try...(), since we don't
                    // want an exception thrown if it fails; we want to handle it ourselves
                    // via the Maybe<> return
                    object obj;
                    if (Configuration.TryGetValue("CoalesceProc", out obj))
                    {
                        // It will be either "Symphony.Core.CoalescingDevice.OneItemCoalesce"
                        // or something like "Custom.Assembly.Name,Custom.ClassName.CoalescingProcInstance"
                        string assmClassPair = (string)obj;

                        string assemblyName = "Symphony.Core";
                        string name = assmClassPair;
                        if (assmClassPair.Contains(','))
                        {
                            assemblyName = assmClassPair.Split(',')[0];
                            name = assmClassPair.Split(',')[1];
                        }

                        Assembly assm = assemblyName != "Symphony.Core" ?
                            Assembly.Load(assemblyName) :
                            Assembly.GetExecutingAssembly();

                        string instName = name.Substring(name.LastIndexOf('.') + 1); // Trim off leading "."
                        string className = name.Substring(0, name.LastIndexOf('.'));

                        Type classType = assm.GetType(className);
                        MemberInfo[] instMembers = classType.GetMember(instName);
                        if (instMembers.Length == 0)
                            return Maybe<string>.No(string.Format("Couldn't find {0}.{1} CoalesceProc", className, instName));
                        if (instMembers.Length > 2)
                            return Maybe<string>.No(
                                string.Format("Too many members for {0}.{1} CoalesceProc",
                                    className, instName));

                        MemberInfo mi = instMembers[0];
                        if (mi is PropertyInfo)
                        {
                            PropertyInfo pi = (PropertyInfo)mi;

                            if (!(pi.CanRead))
                                return Maybe<string>.No(string.Format("{0}.{1} is not readable", className, instName));

                            this.Coalesce = (CoalesceProc)pi.GetValue(null, null);
                        }
                        else if (mi is FieldInfo)
                        {
                            FieldInfo fi = (FieldInfo)mi;

                            if (!fi.Attributes.HasFlag(FieldAttributes.Public))
                                return Maybe<string>.No(string.Format("{0}.{1} must be public", className, instName));

                            if (!fi.Attributes.HasFlag(FieldAttributes.Static))
                                return Maybe<string>.No(string.Format("{0}.{1} must be static", className, instName));

                            this.Coalesce = (CoalesceProc)fi.GetValue(null);
                        }
                        else
                            return Maybe<string>.No(string.Format("{0}.{1} is not a field or property"));

                        return Maybe<string>.Yes();
                    }
                    else
                        return Maybe<string>.No("No CoalesceProc provided in Configuration");
                }
                else
                    return Maybe<string>.Yes();
            }
            else
                return baseValidation;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validate the configuration
        /// </summary>
        /// <returns></returns>
        public override Maybe <string> Validate()
        {
            Maybe <string> baseValidation = base.Validate();

            if (baseValidation.Item1) // assuming base passed validation....
            {
                // 98% of the time, the CoalesceProc will need to come from configuration,
                // but a user may be wiring up the rig programmatically, and we should
                // be prepared for that
                if (this.Coalesce == null)
                {
                    // Go look in Configuration for it using Try...(), since we don't
                    // want an exception thrown if it fails; we want to handle it ourselves
                    // via the Maybe<> return
                    object obj;
                    if (Configuration.TryGetValue("CoalesceProc", out obj))
                    {
                        // It will be either "Symphony.Core.CoalescingDevice.OneItemCoalesce"
                        // or something like "Custom.Assembly.Name,Custom.ClassName.CoalescingProcInstance"
                        string assmClassPair = (string)obj;

                        string assemblyName = "Symphony.Core";
                        string name         = assmClassPair;
                        if (assmClassPair.Contains(','))
                        {
                            assemblyName = assmClassPair.Split(',')[0];
                            name         = assmClassPair.Split(',')[1];
                        }

                        Assembly assm = assemblyName != "Symphony.Core" ?
                                        Assembly.Load(assemblyName) :
                                        Assembly.GetExecutingAssembly();

                        string instName  = name.Substring(name.LastIndexOf('.') + 1); // Trim off leading "."
                        string className = name.Substring(0, name.LastIndexOf('.'));

                        Type         classType   = assm.GetType(className);
                        MemberInfo[] instMembers = classType.GetMember(instName);
                        if (instMembers.Length == 0)
                        {
                            return(Maybe <string> .No(string.Format("Couldn't find {0}.{1} CoalesceProc", className, instName)));
                        }
                        if (instMembers.Length > 2)
                        {
                            return(Maybe <string> .No(
                                       string.Format("Too many members for {0}.{1} CoalesceProc",
                                                     className, instName)));
                        }

                        MemberInfo mi = instMembers[0];
                        if (mi is PropertyInfo)
                        {
                            PropertyInfo pi = (PropertyInfo)mi;

                            if (!(pi.CanRead))
                            {
                                return(Maybe <string> .No(string.Format("{0}.{1} is not readable", className, instName)));
                            }

                            this.Coalesce = (CoalesceProc)pi.GetValue(null, null);
                        }
                        else if (mi is FieldInfo)
                        {
                            FieldInfo fi = (FieldInfo)mi;

                            if (!fi.Attributes.HasFlag(FieldAttributes.Public))
                            {
                                return(Maybe <string> .No(string.Format("{0}.{1} must be public", className, instName)));
                            }

                            if (!fi.Attributes.HasFlag(FieldAttributes.Static))
                            {
                                return(Maybe <string> .No(string.Format("{0}.{1} must be static", className, instName)));
                            }

                            this.Coalesce = (CoalesceProc)fi.GetValue(null);
                        }
                        else
                        {
                            return(Maybe <string> .No(string.Format("{0}.{1} is not a field or property")));
                        }

                        return(Maybe <string> .Yes());
                    }
                    else
                    {
                        return(Maybe <string> .No("No CoalesceProc provided in Configuration"));
                    }
                }
                else
                {
                    return(Maybe <string> .Yes());
                }
            }
            else
            {
                return(baseValidation);
            }
        }