Property class

Let's say that you have the following class:

public class User
{
  public string UserName {get;set;}
  public int Age {get;set;}
  public AccountLevel Level {get;set;}
  public DateTime RegisteredAt {get;set;}
  public bool IsActive  {get;set;}
}
[Flags]
public enum AccountLevel
{
  User = 1,
  Admin = 1 + 2
}


Then you can do all this:

  User user = new User();
  Property.Set(user, "UserName", "Arne");
  string firstName = Property.Get(user, "UserName");
  
  // bool can be used in these ways
  Property.Set(user, "IsActive", 1);
  Property.Set(user, "IsActive", true);
  Property.Set(user, "IsActive", "true");

  // Enums are converted to the correct type:
  Property.Set(user, "Level", 3); // <-- will be set to UserLevel.Admin
  Property.Set(user, "Level", "User"); // <-- will be set to UserLevel.User
  Property.Set(user, "Level", UserLevel.Admin);

  // ints are also converted from strings:
  Property.Set(user, "Age", 19);
  Property.Set(user, "Age", "10");


We created this class to make form processing a lot easier in our webserver (see http://www.codeplex.com/webserver). Consider having this html form:

<form method="post" action="/user/edit/1">
<input name="user[UserName]" />
<input name="user[Age]" />
<input name="user[Level]" />
</form>


With our webserver we can assign all fields with to lines of code:

User user = _dataLayer.GetById<User>(Id);
foreach (HttpInput item in Request.Form["user"])
  Property.Set(user, item.name, item.Value);
_dataLayer.Save(user);


Easy, huh? The Data layer can be found in Fadd.Data namespace. No documentation yet though.

Last edited Dec 10, 2008 at 12:10 PM by jgauffin, version 2

Comments

No comments yet.