NerdDinner is another example which is developed by Microsoft to demonstrate the way to build asp.net mvc application. The contact manger example is developed by Stephen Walther, while the NerdDinner is developed by Scott Hanselman. Both are very gurus in the asp.net mvc fields.
Similar to the contact management example, it uses a repository pattern to CRUD data,
public interface IDinnerRepository {
IQueryable FindAllDinners();
IQueryable FindByLocation(float latitude, float longitude);
IQueryable FindUpcomingDinners();
Dinner GetDinner(int id);
void Add(Dinner dinner);
void Delete(Dinner dinner);
void Save();
} The difference is
- NerdDinner uses LINQ2SQL while the ContactManagment uses the entity framework.
- ContactManagment uses a service layer to bridge the communication between control and the model, while the NerdDinner passes the repository layer to the controller directly. Personally, I like the service layer a little better even it causes another layer of indirection.
- ContactManagment uses IValidationDictionary and a ModelStateWrapper wrapper to manage the validation logic. This ModelStateWrapper will be passed to the service layer in turn.
public interface IValidationDictionary
{
void AddError(string key, string errorMessage);
bool IsValid {get;}
}
public class ModelStateWrapper : IValidationDictionary
{
private ModelStateDictionary _modelState;
public ModelStateWrapper(ModelStateDictionary modelState)
{
_modelState = modelState;
}
public void AddError(string key, string errorMessage)
{
_modelState.AddModelError(key, errorMessage);
}
public bool IsValid
{
get { return _modelState.IsValid; }
}
}
while NerdDinner uses RuleViolation object to manage the error.
public class RuleViolation {
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage) {
ErrorMessage = errorMessage;
}
public RuleViolation(string errorMessage, string propertyName) {
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
} and defines public IEnumerable
public partial class Dinner {
public bool IsValid {
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerableGetRuleViolations() {
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title is required", "Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description is required", "Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy is required", "HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address is required", "Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country is required", "Address");
if (String.IsNullOrEmpty(ContactPhone))
yield return new RuleViolation("Phone# is required", "ContactPhone");
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
yield break;
}
partial void OnValidate(ChangeAction action) {//partial method for LINQ generated code.
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
I cannot say which is better, but I still prefer the Contact Manager Example.
Both projects use Moq framework to mock the interface and abstract class.
0 comments:
Post a Comment