We first need to create a Visual Studio project whose template is a Class Library. For this tutorial we'll be using Visual Studio 2008.
Create the DLL
- Start Visual Studio
- Go to File -> New -> Project
- In the Template Categories, expand Visual C# and select the Windows.
- Then select the Class Library template.
- Type a name for your project in the Name field - we will use "TestRules" for this example.
- Click OK. The new project appears in Solution Explorer.
- If Program.cs is not open in the Code Editor, right-click Program.cs in Solution Explorer and then click View Code.
- Renamed Program.cs to NameMathRules.cs.
Replace the contents of NameMathRules.cs with the following code.
C#using System; using System.Collections.Generic; using System.Linq; using ASF.BDS.Rules.Common; using Common.Logging; using ASF.Framework.Service.Parameters; using System.Net; using ASF.Framework.Util; namespace TestRules { public static class NameMathRules { private static readonly ILog Logger = LogManager.GetLogger(typeof(NameMathRules)); public static double MultiplyRule(double x, double y) { return (x * y); } public static RuleResponse Handle(RuleRequest request) { var result = new ParameterCollection(); result.Add(new Parameter("Test_resp_pa") { Value = "11111" }); try { LogHelper.Error(Logger, string.Format("Test", "Success")); foreach (var p in request.Parameters) { result.Add(new Parameter(p.Key) { Value = p.Value }); } if (request.Parameters.ContainsKey("TestParam")) { var myParam = ConvertHelper.SafeObjectToInt(request.Parameters["TestParam"]); ValidationHelper.ValidateNotZeroInt(myParam, "myParam"); //ValidationHelper.ValidateNotNullString(myParam, "myParam"); result.Add(new Parameter("MultiplyValue") { Value = MultiplyRule(myParam, myParam + 1) }); } return new RuleResponse() { ErrorMessage = "", StatusCode = HttpStatusCode.OK, Result = result }; } catch (Exception e) { LogHelper.Error(Logger, string.Format("Error", e.ToString())); return new RuleResponse() { ErrorMessage = e.ToString(), StatusCode = HttpStatusCode.OK }; } } private static Dictionary<string, string>[] __GetMethodParameterMetas(string method) { if (method == "Handle") { var handleParameters = new List<Dictionary<string, string>>(); var handleParameterInfo = new Dictionary<string, string>(); handleParameterInfo["name"] = "TestParam"; handleParameterInfo["behavior"] = "1"; handleParameterInfo["type"] = "Text"; handleParameters.Add(handleParameterInfo); var outputParameterInfo = new Dictionary<string, string>(); outputParameterInfo["name"] = "MultiplyValue"; outputParameterInfo["behavior"] = "2"; outputParameterInfo["type"] = "Number"; handleParameters.Add(outputParameterInfo); return handleParameters.ToArray(); } return null; } } }
- Add another class called ConcatenateRules.cs
- Replace the contents of ConcatenateRules.cs with the following code. C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ASF.BDS; using ASF.BDS.Common; using ASF.BDS.Rules.Common; using ASF.Framework; using ASF.Framework.Service; using ASF.Security; using Common.Logging; using Newtonsoft.Json; using ASF.Framework.Service.Parameters; using System.Net; using ASF.Framework.Util; namespace TestRules { public static class ConcatenateRules { private static readonly ILog Logger = LogManager.GetLogger(typeof(NameMathRules)); public static string FullNameRule(string fName, string lName) { return (fName.TrimEnd().TrimStart() + " " + lName.TrimEnd().TrimStart()); } public static RuleResponse Handle(RuleRequest request) { var result = new ParameterCollection(); result.Add(new Parameter("Test_resp_pa") { Value = "11111" }); try { LogHelper.Error(Logger, string.Format("Test", "Success")); foreach (var p in request.Parameters) { result.Add(new Parameter(p.Key) { Value = p.Value }); } if (request.Parameters.ContainsKey("TestParam")) { var myParam = ConvertHelper.SafeObjectToString(request.Parameters["TestParam"]); //ValidationHelper.ValidateNotZeroInt(myParam, "myParam"); ValidationHelper.ValidateNotNullString(myParam, "myParam"); result.Add(new Parameter("ConcatenateValue") { Value = FullNameRule(myParam, myParam + "abc") }); } return new RuleResponse() { ErrorMessage = "", StatusCode = HttpStatusCode.OK, Result = result }; } catch (Exception e) { LogHelper.Error(Logger, string.Format("Error", e.ToString())); return new RuleResponse() { ErrorMessage = e.ToString(), StatusCode = HttpStatusCode.OK }; } } private static Dictionary<string, string>[] __GetMethodParameterMetas(string method) { if (method == "Handle") { var handleParameters = new List<Dictionary<string, string>>(); var handleParameterInfo = new Dictionary<string, string>(); handleParameterInfo["name"] = "TestParam"; handleParameterInfo["behavior"] = "1"; handleParameterInfo["type"] = "Text"; handleParameters.Add(handleParameterInfo); var outputParameterInfo = new Dictionary<string, string>(); outputParameterInfo["name"] = "ConcatenateValue"; outputParameterInfo["behavior"] = "2"; outputParameterInfo["type"] = "Text"; handleParameters.Add(outputParameterInfo); return handleParameters.ToArray(); } return null; } } }
- Now build the solution (Ctrl+Shift+B).
- Find the entire Visual Studio project here: TestRules.zip
Use the DLL in AppBase
Next we need to write a upload the compiled DLL to AppBase.
- Open Application Studio of your solution.
- From the left menu, select Business Rules -> DLL Extensions
- Click the New Extension button. The New Extension page is displayed.
Enter the specific extension properties as follows:
Property Value Extension Name TestRulesDLL Upload DLL Upload the DLL that the compiled Visual Studio project created. - Click Save.
- Now deploy your solution.
- From the left menu, select Business Rules -> Rules
- Click the New Rule button. The New Rule page is displayed.
Enter the specific extension properties as follows:
Property Value Name TestRulesDLLRule Rule Usage Capture Rule Type Extension Extension TestRuleDLL Class Name NameMathRules Method Name Handle - Click Save. You will be redirected to the Rule detail page.
- Click Debug.
- Type any number into the TestParam value and click Run.
- Inspect the Rule Result text area that displays the correct output of the rule.
- Rule the class ConcatenateRules by adding a new rule.
- From the left menu, select Business Rules -> Rules
- Click the New Rule button. The New Rule page is displayed.
Enter the specific extension properties as follows:
Property Value Name TestRulesDLLRule2 Rule Usage Capture Rule Type Extension Extension TestRuleDLL Class Name ConcatenateRules Method Name Handle - Click Save. You will be redirected to the Rule detail page.
- Click Debug.
- Type any word into the TestParam value and click Run.
- Inspect the Rule Result text area that displays the correct output of the rule.