S
Shapper
Hello,
I am trying to validate a few Reply class properties.
When a condition is satisfied an object is returned:
CustomResult r = new ReplyValidator<FindFileByIdReply, CustomResult>()
.Set(x => x.Info == ReplyInfo.AccessDenied, CustomResult.AccessDenied())
.Set(x => x.Info == ReplyInfo.NotFound, CustomResult.NotFound())
.Validate(reply);
or:
String r = new ReplyValidator<FindFileByIdReply, String>()
.Set(x => x.Info == ReplyInfo.AccessDenied, "Access is Denied")
.Set(x => x.Info == ReplyInfo.NotFound, "Not Found")
.Validate(reply);
Consider the following cases:
1) myReply.Info = ReplyInfo.AccessDenied >> GET Acess is Denied
2) myReply.Info = ReplyInfo.NotFound >> GET Not Found
2) myReply.Info = ReplyInfo.OK >> GET null.
However, I am having problems when returning null ...
Could someone help me out or maybe suggesting a different approach?
public class ReplyValidator<T, R> where T : Reply {
private IDictionary<Func<T, Boolean>, R> Rules { get; set; }
public ReplyValidator() {
Rules = new Dictionary<Func<T, Boolean>, R>();
} // ReplyValidator
public ReplyValidator<T, R> Set(Func<T, Boolean> predicate, R result) {
Rules.Add(predicate, result);
return this;
} // Set
public R Validate(T reply) {
foreach (KeyValuePair<Func<T, Boolean>, R> rule in Rules)
if (rule.Key(reply))
return rule.Value;
return null; // NOT ABLE TO RETURN NULL
} // Validate
} // ReplyValidator
Thank You,
Miguel
I am trying to validate a few Reply class properties.
When a condition is satisfied an object is returned:
CustomResult r = new ReplyValidator<FindFileByIdReply, CustomResult>()
.Set(x => x.Info == ReplyInfo.AccessDenied, CustomResult.AccessDenied())
.Set(x => x.Info == ReplyInfo.NotFound, CustomResult.NotFound())
.Validate(reply);
or:
String r = new ReplyValidator<FindFileByIdReply, String>()
.Set(x => x.Info == ReplyInfo.AccessDenied, "Access is Denied")
.Set(x => x.Info == ReplyInfo.NotFound, "Not Found")
.Validate(reply);
Consider the following cases:
1) myReply.Info = ReplyInfo.AccessDenied >> GET Acess is Denied
2) myReply.Info = ReplyInfo.NotFound >> GET Not Found
2) myReply.Info = ReplyInfo.OK >> GET null.
However, I am having problems when returning null ...
Could someone help me out or maybe suggesting a different approach?
public class ReplyValidator<T, R> where T : Reply {
private IDictionary<Func<T, Boolean>, R> Rules { get; set; }
public ReplyValidator() {
Rules = new Dictionary<Func<T, Boolean>, R>();
} // ReplyValidator
public ReplyValidator<T, R> Set(Func<T, Boolean> predicate, R result) {
Rules.Add(predicate, result);
return this;
} // Set
public R Validate(T reply) {
foreach (KeyValuePair<Func<T, Boolean>, R> rule in Rules)
if (rule.Key(reply))
return rule.Value;
return null; // NOT ABLE TO RETURN NULL
} // Validate
} // ReplyValidator
Thank You,
Miguel