| By Kevin Hoffman | Article Rating: |
|
| March 29, 2008 05:30 AM EDT | Reads: |
6,303 |
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: adonet entity framework adoef partialclasses
links: digg this del.icio.us technorati reddit
Published March 29, 2008 Reads 6,303
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Kevin Hoffman
Kevin Hoffman, editor-in-chief of SYS-CON's iPhone Developer's Journal, 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. Hoffman is coauthor of Professional .NET Framework (Wrox Press) and co-author with Robert Foster of Microsoft SharePoint 2007 Development Unleashed. He authors The .NET Addict's Blog at .NET Developer's Journal.
![]() |
.NET News Desk 03/26/08 10:01:13 AM EDT | |||
So you're building your data-driven application and you've got an ADO.NET Entity Model that represents an abstraction around your database. |
||||
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications





























