//*****************************************************************************
// 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
}
}