Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C# 4.0

Composable Data Validations. Func Delegates Everywhere

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Dec 2012CPOL10 min read 14.4K   105   18  
Composable data validations
This article will show you how to combine type based validation rules with other validations, how to compose them and apply them in different scenarios, adding/removing validations when applied on concrete instances, combining objects of different types and implementing one validation strategy on all.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComposableCore
{
    public class fState<T, S> where S : ICloneable
    {
        //De essentie van deze klasse is dat
        //hij geen enkele State member variabele kent en resultaten Kloont 
        //Zo krijgt een externe caller via getState() GEEN reference pointer naar een eigen state uit deze klasse
        //zodat hij alsnog, buiten de stateSetter, de interne state van deze klasse kan wijzigen.
        private Action<S, T> _statesetter;
        private fType<S, S> _invoke;
        private Func<Unit, S> _getstate;
        private Func<Unit, S> _resetstate;
        private Action<S> _resetsetter;
        public fState(Action<S, T> stateSetter, S stateHolder, Action<S> resetSetter)
        {
            //Encapsulate stateholder variable in Action! (private voor de action)
            Func<S, Action<S, T>> init = (x =>
            {
                x = stateHolder;
                return stateSetter;
            });
            //Encapsulate stateholder variable in Action! (private voor de action)
            Func<S, Action<S>> initreset = (x =>
            {
                x = stateHolder;
                return resetSetter;
            });

            _statesetter = init.Invoke(stateHolder);
            _invoke = fType.toFtype<S, S>(y => stateHolder);
            _getstate = (x => stateHolder);
            _resetsetter = initreset.Invoke(stateHolder);
            _resetstate = (x => stateHolder);
        }
        public void resetState()
        {
            if (_resetstate != null && _resetsetter != null)
            {
                _resetsetter(_resetstate.Invoke(new Unit()));
            }
        }
        public void setState(T value)
        {
            fType<S, S> theninvoke = fType.toFtype<S, S>(y =>
            {
                _statesetter(y, value);
                return y;
            });
            _invoke = _invoke.Then(theninvoke);
        }
        public S getState()
        {
            S ret = _invoke.Invoke(_getstate.Invoke(new Unit()));
            _invoke = fType.toFtype<S, S>(y => ret);
            return (S)ret.Clone();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions