Programmatically Verify TargetType exist in config source

Topics: General discussion, Validation Application Block
Mar 9, 2010 at 10:26 PM
Edited Mar 10, 2010 at 12:16 PM

Hello,

Is there a way to programmatically verify that a 'TargetType' exist within a configuration file source?  Here is what I am trying to do:

Validator validator =ValidationFactory.CreateValidator(TargetType, RuleSet,new FileConfigurationSource("..\..\app.config"));

ValidationResults

results = validator.Validate(ObjectToValidate);

Right now, regardless of whether the 'TargetType' exist within the given config file, there no exception is ever thrown; which is good. However, the 

ValidationResults

returned are not very helpful in indicating that the 'TargetType' did not exist and since that was the case no validation was performed. The ValidationResults returned IsValid flag is set to true making it seem as though some work was done, but in reality no validation was performed at all. I guess that is my main issue is that it seems as though there is no way to prevent this from happening and I think there should be some kind of verification or preventive measusre that can be taken to avoid this issue. Anyone have some ideas about this issue? Any help is appreciated.

 

Mar 10, 2010 at 5:15 AM
Edited Mar 10, 2010 at 8:44 AM

Hi,

I dont see any built-in method that does that. However, after a few tries. I've come up with this one. Please see below:

 

static void Main(string[] args)
        {
            Type targetType = typeof(Person);
            string ruleSet = "Default";
            string config = Assembly.GetExecutingAssembly().ManifestModule.Name + ".config";

            Person p = new Person { 
                Age = 1,
                FullName = ""
            };

            VerifyTargetTypeExists(targetType, ruleSet, config);

            Validator validator =ValidationFactory.CreateValidator(targetType, ruleSet,new FileConfigurationSource(config));
            ValidationResults results = validator.Validate(p);
        }

        private static void VerifyTargetTypeExists(Type targetType, string ruleSet, string config)
        {
            IConfigurationSource source = new FileConfigurationSource(config);
            ValidationSettings validation = source.GetSection(ValidationSettings.SectionName) as ValidationSettings;
            if (validation != null)
            {
                foreach (var item in validation.Types)
                {
                    if (item == targetType)
                    {
                        //the target assembly exists
                    }
                    else
                    { 
                        //there is no defined type
                        //throw exception
                    }
                }
            }
            else
            {
                throw new Exception("No Validation Section defined.");
            }
        }
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 10, 2010 at 8:41 AM

Valiant,

Why are you comparing the assembly names of the two types? It seems to me that you should compare the two types, so instead of doing  this:

item.AssemblyName == targetType.Assembly.FullName

 

it seems more logically to do this:

item == targetType

Could you comment on this?

 

Mar 10, 2010 at 8:45 AM

Yes, you're right. I have already updated it. Thanks for the correction. :)

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 10, 2010 at 2:01 PM

Hello Valiant,

Thanks! This solution works perfectly for the issue I mentioned.