MVC3 Model Validation (Compare propery in another class) - asp.net-mvc-3

public class User
{
string username;
string password;
}
public class registration
{
User user;
**[compare(user.password)]**
string newPass;
}
It produces error (Property is not found)
Is there a way to create validation for a property inside another class ?
thanks

public class CompareOther:ValidationAttribute, IClientValidatable
{
private readonly string testedPropertyName;
private readonly string className;
public CompareOther(string testedPropertyName)
{
string[] word = testedPropertyName.Split('.');
this.testedPropertyName = word[1];
this.className = word[0];
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var propertyTestedInfo = validationContext.ObjectType.GetProperty(this.className);
if (propertyTestedInfo == null)
{
//return new ValidationResult("unknown property");
return new ValidationResult(string.Format("unknown property {0}.{1}",this.className ,this.testedPropertyName));
}
var propertyObject = propertyTestedInfo.GetValue(validationContext.ObjectInstance,null);
var propertyTestedValue = getValue(testedPropertyName,propertyObject);
if (value == null)
{
return ValidationResult.Success;
}
if (propertyTestedValue == null)
{
return ValidationResult.Success;
}
if (propertyTestedValue.ToString() == value.ToString())
{
return ValidationResult.Success;
}
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessageString,
ValidationType = "lessthan"
};
rule.ValidationParameters["propertytested"] = this.testedPropertyName;
yield return rule;
}
private Object getValue(string name,Object obj)
{
if (obj == null)
return null;
Type type = obj.GetType();
PropertyInfo info = type.GetProperty(name);
if (info == null)
{ return null; }
obj = info.GetValue(obj, null);
return obj;
}
}

Related

Equal Collection returns false when Object implements IEquatable?

I have the following test example:
var a = new List<OrderRule> {
new OrderRule("name", OrderDirection.Ascending),
new OrderRule("age", OrderDirection.Descending)
};
var b = new List<OrderRule> {
new OrderRule("name", OrderDirection.Ascending),
new OrderRule("age", OrderDirection.Descending)
};
var r = a.SequenceEqual(b);
Assert.Equal(a, b);
The variable r is true but Assert.Equal is false ...
The OrderRule class is the following:
public class OrderRule : IEquatable<OrderRule> {
public OrderDirection Direction { get; }
public String Property { get; }
public OrderRule(String property, OrderDirection direction) {
Direction = direction;
Property = property;
}
public Boolean Equals(OrderRule other) {
if (other == null)
return false;
return Property.Equals(other.Property) && Direction.Equals(other.Direction);
}
public override Boolean Equals(Object obj) {
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals(obj as IncludeRule);
}
public override Int32 GetHashCode() {
return HashCode.Of(Property).And(Direction);
}
}
public enum OrderDirection { ASC, DESC }
Is there any problem with Assert.Equal when overriding Equals and implementing IEquatable?
UPDATE - HashCode helper
public struct HashCode {
private readonly Int32 Value;
private HashCode(Int32 value) {
Value = value;
}
public static implicit operator Int32(HashCode hashCode) {
return hashCode.Value;
}
public static HashCode Of<T>(T item) {
return new HashCode(GetHashCode(item));
}
public HashCode And<T>(T item) {
return new HashCode(CombineHashCodes(Value, GetHashCode(item)));
}
public HashCode AndEach<T>(IEnumerable<T> items) {
Int32 hashCode = items.Select(x => GetHashCode(x)).Aggregate((x, y) => CombineHashCodes(x, y));
return new HashCode(CombineHashCodes(Value, hashCode));
}
private static Int32 CombineHashCodes(Int32 x, Int32 y) {
unchecked {
return ((x << 5) + x) ^ y;
}
}
private static Int32 GetHashCode<T>(T item) {
return item == null ? 0 : item.GetHashCode();
}
}
Your code works as expected on my side. I've only fixed compilation erros - IncludeRule changed to OrderRule in Equals, also fixed OrderDirection enum members.

Sorting List of objects having list in it in java

I have a class Person which has couple of variables. The class City has List of Person objects as its only variable. And the class State has List of city objects as its only variable.
From a test class i created a arraylist containing City objects. How do I sort the City objects in the list? i.e., how to sort an arraylist containing a type of objects, where each object has another list in it.
Person class
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public static Comparator<Person> personComparator = new Comparator<Person>() {
#Override
public int compare(Person o1, Person o2) {
int returnResult = o1.getName().compareTo(o2.getName());
if (returnResult == 0) {
return o1.getAge().compareTo(o2.getAge());
}
return returnResult;
}
};
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Person))
return false;
Person other = (Person) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
City class:
public class City {
private List<Person> personList;
public List<Person> getPersonList() {
if (personList == null) {
personList = new ArrayList<Person>();
}
return personList;
}
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((personList == null) ? 0 : personList.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof City))
return false;
City other = (City) obj;
if (personList == null) {
if (other.personList != null)
return false;
} else if (!personList.equals(other.personList))
return false;
return true;
}
}
State class:
public class State {
private List<City> cityList;
public List<City> getCityList() {
if (cityList == null) {
cityList = new ArrayList<City>();
}
return cityList;
}
public void setCityList(List<City> cityList) {
this.cityList = cityList;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((cityList == null) ? 0 : cityList.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof State))
return false;
State other = (State) obj;
if (cityList == null) {
if (other.cityList != null)
return false;
} else if (!cityList.equals(other.cityList))
return false;
return true;
}
}
public class TestClass {
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("John");
p1.setAge(40);
Person p2 = new Person();
p2.setName("Antony");
p2.setAge(50);
Person p3 = new Person();
p3.setName("Bob");
p3.setAge(24);
Person p4 = new Person();
p4.setName("Mark");
p4.setAge(35);
City city1 = new City();
city1.getPersonList().add(p1);
city1.getPersonList().add(p2);
City city2 = new City();
city2.getPersonList().add(p3);
city2.getPersonList().add(p4);
State state1 = new State();
state1.getCityList().add(city1);
state1.getCityList().add(city2);
//How to sort the citylist in the state object?
}
}
Develop a method called compareTo that compares some field within the city object to determine the order that you want, after you do that, you can call the method Collections.sort(); on the initialized array list object. Look here find out more.
You could check out the Comparable interface. Here is the documentation.

Extending webapi 2.1's help page to include additional data

I found a pretty dated post from Yao at microsoft advanced help page customizations
I have tried to implement the steps however I am unable to get my custom comment to display within the help documentation.
Within my
XmlDocumentationProvider.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using System.Xml.XPath;
namespace MyApp.Areas.HelpPage
{
/// <summary>
/// A custom <see cref="IDocumentationProvider"/> that reads the API documentation from an XML documentation file.
/// </summary>
public class XmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
{
private XPathNavigator _documentNavigator;
private const string TypeExpression = "/doc/members/member[#name='T:{0}']";
private const string MethodExpression = "/doc/members/member[#name='M:{0}']";
private const string PropertyExpression = "/doc/members/member[#name='P:{0}']";
private const string FieldExpression = "/doc/members/member[#name='F:{0}']";
private const string ParameterExpression = "param[#name='{0}']";
/// <summary>
/// Initializes a new instance of the <see cref="XmlDocumentationProvider"/> class.
/// </summary>
/// <param name="documentPath">The physical path to XML document.</param>
public XmlDocumentationProvider(string documentPath)
{
if (documentPath == null)
{
throw new ArgumentNullException("documentPath");
}
XPathDocument xpath = new XPathDocument(documentPath);
_documentNavigator = xpath.CreateNavigator();
}
public string GetDocumentation(HttpControllerDescriptor controllerDescriptor)
{
XPathNavigator typeNode = GetTypeNode(controllerDescriptor.ControllerType);
return GetTagValue(typeNode, "summary");
}
public virtual string GetDocumentation(HttpActionDescriptor actionDescriptor)
{
XPathNavigator methodNode = GetMethodNode(actionDescriptor);
return GetTagValue(methodNode, "summary");
}
public virtual string GetActionTypes(HttpActionDescriptor actionDescriptor)
{
XPathNavigator methodNode = GetMethodNode(actionDescriptor);
return GetTagValue(methodNode, "action");
}
public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
{
ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor;
if (reflectedParameterDescriptor != null)
{
XPathNavigator methodNode = GetMethodNode(reflectedParameterDescriptor.ActionDescriptor);
if (methodNode != null)
{
string parameterName = reflectedParameterDescriptor.ParameterInfo.Name;
XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, ParameterExpression, parameterName));
if (parameterNode != null)
{
return parameterNode.Value.Trim();
}
}
}
return null;
}
public string GetResponseDocumentation(HttpActionDescriptor actionDescriptor)
{
XPathNavigator methodNode = GetMethodNode(actionDescriptor);
return GetTagValue(methodNode, "returns");
}
public string GetDocumentation(MemberInfo member)
{
string memberName = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", GetTypeName(member.DeclaringType), member.Name);
string expression = member.MemberType == MemberTypes.Field ? FieldExpression : PropertyExpression;
string selectExpression = String.Format(CultureInfo.InvariantCulture, expression, memberName);
XPathNavigator propertyNode = _documentNavigator.SelectSingleNode(selectExpression);
return GetTagValue(propertyNode, "summary");
}
public string GetDocumentation(Type type)
{
XPathNavigator typeNode = GetTypeNode(type);
return GetTagValue(typeNode, "summary");
}
private XPathNavigator GetMethodNode(HttpActionDescriptor actionDescriptor)
{
ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor;
if (reflectedActionDescriptor != null)
{
string selectExpression = String.Format(CultureInfo.InvariantCulture, MethodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));
return _documentNavigator.SelectSingleNode(selectExpression);
}
return null;
}
private static string GetMemberName(MethodInfo method)
{
string name = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", GetTypeName(method.DeclaringType), method.Name);
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length != 0)
{
string[] parameterTypeNames = parameters.Select(param => GetTypeName(param.ParameterType)).ToArray();
name += String.Format(CultureInfo.InvariantCulture, "({0})", String.Join(",", parameterTypeNames));
}
return name;
}
private static string GetTagValue(XPathNavigator parentNode, string tagName)
{
if (parentNode != null)
{
XPathNavigator node = parentNode.SelectSingleNode(tagName);
if (node != null)
{
return node.Value.Trim();
}
}
return null;
}
private XPathNavigator GetTypeNode(Type type)
{
string controllerTypeName = GetTypeName(type);
string selectExpression = String.Format(CultureInfo.InvariantCulture, TypeExpression, controllerTypeName);
return _documentNavigator.SelectSingleNode(selectExpression);
}
private static string GetTypeName(Type type)
{
string name = type.FullName;
if (type.IsGenericType)
{
// Format the generic type name to something like: Generic{System.Int32,System.String}
Type genericType = type.GetGenericTypeDefinition();
Type[] genericArguments = type.GetGenericArguments();
string genericTypeName = genericType.FullName;
// Trim the generic parameter counts from the name
genericTypeName = genericTypeName.Substring(0, genericTypeName.IndexOf('`'));
string[] argumentTypeNames = genericArguments.Select(t => GetTypeName(t)).ToArray();
name = String.Format(CultureInfo.InvariantCulture, "{0}{{{1}}}", genericTypeName, String.Join(",", argumentTypeNames));
}
if (type.IsNested)
{
// Changing the nested type name from OuterType+InnerType to OuterType.InnerType to match the XML documentation syntax.
name = name.Replace("+", ".");
}
return name;
}
public interface ICustomCommentProvider
{
string GetCustomComment(HttpActionDescriptor actionDescriptor);
}
public virtual string GetCustomComment(HttpActionDescriptor actionDescriptor)
{
XPathNavigator methodNode = GetMethodNode(actionDescriptor);
if (methodNode != null)
{
XPathNavigator returnsNode = methodNode.SelectSingleNode("customcomment");
if (returnsNode != null)
{
return returnsNode.Value.Trim();
}
}
return null;
}
}
}
I have modifie
public interface ICustomCommentProvider
{
string GetCustomComment(HttpActionDescriptor actionDescriptor);
}
public virtual string GetCustomComment(HttpActionDescriptor actionDescriptor)
{
XPathNavigator methodNode = GetMethodNode(actionDescriptor);
if (methodNode != null)
{
XPathNavigator returnsNode = methodNode.SelectSingleNode("customcomment");
if (returnsNode != null)
{
return returnsNode.Value.Trim();
}
}
return null;
}
I have updated the
HelpPageModel.cs
to include
public string CustomMessage { get; set; }
Last thing I have updated is the GenerateApiModel
private static HelpPageApiModel GenerateApiModel(ApiDescription apiDescription, HttpConfiguration config)
{
HelpPageApiModel apiModel = new HelpPageApiModel()
{
ApiDescription = apiDescription,
};
var responseDocProvider =
config.Services.GetDocumentationProvider() as XmlDocumentationProvider.ICustomCommentProvider;
if (responseDocProvider != null)
{
apiModel.CustomMessage = responseDocProvider.GetCustomComment(apiDescription.ActionDescriptor);
}
Everytime I run the application responseDocProvider is null. Is there anything sticking out that looks like I have done it wrong?
Help on the problem would be very much appreciated
You would need to set the documentation provider. You can uncomment the following piece of code present under Areas\HelpPage\App_Start\HelpPageConfig.cs
//// Uncomment the following to use the documentation from XML documentation file.
//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
ANSWER FROM COMMENT (Kiran Challa):
but that would not work right..as your type is not implementing that interface and you are trying to cast to it...change the
XmlDocumentationProvider
to implementing the ICustomCommentProvider interface
and do a
var responseDocProvider =config.Services.GetDocumentationProvider() as ICustomCommentProvider;

How do validators work on the client side?

I am developing a simple validator. It works fine with server side requests but it does not work on the client side because of the forbidden post back. .Net's RequiredAttribute validator works on the client side and does not need a post back.
How can I change my validator to work like it?
My validator gets a string and checks if it is at least 5 characters long.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
AttributeTargets.Parameter, AllowMultiple = false)]
public class MyValidatorAttribute : ValidationAttribute
{
private readonly int _length = 0;
public MyValidatorAttribute(int length)
{
_length = length;
}
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
if (value != null)
{
string data = (string)value;
if (data.Length < _length)
{
return new ValidationResult("length is short");
}
}
return ValidationResult.Success;
}
}
public class MyValidator : DataAnnotationsModelValidator<MyValidatorAttribute>
{
int minlength = 5;
string errorMsg = string.Empty;
public MyValidator(ModelMetadata metadata,
ControllerContext context,
MyValidatorAttribute attribute)
: base(metadata, context, attribute)
{
errorMsg = attribute.ErrorMessage;
}
public override IEnumerable<ModelClientValidationRule>
GetClientValidationRules()
{
ModelClientValidationRule validationRule =
new ModelClientValidationRule();
validationRule.ErrorMessage = errorMsg;
validationRule.ValidationType = "MyValidator";
validationRule.ValidationParameters.Add("minlength", minlength);
return new[] { validationRule };
}
}

Why am I getting duplicated row inserts?

I have windows phone 7 application and classes for database mapping.
A Poll class looks like that:
[Table]
public class Poll // :BaseModel
{
//private int _pid;
//private string _pdesc;
//private bool _pisopen;
//private string _pname;
//private bool _prandom;
//private string _qlastupdticks;
//private string _ticks;
[Column(DbType = "INT NOT NULL IDENTITY(1,1)", IsPrimaryKey = true, IsDbGenerated = true)]
public int id { get; set; }
[Column(DbType = "INT")]
public int pid { get; set; }
//{
// get { return _pid; }
// set { SetValue(ref _pid, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
[Column]
public string pdesc { get; set; }
//{
// get { return _pdesc; }
// set { SetValue(ref _pdesc, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
[Column]
public bool pisopen { get; set; }
//{
// get { return _pisopen; }
// set { SetValue(ref _pisopen, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
[Column]
public string pname { get; set; }
//{
// get { return _pname; }
// set { SetValue(ref _pname, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
[Column]
public bool prandom { get; set; }
//{
// get { return _prandom; }
// set { SetValue(ref _prandom, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
[Column(DbType = "NVARCHAR(255)")]
public string qlastupdticks { get; set; }
//{
// get { return _qlastupdticks; }
// set { SetValue(ref _qlastupdticks, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
[Column(DbType = "NVARCHAR(255)")]
public string ticks { get; set; }
//{
// get { return _ticks; }
// set { SetValue(ref _ticks, value, GetPropertyName(MethodBase.GetCurrentMethod())); }
//}
public override bool Equals(object obj)
{
var item = obj as Poll;
if (item != null)
{
Equals(item);
}
return false;
}
public bool Equals(Poll other)
{
if (ReferenceEquals(null, other)) return false;
return (other.pid == pid);
}
public override string ToString()
{
return string.Format("{0}_{1}", GetType().Name, pid);
}
public override int GetHashCode()
{
return ToString().ToUpper().GetHashCode();
}
}
Save method looks like that:
public bool RowsSave<T>(IEnumerable<T> entities, out string error)
{
error = string.Empty;
bool res;
var type = typeof (T);
using (var ctx = new PpaDataContext(_connectionString))
{
try
{
var entitesInDb = ctx.GetTable(type).Cast<T>().ToList();
var entitesForSave = new List<T>();
foreach (var entity in entities)
{
if (!entitesInDb.Contains(entity))
{
entitesForSave.Add(entity);
}
else
{
var index = entitesInDb.IndexOf(entity);
foreach (var prop in PropertiesGet(entity))
{
prop.SetValue(entitesInDb[index], prop.GetValue(entity, null), null);
}
}
}
if(entitesForSave.Count > 0)
{
ctx.GetTable(type).InsertAllOnSubmit(entitesForSave);
}
ctx.SubmitChanges();
res = true;
}
catch (Exception ex)
{
error = string.Format("{0}", ex.Message);
res = false;
}
}
return res;
}
When I try insert, each object inserts twice. What incorrect in this code?
The RowsSave method calling one time only.
This is a bit after the fact, but I've run into the exact same problem. This has to do with an error in overridden equality operators. In this specific case, the problem appears because the code does not return the value of the Equals function in:
public override bool Equals(object obj)
{
var item = obj as Poll;
if (item != null)
{
Equals(item);
}
return false;
}
That code will always return false. The corrected code should be:
public override bool Equals(object obj)
{
var item = obj as Poll;
if (item != null)
{
return Equals(item);
}
return false;
}
LINQ-to-SQL uses the object equals comparison after insertions. I'm not sure in what context and for what reason this happens, but if the comparison returns false, it runs the insertion again. If you're experiencing the same behavior, check any overridden equality methods for your entity for correctness.
I don't think you need the line:
ctx.SubmitChanges();
as an insert is being done on the line:
ctx.GetTable(type).InsertAllOnSubmit(entitesForSave);

Resources