Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Foo; // the MVC web project
- Foo.Models;
- Foo.Repositories;
- Foo.Services;
- public class User
- {
- public string Username { get; set; }
- public string Email { get; set; }
- public string Password { get; set; }
- }
- public class UserBinder : DefaultModelBinder
- {
- //...
- }
- public string Email { get; set; }
- public string Password { get; set; }
- public ICollection<KeyValuePair<string, string>> ValidateErrors()
- {
- //Validate if Username, Email and Password has been passed
- }
- // validate everything here
- base.OnModelUpdated(controllerContext, bindingContext);
- }
- public ActionResult InputAddress(InputAddressViewModel model)
- {
- if (ModelState.IsValid)
- {
- // "Front-line" validation passed; let's execute the save operation
- // in the our view model
- var result = model.Execute();
- // The view model returns a status code to help the
- // controller decide where to redirect the user next
- switch (result.Status)
- {
- case InputAddressViewModelExecuteResult.Saved:
- return RedirectToAction("my-work-is-done-here");
- case InputAddressViewModelExecuteResult.UserCorrectableError:
- // Something went wrong after we interacted with the
- // datastore, like a bogus Canadian postal code or
- // something. Our view model will have updated the
- // Error property, but we need to call TryUpdateModel()
- // to get these new errors to get added to
- // the ModelState, since they were just added and the
- // model binder ran before this method even got called.
- TryUpdateModel(model);
- break;
- }
- // Redisplay the input form to the user, using that nifty
- // Html.ValidationMessage to convey model state errors
- return View(model);
- }
- }
- public InputAddressViewModelExecuteResult Execute()
- {
- InputAddressViewModelExecuteResult result;
- if (this.errors.Count > 0)
- {
- throw new InvalidOperationException(
- "Don't call me when I have errors");
- }
- // This is just my abstraction for clearly demarcating when
- // I have an open connection to a highly contentious resource,
- // like a database connection or a network share
- using (ConnectionScope cs = new ConnectionScope())
- {
- var scheme = new AddressSchemeRepository().Load(this.Country);
- var builder = new AddressBuilder(scheme)
- .WithCityAs(this.City)
- .WithStateOrProvinceAs(this.StateOrProvince);
- if (!builder.CanBuild())
- {
- this.errors.Add("Blah", builder.Error);
- result = new InputAddressViewModelExecuteResult()
- {
- Status = InputAddressViewModelExecuteStatus
- .UserCorrectableError
- };
- }
- else
- {
- var address = builder.Build();
- // save the address or something...
- result = new InputAddressViewModelExecuteResult()
- {
- Status = InputAddressViewModelExecuteStatus.Success,
- Address = address
- };
- }
- }
- return result;
- }
- public class UserRepository : IRepository<User>
- {
- public void Create(User user)
- {
- user.Validate();
- var db = dbFooEntities();
- db.AddToUsers(user);
- db.SaveChanges();
- }
- }
- class User
- {
- string Username { get; set; }
- string Email { get; set; }
- string Password { get; set; } // hashed and salted of course :)
- IEnumerable<RuleViolation> Validate()
- {
- List<RuleViolation> violations = new List<RuleViolation>();
- IUserService service = MyApplicationService.UserService; // MyApplicationService is a singleton class, especialy designed so that the User model can access application services
- // Username is required
- if ( string.IsNullOrEmpty(Username) )
- violations.Add(new RuleViolation("Username", "Username is required"));
- // Username must be unique: Should uniqueness be validated here?
- else if( !service.IsUsernameAvailable(Username)
- violations.Add(new RuleViolation("Username", "Username is already taken!"));
- // Validate email etc...
- return violations;
- }
- }
- interface IUserRepository
- {
- void Save(User item);
- }
- interface IUserService
- {
- IUserRepository UserRepository { get; }
- void Save(User item);
- }
- class UserService : IUserService
- {
- public UserService(IUserRepository userRepository)
- {
- this.UserRepository = userRepository;
- }
- IUserRepository UserRepository { get; private set}
- public void Save(User user)
- {
- IEnumerable<RuleViolation> violations = user.Validate();
- if(violations.Count() > 0)
- throw new RuleViolationException(violations); // this will be catched by the Controller, which will copy the violations to the ModelState errors collection. But the question is, should we validat the user here, or in the UserRepository class?
- UserRepository.Save(user);
- }
- }
- class UserRepository : IUserRepository
- {
- void Save(User item)
- {
- IEnumerable<RuleViolation> violations = user.Validate();
- if(violations.Count() > 0)
- throw new RuleViolationException(violations); // this will be catched by the Controller, which will copy the violations to the ModelState errors collection. But the question is, should we validate the user here, or in the UserService class?
- UserRepository.Save(user);
- }
- }
- Html.AutoForm(Model);
- Html.AutoGrid(Model.Products);
Add Comment
Please, Sign In to add comment