Blog de Francisco Velázquez

otro blog personal

Modelando el negocio

with one comment

Leyendo el libro de ASP.NET MVC 1.0, a parte de aprender un poco de MVC, estoy refrescando algunos conceptos de modelado de datos. Uno que me ha parecido muy interesante es la manera de modelar el acceso a datos. Para ello recomiendan que lo que actualmente en mi modelado es una clase entidad (aún sigo usando el motor de persistencia Gentle.NET), se recomienda que se divida en 3, siguiendo el patrón Repositorio.

– DomainClass
– IRepository
– RepositoyClass

Otro punto interesante es como se incluyen las reglas de validación y reglas del negocio en las clases del dominio.

public partial class Dinner { 

    public bool IsValid {
        get { return (GetRuleViolations().Count() == 0); }
    } 

     public IEnumerable<RuleViolation> GetRuleViolations() { 

        if (String.IsNullOrEmpty(Title))
            yield return new RuleViolation("Title required", "Title"); 

        if (String.IsNullOrEmpty(Description))
            yield return new RuleViolation("Description required","Description"); 

        if (String.IsNullOrEmpty(HostedBy))
            yield return new RuleViolation("HostedBy required", "HostedBy"); 

        if (String.IsNullOrEmpty(Address))
            yield return new RuleViolation("Address required", "Address"); 

        if (String.IsNullOrEmpty(Country))
            yield return new RuleViolation("Country required", "Address"); 

        if (String.IsNullOrEmpty(ContactPhone))
            yield return new RuleViolation("Phone# required", "ContactPhone"); 

        if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
            yield return new RuleViolation("Phone# does not match country",
                                           "ContactPhone"); 

        yield break;
    } 

    partial void OnValidate(ChangeAction action) {
        if (!IsValid)
            throw new ApplicationException("Rule violations prevent saving");
    }
}

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;
    }
} 

Written by fravelgue

November 25, 2009 at 10:19 pm

Posted in development

Tagged with

One Response

Subscribe to comments with RSS.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: