AbstractSimpleBinding.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » DataBinding » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » DataBinding » AbstractSimpleBinding.cs
using System;
using System.Collections;
using System.Reflection;
using Common.Logging;
using Spring.Globalization;
using Spring.Validation;

namespace Spring.DataBinding{
    /// <summary>
    /// Abstract base class for simple, one-to-one <see cref="IBinding"/> implementations.
    /// </summary>
    /// <author>Aleksandar Seovic</author>
    public abstract class AbstractSimpleBinding : AbstractBinding
    {
        #region Fields

        private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private IFormatter formatter;

        #endregion

        #region Constructor(s)

        /// <summary>
        /// Initialize a new instance of <see cref="AbstractSimpleBinding"/> without any <see cref="IFormatter"/>
        /// </summary>
        protected AbstractSimpleBinding()
        {
        }

        /// <summary>
        /// Initialize a new instance of <see cref="AbstractSimpleBinding"/> with the 
        /// specified <see cref="IFormatter"/>.
        /// </summary>
        protected AbstractSimpleBinding(IFormatter formatter)
        {
            this.formatter = formatter;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the <see cref="IFormatter"/> to use.
        /// </summary>
        /// <value>The formatter to use.</value>
        public IFormatter Formatter
        {
            get { return formatter; }
            set { formatter = value; }
        }

        #endregion

        #region IBinding Implementation

        /// <summary>
        /// Binds source object to target object.
        /// </summary>
        /// <param name="source">
        /// The source object.
        /// </param>
        /// <param name="target">
        /// The target object.
        /// </param>
        /// <param name="validationErrors">
        /// Validation errors collection that type conversion errors should be added to.
        /// </param>
        /// <param name="variables">
        /// Variables that should be used during expression evaluation.
        /// </param>
        public override void BindSourceToTarget(object source, object target, IValidationErrors validationErrors,
                                                IDictionary variables)
        {
            if (this.IsValid(validationErrors)
                && (this.Direction == BindingDirection.Bidirectional || this.Direction == BindingDirection.SourceToTarget))
            {
                try
                {
                    DoBindSourceToTarget(source, target, variables);
                }
                catch (Exception)
                {
                    if (!SetInvalid(validationErrors)) throw;
                }
            }
        }

        /// <summary>
        /// Concrete implementation if source to target binding.
        /// </summary>
        /// <param name="source">
        /// The source object.
        /// </param>
        /// <param name="target">
        /// The target object.
        /// </param>
        /// <param name="variables">
        /// Variables that should be used during expression evaluation.
        /// </param>
        protected virtual void DoBindSourceToTarget(object source, object target, IDictionary variables)
        {
            object value = this.GetSourceValue(source, variables);
            if (this.Formatter != null && value is string)
            {
                value = this.Formatter.Parse((string)value);
            }
            this.SetTargetValue(target, value, variables);
        }

        /// <summary>
        /// Binds target object to source object.
        /// </summary>
        /// <param name="source">
        /// The source object.
        /// </param>
        /// <param name="target">
        /// The target object.
        /// </param>
        /// <param name="validationErrors">
        /// Validation errors collection that type conversion errors should be added to.
        /// </param>
        /// <param name="variables">
        /// Variables that should be used during expression evaluation.
        /// </param>
        public override void BindTargetToSource(object source, object target, IValidationErrors validationErrors,
                                                IDictionary variables)
        {
            if (this.IsValid(validationErrors)
                && (this.Direction == BindingDirection.Bidirectional || this.Direction == BindingDirection.TargetToSource))
            {
                try
                {
                    DoBindTargetToSource(source, target, variables);
                }
                catch (Exception ex)
                {
                    log.Warn(string.Format("Failed binding[{0}]:{1}", this.Id, ex));
                    if (!SetInvalid(validationErrors)) throw;
                }
            }
        }

        /// <summary>
        /// Concrete implementation of target to source binding.
        /// </summary>
        /// <param name="source">
        /// The source object.
        /// </param>
        /// <param name="target">
        /// The target object.
        /// </param>
        /// <param name="variables">
        /// Variables that should be used during expression evaluation.
        /// </param>
        protected virtual void DoBindTargetToSource(object source, object target, IDictionary variables)
        {
            object value = this.GetTargetValue(target, variables);
            if (this.Formatter != null)
            {
                value = this.Formatter.Format(value);
            }
            this.SetSourceValue(source, value, variables);
        }

        #endregion

        #region Abstract Methods

        /// <summary>
        /// Gets the source value for the binding.
        /// </summary>
        /// <param name="source">
        /// Source object to extract value from.
        /// </param>
        /// <param name="variables">
        /// Variables for expression evaluation.
        /// </param>
        /// <returns>
        /// The source value for the binding.
        /// </returns>
        protected abstract object GetSourceValue(object source, IDictionary variables);

        /// <summary>
        /// Sets the source value for the binding.
        /// </summary>
        /// <param name="source">
        /// The source object to set the value on.
        /// </param>
        /// <param name="value">
        /// The value to set.
        /// </param>
        /// <param name="variables">
        /// Variables for expression evaluation.
        /// </param>
        protected abstract void SetSourceValue(object source, object value, IDictionary variables);

        /// <summary>
        /// Gets the target value for the binding.
        /// </summary>
        /// <param name="target">
        /// Source object to extract value from.
        /// </param>
        /// <param name="variables">
        /// Variables for expression evaluation.
        /// </param>
        /// <returns>
        /// The target value for the binding.
        /// </returns>
        protected abstract object GetTargetValue(object target, IDictionary variables);

        /// <summary>
        /// Sets the target value for the binding.
        /// </summary>
        /// <param name="target">
        /// The target object to set the value on.
        /// </param>
        /// <param name="value">
        /// The value to set.
        /// </param>
        /// <param name="variables">
        /// Variables for expression evaluation.
        /// </param>
        protected abstract void SetTargetValue(object target, object value, IDictionary variables);

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.