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

Enterprise Variables for WF4

Rate me:
Please Sign up or sign in to vote.
4.95/5 (21 votes)
23 Sep 2010CPOL23 min read 59.3K   810   39  
This article describes the design, implementation and usage of the Enterprise Variables stored in the Repository.
//*****************************************************************************
//    Description.....Static methods for accessing variables stored in the Repository
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright © 2010 ATZ Consulting Inc.     
//                        
//    Date Created:    07/07/10
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    07/07/10    Roman Kiss     Initial Revision
//
//*****************************************************************************
//  
#region References
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Transactions;
#endregion
//

namespace RKiss.EnterpriseVariables
{

    // get a value from the runtime database based on the machine and application names
    public class EVar
    {
        #region Get
        public class Get<T>
        {
            #region Properties
            public string ConnectionString
            {
                get { return This.ConnectionString; }
                set { This.ConnectionString = value; }
            }
            public string Name { get; set; }
            public Dictionary<string, object> Inputs { private get; set; }
            #endregion

            #region Runtime value
            public T Value
            {
                get
                {
                    string traceText = VariablesHelper.SetScope();

                    using (TransactionScope ts = new TransactionScope())
                    {
                        T variable = Inputs == null ? This.Get(this.Name) : This.Exec(this.Name, Inputs);

                        Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, this.Name));

                        ts.Complete();
                        return variable;
                    }
                }
                private set
                {
                    throw new NotSupportedException();
                }
            }
            public T Item(dynamic index)
            {
                string traceText = VariablesHelper.SetScope();

                using (TransactionScope ts = new TransactionScope())
                {
                    dynamic variable = Inputs == null ? This.Get(this.Name) : This.Exec(this.Name, Inputs);

                    Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, this.Name));

                    ts.Complete();
                    return (T)variable.GetType().GetProperty("Item").GetValue(variable, new object[] { index });
                }
            }
            #endregion

            #region Metadata
            public string ExpressionText
            {
                get
                {
                    string traceText = VariablesHelper.SetScope();

                    using (TransactionScope ts = new TransactionScope())
                    {
                        string variable = This.GetText(this.Name);

                        Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, this.Name));

                        ts.Complete();
                        return variable;
                    }
                }
                private set
                {
                    throw new NotSupportedException();
                }
            }

            public Variable Variable
            {
                get
                {
                    string traceText = VariablesHelper.SetScope();

                    using (TransactionScope ts = new TransactionScope())
                    {
                        Variable variable = This.GetVariable(this.Name);

                        Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, this.Name));

                        ts.Complete();
                        return variable;
                    }
                }
                private set
                {
                    throw new NotSupportedException();
                }
            }
            #endregion
        }
        #endregion

        #region SetValue
        public static void SetValue<T>(string variableName, string expressionText)
        {
            string traceText = VariablesHelper.SetScope();

            using (TransactionScope ts = new TransactionScope())
            {
                This.Put<T>(variableName, expressionText);

                Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, variableName));

                ts.Complete();
            }
        }
        #endregion

        #region Runtime: GetValue
        public static string GetValue(string variableName)
        {
            string traceText = VariablesHelper.SetScope();

            using (TransactionScope ts = new TransactionScope())
            {
                dynamic variable = This.Get(variableName);
                
                Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, variableName));

                ts.Complete();
                return Convert.ChangeType(variable, typeof(string));
            }
        }

        public static T GetValue<T>(string variableName)
        {
            string traceText = VariablesHelper.SetScope();
 
            using (TransactionScope ts = new TransactionScope())
            {
                T variable = This.Get<T>(variableName);

                Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, variableName));
                
                ts.Complete();
                return variable;
            }
        }


        public static T GetItem<T>(string variableName, dynamic index)
        {
            string traceText = VariablesHelper.SetScope();

            using (TransactionScope ts = new TransactionScope())
            {
                T variable = This.GetItem<T>(variableName, index);

                Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, variableName));

                ts.Complete();
                return variable;
            }
        }
        #endregion

        #region Execute Variable as Activity
        public static IDictionary<string, object> Exec(string variableName, IDictionary<string, object> inputs)
        {
            string traceText = VariablesHelper.SetScope();

            using (TransactionScope ts = new TransactionScope())
            {
                var variable = This.Exec(variableName, inputs);

                Trace.WriteLine(string.Format("{0}, connectionString = '{1}', applicationScope = {2}, variableName = {3}", traceText, This.ConnectionString, This.ApplicationScope, variableName));

                ts.Complete();
                return variable;
            }
        }

        public static IDictionary<string, object> Exec(string variableName)
        {
            return Exec(variableName, null);
        }

        public static IDictionary<string, object> Exec(string variableName, object input0)
        {
            var dic = new Dictionary<string, object>();
            dic.Add("inp0", input0);
            return Exec(variableName, dic);
        }

        public static IDictionary<string, object> Exec(string variableName, object input0, object input1)
        {
            var dic = new Dictionary<string, object>();
            dic.Add("inp0", input0);
            dic.Add("inp1", input1);
            return Exec(variableName, dic);
        }

        public static IDictionary<string, object> Exec(string variableName, object input0, object input1, object input2)
        {
            var dic = new Dictionary<string, object>();
            dic.Add("inp0", input0);
            dic.Add("inp1", input1);
            dic.Add("inp2", input2);
            return Exec(variableName, dic);
        }
        #endregion

        #region Helpers
        public static Dictionary<string, object> Inputs
        {
            get { return new Dictionary<string, object>(); }           
        }
        #endregion   
    }
}



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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions