Language preference:

Phase 1 / Introducing Data Access Objects

A Data Access Object (“DAO”) is a class that provides the CRUD (“Create, Read, Update, Delete”) support for a specific Entity class. It resides in the Business Logic Layer and is yours to maintain and modify. It’s CRUD commands interact with the Data Access Layer, using whatever technologies the DAL demands. For example, to apply an update, a DAO that works with Entity Framework would call the SaveChanges() method on the ObjectContext object while with LINQ to SQL, it would call SubmitChanges() on the DataContext object.

BLD supplies a rich set of Data Access Object classes called BLD DataAccessObjects. While recommended, you can use your own Data Access Objects, so long as your write code for the user interface layer to communicate with it.

BLD DataAccessObjects provide base classes for each Data Access Layer, such as LinqToSQLEntityDAO for Linq to SQL, EFEntityDAO for Entity Framework, and ADOEntityDAO for ADO.NET. (Create your own EntityDAO base class for other Data Access Layers.) They all have default code to handle Update, Insert, and Delete actions. They also have predefined queries for select one record, select all records, and select by filter objects (called EntityFilters).

You must subclass from one of these for each Entity class, overriding any of the default methods for Update, Insert and Delete, and adding methods for custom queries

In this example, ProductEntityDAO is using the Entity Framework through the EFEntityDAO class. It introduces a new query, SelectPriceRange().

public class ProductEntityDAO : EFEntityDAO<Product>
{
  public IEnumerable<Product> SelectPriceRange(params)
  { 
   // code to run the query
  }
}

As you can see, your EntityDAO subclass can be pretty simple. The base class does most of the work. Even the code within SelectPriceRange() is simple, because after creating your filters or an actual query statement (in LINQ or SQL), you pass it to the ExecuteSelectCommand() method of EntityDAO, which handles caching, paging, and of course, communicating with the Data Access Layer to process your query.

public IEnumerable<Product> SelectPriceRange(
  decimal startPrice, decimal endPrice, SelectArgs selectArgs)
{
  selectArgs.EntityFilters.Add(new RangeEntityFilter("UnitPrice", startPrice, endPrice));
  return ExecuteSelectCommand(selectArgs);
}

We'll take a deeper look into BLD DataAccessObjects shortly.

The next topic introduces a class that identifies your database itself.