9using System.Collections.Generic;
28 : this(new Uri(ruleString))
38 if (_uri.Scheme !=
"rule")
39 throw new ArgumentException(
"RuleUri.Scheme");
48 : this(GetTypeName(rule), ((property == null) ?
"(object)" : property.Name))
57 public RuleUri(
string typeName,
string propertyName)
59 var hostName = EncodeString(typeName).Replace(
".-",
".");
60 if (hostName.Length > 63)
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);
69 var uriString =
"rule://" + hostName +
"/" + EncodeString(propertyName);
70 _uri =
new Uri(uriString);
73 private static string EncodeString(
string 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(
"%",
"-");
105 return _uri.ToString();
116 if (uriText.Contains(
"?"))
117 uriText = uriText +
"&" + Uri.EscapeUriString(key) +
"=" + Uri.EscapeUriString(value);
119 uriText = uriText +
"?" + Uri.EscapeUriString(key) +
"=" + Uri.EscapeUriString(value);
120 _uri =
new Uri(uriText);
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(
"/",
"");
145 get {
return _uri.Parts()[_uri.Parts().Length - 1]; }
157 Dictionary<string, string> result =
null;
158 string args = _uri.Query;
159 if (!(
string.IsNullOrEmpty(args)))
161 if (args.StartsWith(
"?"))
162 args = args.Remove(0, 1);
164 result =
new Dictionary<string, string>();
165 string[] argArray = args.Split(
'&');
166 foreach (
string arg
in argArray)
168 string[] argParams = arg.Split(
'=');
170 System.Uri.UnescapeDataString(argParams[0]),
171 System.Uri.UnescapeDataString(argParams[1]));
180 var type = rule.GetType();
181 return GetTypeName(type);
190 private static string GetTypeName(Type type)
192 if (!type.IsGenericType)
194 return type.FullName;
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())
207 sb.Append(GetTypeName(t));
209 sb.Append(t.FullName);
212 return sb.ToString();
220 public static class UriExtensions
229 public static string[] Parts(
this System.Uri uri)
Parses a rule:// URI to provide easy access to the parts of the URI.
RuleUri(IBusinessRuleBase rule, Csla.Core.IPropertyInfo property)
Creates an instance of the object.
string RuleTypeName
Gets the name of the type containing the rule method.
Dictionary< string, string >? Arguments
Gets a Dictionary containing the name/value arguments provided to the rule method.
RuleUri(Uri uri)
Creates an instance of the object.
RuleUri(string ruleString)
Creates an instance of the object by parsing the provided rule:// URI.
static RuleUri Parse(string ruleString)
Parses a rule:// URI.
string PropertyName
Gets the name of the property with which the rule is associated.
RuleUri(string typeName, string propertyName)
Creates an instance of the object.
override string ToString()
Gets a string representation of the rule URI.
void AddQueryParameter(string key, string value)
Adds a query parameter to the URI.
Maintains metadata about a property.
Interface defining a business/validation rule implementation.