/// <summary> /// Validates the specified model to make sure that any methods that are being intercepted /// are virtual and can be intercepted. /// </summary> /// <param name="model">The model.</param> /// <param name="store">The store.</param> private void Validate(ComponentModel model, MethodValidatorMetaStore store) { if (model.Service == null || model.Service.IsInterface) return; MethodValidatorMetaInfo meta = store.GetMetaFor(model.Implementation); if (meta == null) return; List<string> problematicMethods = new List<string>(); foreach (MethodInfo method in meta.Methods) { if (!method.IsVirtual) problematicMethods.Add(method.Name); } if (problematicMethods.Count != 0) { String message = String.Format("The class {0} wants to use method validator interception, " + "however the methods must be marked as virtual in order to do so. Please correct " + "the following methods: {1}", model.Implementation.FullName, String.Join(", ", problematicMethods.ToArray())); throw new FacilityException(message); } }
/// <summary> /// Validates the specified model to make sure that any methods that are being intercepted /// are virtual and can be intercepted. /// </summary> /// <param name="model">The model.</param> /// <param name="store">The store.</param> private void Validate(ComponentModel model, MethodValidatorMetaStore store) { if (model.Service == null || model.Service.IsInterface) { return; } MethodValidatorMetaInfo meta = store.GetMetaFor(model.Implementation); if (meta == null) { return; } List <string> problematicMethods = new List <string>(); foreach (MethodInfo method in meta.Methods) { if (!method.IsVirtual) { problematicMethods.Add(method.Name); } } if (problematicMethods.Count != 0) { String message = String.Format("The class {0} wants to use method validator interception, " + "however the methods must be marked as virtual in order to do so. Please correct " + "the following methods: {1}", model.Implementation.FullName, String.Join(", ", problematicMethods.ToArray())); throw new FacilityException(message); } }
/// <summary> /// Adds the method interceptor to the type if there are methods that have validators /// declared. /// </summary> /// <param name="model">The model.</param> /// <param name="store">The store.</param> private static void AddMethodInterceptorIfValidatable(ComponentModel model, MethodValidatorMetaStore store) { MethodValidatorMetaInfo meta = store.GetMetaFor(GetServiceOrDefault(model)); if (meta == null) { return; } model.Dependencies.Add(new DependencyModel(DependencyType.Service, null, typeof(MethodValidatorInterceptor), false)); model.Interceptors.AddFirst(new InterceptorReference(typeof(MethodValidatorInterceptor))); }
/// <summary> /// Processes the model. /// </summary> /// <param name="kernel">The kernel.</param> /// <param name="model">The model.</param> public override void ProcessModel(IKernel kernel, ComponentModel model) { if (metaStore == null) metaStore = (MethodValidatorMetaStore)kernel[typeof(MethodValidatorMetaStore)]; Type service = GetServiceOrDefault(model); if (HasBuilders(model.Implementation)) metaStore.CreateMetaFromType(service, model.Implementation); Validate(model, metaStore); AddMethodInterceptorIfValidatable(model, metaStore); }
/// <summary> /// Processes the model. /// </summary> /// <param name="kernel">The kernel.</param> /// <param name="model">The model.</param> public override void ProcessModel(IKernel kernel, ComponentModel model) { if (metaStore == null) { metaStore = (MethodValidatorMetaStore)kernel[typeof(MethodValidatorMetaStore)]; } Type service = GetServiceOrDefault(model); if (HasBuilders(model.Implementation)) { metaStore.CreateMetaFromType(service, model.Implementation); } Validate(model, metaStore); AddMethodInterceptorIfValidatable(model, metaStore); }
/// <summary> /// Adds the method interceptor to the type if there are methods that have validators /// declared. /// </summary> /// <param name="model">The model.</param> /// <param name="store">The store.</param> private static void AddMethodInterceptorIfValidatable(ComponentModel model, MethodValidatorMetaStore store) { MethodValidatorMetaInfo meta = store.GetMetaFor(GetServiceOrDefault(model)); if (meta == null) return; model.Dependencies.Add(new DependencyModel(DependencyType.Service, null, typeof(MethodValidatorInterceptor), false)); model.Interceptors.AddFirst(new InterceptorReference(typeof(MethodValidatorInterceptor))); }
/// <summary> /// Initializes a new instance of the <see cref="MethodValidationContributor"/> class. /// </summary> /// <param name="methodValidatorMetaStore">The method validator meta store.</param> public MethodValidationContributor(MethodValidatorMetaStore methodValidatorMetaStore) { this.methodValidatorMetaStore = methodValidatorMetaStore; }