开发者

Extending entity framework classes

Even many Q/A on the subject, I didn't find a clear answer for this question:

What's the best design practice for adding business rules (i.e, validations) to entity classes.

I simply want to check some validations before setting the underlying entity value:

public Property
{
    get { return base.Property; }
    set
    {
   开发者_C百科    // Do some validations or other business logic
       base.Property = value;
    }
}

It doesn't make sense to create a new class from scratch in BLL when all properties are already there in entity class. On the other hand, entity classes need to be extended with business logic rules.

Using interface need extra work, because a change in DAL (entity) would be reflected in both interface and BLL class.

I'm not sure if inheriting from entity class and overriding it's properties and adding extra properties and methods is a good idea or not.

A sample pseudo code in more helpful to me.

Thanks


I would like to elaborate on Stephen Cleary's answer. He is correct in using the partial class/methods to handle business rules in EF. However, he did not go into much detail about what to do within that partial class/method. I created a URL shortening service on my blog to use as an example for this. My ShortURL entity has only two columns/properties. Url and ID.

I wanted to validate that the URL being shortened is a valid URL before it actually stores it in the database through EF. So I created a partial class and method like so:

public partial class ShortURL
{
    partial void OnUrlChanging(string url)
    {
        if (!Regex.IsMatch(url, @"(^((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)"))
            throw new Exception("Not a valid URL.");
    }
}

This stopped EF from changing the property, leaving it NULL. But that's all it did. It didn't give me an easy way to get at the error message and display it to the user (that I am aware of EDIT: According to http://www.sellsbrothers.com/posts/Details/12700 IDataErrorInfo is the only way to get the error message to display properly in ASP.NET MVC) so I followed another example I found in the dark recesses of the web somewhere and I made my partial class inherit from IDataErrorInfo. I then implemented the interface and included a private dictionary object to store error messages in.

public partial class ShortURL : IDataErrorInfo
{
    private Dictionary<string, string> errors = new Dictionary<string, string>();

    partial void OnUrlChanging(string url)
    {
        if (!Regex.IsMatch(url, @"(^((http|ftp|https):\/\/|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)"))
            errors.Add("Url", "Not a valid URL.");
    }

    public string Error
    {
        get { return string.Empty; } //I never use this so I just return empty.
    }

    public string this[string columnName]
    {
        get
        {
            if (errors.ContainsKey(columnName))
                return errors[columnName];
            return string.Empty; //Return empty if no error in dictionary.
        }
    }
}

Now, I have a fully-functioning way to store, retrieve, and display error messages. Now back in my controller (in MVC) I am able to do if (!ModelState.IsValid)

    [HttpPost]
    public ViewResult URLShortener(ShortURL shortURL)
    {
        if (!ModelState.IsValid)
            return View();
        shortURL.Url = shortURL.Url.ToLower().StartsWith("www.") ? "http://" + shortURL.Url : shortURL.Url;
        shortURLRepository.AddShortURL(shortURL);
        object model = "http://www.u413.com/" + ShortCodes.LongToShortCode(shortURL.UrlID);
        //Not related to this answer but I had to cast my string as a generic object because the View() method has a (string, string) constructor that does something totally different. My view actually uses string as the model. I know I know, I could have just used ViewBag.
        return View("ShowUrl", model);
    }

There ya go. A working example of how to not only extend EF's partial methods, but also how to propagate the validation back to the UI. Let me know if anything needs improving or if there was something I missed.


Check out your EF designer-generated code.

Each property Property is actually implemented like this:

    public global::System.String Property
    {
        get
        {
            return _Property;
        }
        set
        {
            OnPropertyChanging(value);
            ReportPropertyChanging("Property");
            _Property = StructuralObject.SetValidValue(value, false);
            ReportPropertyChanged("Property");
            OnPropertyChanged();
        }
    }
    private global::System.String _Property;
    partial void OnPropertyChanging(global::System.String value);
    partial void OnPropertyChanged();

The partial method On-Property-Changing is where you can do single-property validation or business logic.


Xaqron, the best way I have found it to use Partial Classes, for example, if you have a class in your EF called PropertyListing you can use a partial class like this:

Partial Public Class PropertyListing
    Inherits EntityObject

    'Do something here

End Class

You can now extend the class as little or as much as you want without much fuss. The example is in VB but you get the jist of it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜