Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 4.68 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to handle custom Properties in AutoMapper
  2. public class UsersDetailsViewModel
  3. {
  4.     public string UserName { get; set; }
  5.     public string Email { get; set; }
  6.     public string Website { get; set; }
  7.     public int ID { get; set; }
  8.     public List<OpenID> OpenIds { get; set; }
  9.     public string UserAge { get; set; }
  10.     public string About { get; set; }
  11.     public string Slug { get; set; }
  12.     public System.DateTime LastSeen { get; set; }
  13.     public string Region { get; set; }
  14.     public string MemberSince { get; set; }
  15.     public string Reputation { get; set; }
  16.     public bool IsUserMatch { get; set; }
  17.  
  18.     private readonly MarkdownDeep.Markdown _markdown;
  19.  
  20.  
  21.     public UsersDetailsViewModel(Domain.User user)
  22.     {
  23.         AuthUserData currentuser = AuthenticationHelper.RetrieveAuthUser;
  24.         _markdown.NoFollowLinks = true;
  25.         _markdown.SafeMode = true;
  26.         _markdown.ExtraMode = false;
  27.         _markdown.MarkdownInHtml = true;
  28.  
  29.         // We want to ensure that the user has a username, even if they
  30.         // haven't set one yet. What this does is check to see if the
  31.         // user.UserName field is blank, and if it is, it will set the
  32.         // username to "UserNNNN" where NNNN is the user ID number.
  33.         _UserName = (user.UserName != null) ? user.UserName : "User" + user.ID.ToString;
  34.  
  35.         // Nothing fancy going on here, we're just re-passing the object from
  36.         // the database to the View. No data manipulation!
  37.         _Email = user.Email;
  38.         _Website = user.WebSite;
  39.         _ID = user.ID;
  40.  
  41.         // Get's a list of all of the user's OpenID's
  42.         _OpenIds = user.OpenIDs.ToList;
  43.  
  44.         // Converts the users birthdate to an age representation
  45.         _UserAge = user.BirthDate.ToAge;
  46.         //IE: 29
  47.  
  48.         // Because some people can be real ass holes and try to submit bad
  49.         // data (scripts and shitè) we have to modify the "About" content in
  50.         // order to sanitize it.  At the same time, we transform the Markdown
  51.         // into valid HTML. The raw input is stored without sanitization in
  52.         // the database.  this could mean Javascript injection, etc, so the
  53.         // output ALWAYS needs to be sanitized.
  54.  
  55.         // This method below was used in conjunction with MarkDownSharp
  56.         // _About = Trim(Utilities.HtmlSanitizer.Sanitize(Markdown.Transform(user.About)))
  57.         _About = _markdown.Transform(user.About);
  58.  
  59.         // Removes spaces from Usernames in order to properly display the
  60.         // username in the address bar
  61.         _Slug = Strings.Replace(user.UserName, " ", "-");
  62.  
  63.         // Returns a boolean result if the current logged in user matches the
  64.         // details view of tBhe user in question.  This is done so that we can
  65.         // show the edit button to logged in users.
  66.         _IsUserMatch = (currentuser.ID == user.ID);
  67.  
  68.  
  69.         // Grabs the users registration data and formats it to a <time> tag
  70.         // for use with the timeago jQuery plugin
  71.         _MemberSince = user.MemberSince;
  72.  
  73.         // Grabs the users last activity and formats it to a <time> tag
  74.         // for use with the timeago jQuery plugin
  75.         _LastSeen = user.ActivityLogs.Reverse.FirstOrDefault.ActivityDate;
  76.  
  77.         // Formats the users reputation to a comma Deliminated number
  78.         //    IE: 19,000 or 123k
  79.         _Reputation = user.Reputation.ToShortHandNumber;
  80.  
  81.  
  82.         // Get the name of the users Default Region.
  83.         _Region = user.Region.Name.FirstOrDefault;
  84.     }
  85.  
  86. }
  87.        
  88. public ActionResult Details(int id)
  89. {
  90.     User user = _userService.GetUserByID(id);
  91.  
  92.     if (user != null) {
  93.         Domain.ViewModels.UsersDetailsViewModel userviewmodel = new Domain.ViewModels.UsersDetailsViewModel(user);
  94.         return View(userviewmodel);
  95.     } else {
  96.         // Because of RESTful URL's, some people will want to "hunt around"
  97.         // for other users by entering numbers into the address.  We need to
  98.         // gracefully redirect them to a not found page if the user doesn't
  99.         // exist.
  100.         throw new ResourceNotFoundException();
  101.     }
  102.  
  103. }
  104.        
  105. Mapper.Map<Domain.User, UsersDetailsViewModel>();
  106.        
  107. Mapper.Map<Domain.User, UsersDetailsViewModel>()
  108.       .ForMember(vm=>vm.UserName, m=>m.MapFrom(u=>(u.UserName != null)
  109.                                                ? u.UserName
  110.                                                : "User" + u.ID.ToString()));
  111.        
  112. Mapper.Map<Domain.User, UsersDetailsViewModel>()
  113.   .ForMember(vm=>vm.IsUserMatch, m=>m.ResolveUsing<MatchingUserResolver>()));
  114.        
  115. AutoMapper.Mapper.CreateMap<Domain.User, UsersDetailsViewModel>()
  116.           .ForMember(o => o.Email, b => b.MapFrom(z => z.Email))
  117.           .ForMember(o => o.UserName , b => b.MapFrom(user => (user.UserName != null) ? user.UserName : "User" + user.ID.ToString));