This project is read-only.

Installation

Just add the BindToolkit library to your project.

Releases History

Examples Explanation

Reference

BindToolkit Namespace

BindToolkit.Behaviors Namespace 

public class SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior 

Use this endpoint behavior in the Wcf endpoints dedicated to the Silverlight clients (and only here!).

When there is a FaultException it changes the Status Code of the response into 200, thus enabling Silverlight to receive the details of the exception( that is the validation errors). 

Configure it this way:

 

<system.serviceModel>
        <extensions>
          <behaviorExtensions>
            <add name="silverlightFaults" type="BindToolkit.Behaviors.SilverlightFaultBehavior, BindToolkit, Version=0.8.0.0, Culture=neutral, PublicKeyToken=null" />
          </behaviorExtensions>
        </extensions>

 

 The above declares the configuration file extension. Once declared we can use it into the definition of an endpoint behavior:

 

<behaviors>  
            <endpointBehaviors>
              <behavior name="blabla">
                <silverlightFaults/>
              </behavior>
            </endpointBehaviors>      
</behaviors>

 

Once the behavior is defined we ca use it into an endpoint by setting the endpoinBehavior attribute to "blabla".

Please note that if we give no name to the behavior, the endpoint behavior is applied to all endpoints of all services! Don't do it in a production environment!

The example included with the binaries do it just because it uses Visual Studio internal Web Server(WebDev), that can support just one http endpoint!

You can start developing with WebDev, but then, please, pass to IIS and fix the configuration file!

BindToolkit.DataAnnotations Namespace

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]    

public class DynamicRangeAttribute : DynamicValidationAttribute

public DynamicRangeAttribute(Type targetType, string message)

public DynamicRangeAttribute(Type targetType)

targetType is the type of the property it is applied to. It can be any type that implements the IComparable interface, that is any type whose instances can be compared. In particular, are included: all numeric types, string, and DateTime.

message is the error message to be displayed. If not provided, a standard error will be displayed.  If one would like to localize the error message in various languages, one can set the properties: ErrorMessageResourceType, and ErrorMessageResourceName.

public string SMinimum{set; }

public string SMaximum{set;  }

Constant minimum and maximum in their string representations. They are used together to dynamic maximum and dynamic minimum (the more constraining of the two value is used).

public string DynamicMinimum{set; get; }

public string DynamicMaximum{set; get; }

The names of the property that contains the minimum and maximum to be used at run-time. Each property name contains all properties, separated by dots,  that need to be traversed to reach the target property from the root object. The root object is the one containing the DynamicRangeAttribute. Please note that if the property containing the dynamic maximum or the dynamic minimum are bound to elements of the User Interface, the attribute i re-evaluated each time they are changed

public string DynamicMinimumDelay{set; get; }

public string DynamicMaximumDelay{set; get; }

Displacements added to the dynamic minimum and to the dynamic maximum before the validation. For instance, by setting the DynamicMaximumDelay to a TimeSpan of -7 days onwe can force a value to be always one week less than another value (its dynamic maximum). Please note, delays can only be applied to numeric types and to DateTime because in such cases a meaningful addition operation is defined. In the case of DateTime the delay need to be a TimeSpan.

public class DynamicValidationAttribute: ValidationAttribute 

Base class for defining custom dynamic attributes like the DynamicRangeAttribute.

public DynamicValidationAttribute()

public DynamicValidationAttribute(string message)

message is the error message.

protected void DeclareDependency(params string[] properties)

This function needs to be called at the beginning of the IsValid function definition. properties is the list of all property names used as dynamic values.

protected ValidationResult ValidationResultFor(string error, ValidationContext validationContext, bool success)

Call this function to return the result of the validatio in the IsValid method. Please doesn't return directly the validation result.

success is the result of the validation, error is the error message to return in case validation fail.

 [DataContract]    

public class ValidationErrorInfo    

{        

public ValidationErrorInfo()        {        } 

public ValidationErrorInfo(ValidationResult vr)        {            Members = vr.MemberNames;            Error = vr.ErrorMessage;  }        

[DataMember]        public IEnumerable<string> Members { get; set; }      

[DataMember]    public string Error { get; set;}

}

Use this class to transfer validation errors from the server to the client, since the ValidationResult class is not serializable.

Pass them as details of a  FaultException<IEnumerable<ValidationErrorInfo>> that will be thrown by the server.

Please, don't forget to decorate the Interface definition of the Wcf service with the [FaultContract(typeof(IEnumerable<ValidationErrorInfo>))] attribute.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)]  

public class FormatErrorAttribute: Attribute

public FormatErrorAttribute()

public string ErrorMessage{set; get; }

string to use as message error. The string can have up two parameters, the first if provided is filled with the name of the field, while the second with the value that caused the error.

public Type ErrorMessageResourceType{get; set; }

 The type of the resource files containing the error message(please remember to set the access of the resource file to public otherwise the attribute can not read it)

public string ErrorMessageResourceName{get; set; }      

The entry of the resource file containing the error message 

 

BindWrapper class

Public Constructors

BindWrapper(object wrappedObject, bool wrapDeep=false,  bool wrapEnumerables = false) 

Build a wrapper for the object wrappedObject. If wrapDeep is set to true all son objects of wrappedObject will be wrapped recursively. Recursive wrapping continues through the boundaries of enumerables and dictionaries only if  wrapEnumerables is set to true. Please note that only strong typed enumerables and dictionaries are wrapped ie: IDictionary<string, TValue>, and IEnumerable<TValue>. If you need dictionaries or enumerables that are able to contain several datatypes please use  IDictionary<string, object>, andIEnumerable<object>.  

For wrapping the Items of an enumerable use the WrapEnumerable static method instead.

BindWrapper(Type wrappedType)

Build a wrapper and put an instance of the wrappedType type inside it.

Public Methods

bool ValidateWholeObject(IEnumerable<ValidationException> exceptions=null)

Triggers object level validation, adding the validation errors contained in exceptions. Returns true if the validation is succesfull. 

The ValidationResult contained in each Validation exception contains the property name where the error need to be applied. Each property name contains all properties, separated by dots,  that need to be traversed to reach the target property from the root object.

AddRemoteErrors(IEnumerable<ValidationException> remoteErrors)

Adds remoteErrors to the errors already displayed on the presentation layer. Usefull for adding errors returned by asynchronous calls to web services.

The ValidationResult contained in each Validation exception contains the property name where the error need to be applied. Each property name contains all properties, separated by dots,  that need to be traversed to reach the target property from the root object.

public void AddValidationFaultExceptionContent(IEnumerable<ValidationErrorInfo> errors)

Similar to the previous method, but the ValidationErrorInfo substitute the ValidationException. The ValidationErrorInfo contains exactly the same information of the ValidationResult in ValidationException. However, it is used as content of the FaultException returned by the server since ValidationResult is not serializable. Once on the client one can either call immediately this function or transform the FaultEsception into an IEnumerable of ValidationExceptions.

ResetGlobalEvaluation()

Resets all object level validation errors.

Public Static Methods 

static ObservableCollection<BindWrapper> WrapEnumerable(IEnumerable source, bool wrapDeep = false,  bool wrapEnumerables = false)

Wraps all items of source and puts them into the ObservableCollection that is returned.  If wrapDeep is set to true items are wrapped rcoursively. If wrapEnumerables is true wrapping crosses the boundaries of enumerables and dictionaries at any depth  Please note that only strong typed enumerables and dictionaries are wrapped ie: IDictionary<string, TValue>, andIEnumerable<TValue>. If you need dictionaries or enumerables that are able to contain several datatypes please use  IDictionary<string, object>, andIEnumerable<object>.  

static ObservableDictionary<string, BindWrapper> WrapDictionary<TValue>(IDictionary<string, TValue> source, bool wrapDeep = false, bool wrapEnumerables = false)

Wraps all values of source and puts them into the ObservableDictionary that is returned.  If wrapDeep is set to true items are wrapped rcoursively. If wrapEnumerables is true wrapping crosses the boundaries of enumerables and dictionaries at any depth.  Please note that only strong typed enumerables and dictionaries are wrapped ie: IDictionary<string, TValue>, andIEnumerable<TValue>. If you need dictionaries or enumerables that are able to contain several datatypes please use  IDictionary<string, object>, andIEnumerable<object>.  

static List<T> UnWrapEnumerable<T>(ObservableCollection<BindWrapper> source)

Unwraps all elements of the ObservableCollection and puts them into the List that is returned.

static ValidationResult AddPrefix(string prefix, ValidationResult val)

Add prefix to all property names contained in val. Usefull to build a properly formatted ValidationException.

Interface Implementations

INotifyPropertyChanged

The Silverlight version implements INotifyDataErrorInfo while the WPF version implements IDataErrorInfo

Last edited Apr 24, 2011 at 6:54 PM by frankabbruzzese, version 17

Comments

No comments yet.