R – How to put business logic in the ADO.NET Entity Framework classes

entity-frameworklinqlinq-to-entitiesnet

I'd like to use the ADO.NET Entity Framework for data access, extend its objects for my business logic, and bind those objects to controls in my UI.

As explained in the answers to another question, I cannot extend ADO.NET Entity Framework objects with partial classes and use my custom methods in LINQ queries.

ADO.NET Entity Framework partial class http://img221.imageshack.us/img221/7329/clientsq0.gif

I don't want methods showing up in the Intellisense that are going to create run-time errors! How should I architect my application to avoid this problem?

VB.NET LINQ with custom method http://img83.imageshack.us/img83/1580/iswashingtongn0.gif

Do I need a data access Client class and also a business logic Client class? It seems like that will get confusing.

Best Answer

You can architect your solution using (Plain Old C# Objects) POCO's and Managers.

That way you separate the business logic from the value objects.

To make it "look pretty", you can mark your methods with the (this) modifier on the parameters so you can then use those methods as extension methods.

An example could make this pretty clear:

Location Value Object:

public class Location
{
    public string City { get; set; }
    public string State { get; set; }
}

Location Manager:

public static class LocationManager
{
    public static bool IsWashington(this Location location)
    {
        return location.State == "WA";
    }
}

Now, the extension methods will show up differently than the standard properties/methods on the object.

The "IsWashington" method can be called 2 ways

Location location = new Location { State = "WA" };
LocationManager.IsWashington(location);

OR

Location location = new Location { State = "WA" };
location.IsWashington();

Now you have separation of your business logic and value objects, yet you still can have "pretty" method calls.

If you feel your fellow devs (or you :) ) will abuse the extension method part, then just don't use it.