-
-
Save amogram/10740818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model UserViewModel | |
@using (Html.BeginForm()) | |
{ | |
<div> | |
@Html.HiddenFor(m => m.Id) | |
@Html.TextBoxFor(m => m.FirstName) | |
@Html.TextBoxFor(m => m.LastName) | |
@Html.DropDownListFor(m => m.CountryId, Model.Countries) | |
<input type="submit" /> | |
</div> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IModelBuilder<TViewModel, TEntity> | |
{ | |
TViewModel CreateFrom(TEntity entity); | |
TViewModel Rebuild(TViewModel model); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IModelCommand<TInput> | |
{ | |
void Execute(TInput model); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserEditCommand : IModelCommand<UserEditModel> | |
{ | |
readonly ISession session; | |
public UserEditCommand(ISession session) | |
{ | |
this.session = session; | |
} | |
public void Execute(UserEditModel model) | |
{ | |
var user = string.IsNullOrEmpty(model.Id) ? new User() : session.Find<User>(model.Id); | |
session.Store(user); | |
user.FirstName = model.FirstName; | |
user.LastName = model.LastName; | |
user.Country = session.Find<Country>(model.CountryId); | |
session.SaveChanges(); | |
// Auditing and other interesting things can happen here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserController : Controller | |
{ | |
UserViewModelBuilder builder = new UserViewModelBuilder(); | |
UserEditCommand saveCommand = new UserEditCommand(); | |
public ActionResult Edit(string id) | |
{ | |
var user = session.Find<User>(id) ?? new User(); | |
return View(model, builder.CreateFrom(user)); | |
} | |
[HttpPost] | |
public ActionResult Edit(UserViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return View(builder.Rebuild(model); | |
} | |
saveCommand.Execute(model); | |
return RedirectToAction("Index"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserEditModel | |
{ | |
public string Id { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public string CountryId { get; set; } | |
} | |
class UserViewModel : UserEditModel | |
{ | |
public ICollection<SelectListItem> Countries { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserViewModelBuilder : IModelBuilder<UserViewModel, User> | |
{ | |
readonly ISession session; | |
public UserViewModelBuilder(ISession session) | |
{ | |
this.session = session; | |
} | |
public UserViewModel CreateFrom(User user) | |
{ | |
var model = new UserViewModel(); | |
model.Id = user.FirstName; | |
model.FirstName = user.FirstName; | |
model.LastName = user.LastName; | |
model.Country = user.Country.Id; | |
model.Countries = GetCountries(); | |
return model; | |
} | |
public UserViewModel Rebuild(UserViewModel model) | |
{ | |
model.Countries = GetCountries(); | |
return model; | |
} | |
ICollection<SelectListItem> GetCountries() | |
{ | |
var countries = session.FindAll<Country>(); | |
return countries.Select(c => new SelectListItem { Value = c.Id, Text = c.Name }).ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment