whosrdaddy
Vendor
Hi I am using Caste Validator in my domain objects.
here's an excerpt of my code
I don't like this code 100%.
what I would like to achieve is this:
I don't know to implement the ".AddCustomValidator(ValidateNonAdminUser)" part.
for reference this is the LocalizedCastleValidationRunner:
/Daddy
-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
here's an excerpt of my code
Code:
namespace DCM.Data.Models
{
public class UserEntity : EntityBase
{
[ValidateNonEmpty("RuleNotEmpty, Username")]
public virtual string Username { get; set; }
[ValidateNonEmpty]
public virtual string Password { get; set; }
[ValidateNonEmpty]
public virtual string Fullname { get; set; }
public virtual bool IsDeleted { get; set; }
public virtual LanguageEntity Language { get; set; }
public virtual IList<RoleEntity> Roles { get; private set; }
public UserEntity()
{
Roles = new List<RoleEntity>();
}
public virtual void Validate()
{
var errors = LocalizedCastleValidationRunner.GetErrors(this).ToList();
errors.AddRange(ValidateNonAdminUser(this));
if (errors.Any())
throw new RulesException(errors);
}
public static IEnumerable<ErrorInfo> ValidateNonAdminUser(UserEntity userEntity)
{
if (userEntity.Username.ToLower().Equals("admin"))
yield return new ErrorInfo("Username", "RuleNonAdmin");
}
}
}
I don't like this code 100%.
what I would like to achieve is this:
Code:
namespace DCM.Data.Models
{
public class UserEntity : EntityBase
{
[ValidateNonEmpty("RuleNotEmpty, Username")]
public virtual string Username { get; set; }
[ValidateNonEmpty]
public virtual string Password { get; set; }
[ValidateNonEmpty]
public virtual string Fullname { get; set; }
public virtual bool IsDeleted { get; set; }
public virtual LanguageEntity Language { get; set; }
public virtual IList<RoleEntity> Roles { get; private set; }
public UserEntity()
{
Roles = new List<RoleEntity>();
}
public virtual void Validate()
{
ANewCastleValidationRunner
.ValidateModel(this)
.AddCustomValidator(ValidateNonAdminUser)
.AddCustomValidator(ValidateSomethingElse)
}
public static IEnumerable<ErrorInfo> ValidateNonAdminUser()
{
if (this.Username.ToLower().Equals("admin"))
yield return new ErrorInfo("Username", "RuleNonAdmin");
}
}
}
I don't know to implement the ".AddCustomValidator(ValidateNonAdminUser)" part.
for reference this is the LocalizedCastleValidationRunner:
Code:
internal class LocalizedCastleValidationRunner
{
private static readonly CachedValidationRegistry registry = new CachedValidationRegistry();
public static string LocalizedString(string key)
{
// allow for multiple parameters
// syntax: parm1,parm2,parm3
// will return string.format(parm1, string.format(parm2, parm3));
var keys = key.Split(',');
var res = new ResourceManager(typeof (SR)) {IgnoreCase = true};
var message = string.Empty;
try
{
for (var x = keys.Length - 1; x > -1; x--)
{
var msg = res.GetString(keys[x].Trim(), Thread.CurrentThread.CurrentCulture);
if (message.Equals(string.Empty))
{
message += msg;
}
else
message = string.Format(msg, message);
}
return message;
}
catch
{
return key;
}
}
public static IList<ErrorInfo> GetErrors(object instance)
{
var result = new List<ErrorInfo>();
var runner = new ValidatorRunner(registry);
if (!runner.IsValid(instance))
{
var errorSummary = runner.GetErrorSummary(instance);
result.AddRange(from prop in errorSummary.InvalidProperties
from err in errorSummary.GetErrorsForProperty(prop)
// err is in fact a key for our resource
select new ErrorInfo(prop, LocalizedString(err)));
}
return result;
}
}
}
/Daddy
-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!