YOUR FEEDBACK
Chris Keene's Prescription for Curing the Java Flu
Pedro wrote: "Adobe and Microsoft are doing a far better job making their ...

SYS-CON.TV
TOP MICROSOFT .NET LINKS


Taking Advantage of the Partial Class with the ADO.NET Entity Framework
So you're building a data-driven application and you've got an ADO.NET Entity Model abstraction around your database

Digg This!

So you're building your data-driven application and you've got an ADO.NET Entity Model that represents an abstraction around your database. Maybe you're even pretty savvy and you've used inheritance and some filters to enhance the entity model so that it really is an entity model and not just a raw translation of your database schema into objects. One thing that I have noticed is that in a lot of sample code, a lot of utility functions end up being put in inefficient locations because people forget that the entity model is a partial class. This means that you can extend the model with your own properties and methods.

For example, what if you want to implement a keyword search throughout your entity model and you know that you will probably have multiple entry points in your application utilize that search function. You could probably create a class called EntityTools or some such and create a method called Search(string keyWord) , but there's a better place to put that - right in the model!

Take a look at this method:

public List<Customer> Search(string keyWord)
{


    // also do whatever other normalization you
want like removing filler words, etc

keyWord = keyWord.ToLower();
    var query = from Customer cust in this.Customers
        where cust.FirstName.Contains(keyWord) ||
            cust.LastName.Contains(keyWord) ||
        cust.OrderItems.Any ( oi =>
oi.LineItem.Description.Contains(keyWord)
        select cust;
      return query.ToList();
}

So.. now you can write code that consumes your model like this:

using (CustomerEntities ci = new CustomerEntities())
{
    List<Customer> searchResults = ci.Search("bob");
}

Not only is this easier to follow and easier to read, but its easy to maintain and you can re-use those same methods for any entity model that happens to have the same or similar schema. You can also define custom properties that expose data in a specific way that you know you will be querying frequently:

public IQueryable<Customer> PastDueCustomers
{
    get {
        return from Customer cust in this.Customers
            where cust.OrderItems.Sum ( oi => oi.Amount ) > 0
            select cust;
    }
}

This code automatically creates a queryable property of customers that have unpaid balances remaining in their line item lists. Can you imagine having to do subqueries of Past Due Customers, for example, sorting the list of past due customers by their past due amount and then sub-filtering that list by those customers who are not on the 'Premier' advantage plan yadda yadda. You can use PastDueCustomers as a query source, like this:

var reallyComplicatedQuery = 
from Customer cust in ci.PastDueCustomers ....

So, the next time you go to add additional business logic to your model or you need some utility methods, consider putting them in a partial class that is the same name as your model in the same namespace as your model and you may just find that your ADO.NET EF experience got a little more pleasant.

tags:        
links: digg this    del.icio.us    technorati    reddit

About Kevin Hoffman
Kevin Hoffman, editor-in-chief of SYS-CON's "iPhone Developer's Journal" is one of the most popular "iPhone" and "Silverlight" bloggers on the Net. Kevin has been programming since he was 10 and has written everything from DOS shareware to n-tier, enterprise Web applications in VB, C++, Delphi, and C. He is coauthor of Professional .NET Framework (Wrox Press) and co-author with Robert Foster of Microsoft SharePoint 2007 Development Unleashed. Kevin authors "The .NET Addict's Blog" at ".Net Developer's Journal" (dotnetaddict.dotnetdevelopersjournal.com).

.NET News Desk wrote: So you're building your data-driven application and you've got an ADO.NET Entity Model that represents an abstraction around your database.
read & respond »
MICROSOFT .NET LATEST STORIES
Peer Networking Series - A Closer Look at PNRP vs. Bonjour/ZeroConf
It seems as though whenever I bring up PNRP and its benefits, I am immediately inundated with a list of questions or comments indicating that Microsoft is re-inventing the wheel and that PNRP has already been implemented before in the form of ZeroConf and, more specifically, Apple's im
db4o Open Source Object-Oriented Database Supports LINQ
db4objects has announced that its db4o object database is now optimized for Microsoft's LINQ. With the new support, developers can choose an object-oriented optimized engine without changing the API or compromising performance. db4object's db4o database offers a persistence solution to
Microsoft, Unisys, Yahoo and Vista
Microsoft, which spent $6 billion on aQuantive and was chasing Yahoo for its ads before it came to a dead stop, has been supporting - as in helping write - legislation in New York and Connecticut that would regulate the data that companies like Yahoo and Google collect for targeted adv
AJAX World - Xceed Launches Microsoft Silverlight 2 Control
Xceed launched Xceed Upload for Silverlight, the commercial offering in support of Microsoft's promising new Silverlight technology. The product is available now for purchase or as a fully functional 45-day trial on Xceed's website. Xceed Upload for Silverlight lets developers add uplo
Microsoft To Keynote 4th International Virtualization Conference & Expo
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING NEWS FROM THE WIRES
High Growth Forecasted for the Mobile search 2008-2013: Profiting From Information and Advertising on the Move
Reportlinker.com announces that a new market research report related to the Wireless industr