CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
RuleUri.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="RuleUri.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Parses a rule:// URI to provide</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Text;
11
12namespace Csla.Rules
13{
18 public class RuleUri
19 {
20 private Uri _uri;
21
27 public RuleUri(string ruleString)
28 : this(new Uri(ruleString))
29 { }
30
35 public RuleUri(Uri uri)
36 {
37 _uri = uri;
38 if (_uri.Scheme != "rule")
39 throw new ArgumentException("RuleUri.Scheme");
40 }
41
48 : this(GetTypeName(rule), ((property == null) ? "(object)" : property.Name))
49 //: this(rule.GetType().FullName, ((property == null) ? "null" : property.Name))
50 { }
51
57 public RuleUri(string typeName, string propertyName)
58 {
59 var hostName = EncodeString(typeName).Replace(".-", ".");
60 if (hostName.Length > 63)
61 {
62 var tmp = hostName;
63 hostName = null;
64 for (int i = 0; i < tmp.Length - 1; i = i + 63)
65 hostName = hostName + tmp.Substring(i, ((i + 63 <= tmp.Length) ? 63 : tmp.Length - i)) + "/";
66 hostName = hostName.Substring(0, hostName.Length - 1);
67 }
68
69 var uriString = "rule://" + hostName + "/" + EncodeString(propertyName);
70 _uri = new Uri(uriString);
71 }
72
73 private static string EncodeString(string value)
74 {
75 var result = value;
76 result = result.Replace("+", "-");
77 result = result.Replace(" ", "");
78 result = result.Replace("[", "");
79 result = result.Replace("]", "");
80 result = result.Replace("`", "-");
81 result = result.Replace(",", "-");
82 result = result.Replace("=", "-");
83 result = System.Uri.EscapeUriString(result);
84 result = result.Replace("%", "-");
85 return result;
86 }
87
94 public static RuleUri Parse(string ruleString)
95 {
96 return new RuleUri(ruleString);
97 }
98
103 public override string ToString()
104 {
105 return _uri.ToString();
106 }
107
113 public void AddQueryParameter(string key, string value)
114 {
115 var uriText = ToString();
116 if (uriText.Contains("?"))
117 uriText = uriText + "&" + Uri.EscapeUriString(key) + "=" + Uri.EscapeUriString(value);
118 else
119 uriText = uriText + "?" + Uri.EscapeUriString(key) + "=" + Uri.EscapeUriString(value);
120 _uri = new Uri(uriText);
121 }
122
127 public string RuleTypeName
128 {
129 get
130 {
131 string name = _uri.Host;
132 if (_uri.Parts().Length > 1)
133 for (int i = 0; i < _uri.Parts().Length - 1; i++)
134 name = name + _uri.Parts()[i];
135 return name.Replace("/", "");
136 }
137 }
138
143 public string PropertyName
144 {
145 get { return _uri.Parts()[_uri.Parts().Length - 1]; }
146 }
147
153 public Dictionary<string, string> Arguments
154 {
155 get
156 {
157 Dictionary<string, string> result = null;
158 string args = _uri.Query;
159 if (!(string.IsNullOrEmpty(args)))
160 {
161 if (args.StartsWith("?"))
162 args = args.Remove(0, 1);
163
164 result = new Dictionary<string, string>();
165 string[] argArray = args.Split('&');
166 foreach (string arg in argArray)
167 {
168 string[] argParams = arg.Split('=');
169 result.Add(
170 System.Uri.UnescapeDataString(argParams[0]),
171 System.Uri.UnescapeDataString(argParams[1]));
172 }
173 }
174 return result;
175 }
176 }
177
178 private static string GetTypeName(IBusinessRuleBase rule)
179 {
180 var type = rule.GetType();
181 return GetTypeName(type);
182 }
183
190 private static string GetTypeName(Type type)
191 {
192 if (!type.IsGenericType)
193 {
194 return type.FullName;
195 }
196 else // generic type
197 {
198 var sb = new StringBuilder();
199 if (!string.IsNullOrEmpty(type.Namespace))
200 sb.Append(type.Namespace + ".");
201 var typeName = type.Name;
202 sb.Append(typeName.Replace("`1", ""));
203 foreach (var t in type.GetGenericArguments())
204 {
205 sb.Append("-");
206 if (t.IsGenericType)
207 sb.Append(GetTypeName(t));
208 else
209 sb.Append(t.FullName);
210 sb.Append("-");
211 }
212 return sb.ToString();
213 }
214 }
215 }
216
220 public static class UriExtensions
221 {
229 public static string[] Parts(this System.Uri uri)
230 {
231 return uri.Segments;
232 }
233 }
234
235}
Parses a rule:// URI to provide easy access to the parts of the URI.
Definition: RuleUri.cs:19
RuleUri(IBusinessRuleBase rule, Csla.Core.IPropertyInfo property)
Creates an instance of the object.
Definition: RuleUri.cs:47
string RuleTypeName
Gets the name of the type containing the rule method.
Definition: RuleUri.cs:128
Dictionary< string, string >? Arguments
Gets a Dictionary containing the name/value arguments provided to the rule method.
Definition: RuleUri.cs:154
RuleUri(Uri uri)
Creates an instance of the object.
Definition: RuleUri.cs:35
RuleUri(string ruleString)
Creates an instance of the object by parsing the provided rule:// URI.
Definition: RuleUri.cs:27
static RuleUri Parse(string ruleString)
Parses a rule:// URI.
Definition: RuleUri.cs:94
string PropertyName
Gets the name of the property with which the rule is associated.
Definition: RuleUri.cs:144
RuleUri(string typeName, string propertyName)
Creates an instance of the object.
Definition: RuleUri.cs:57
override string ToString()
Gets a string representation of the rule URI.
Definition: RuleUri.cs:103
void AddQueryParameter(string key, string value)
Adds a query parameter to the URI.
Definition: RuleUri.cs:113
Maintains metadata about a property.
Interface defining a business/validation rule implementation.