The code

The code consists of 4 projects; GenericDataLayer, which contains the reusable code and 3 demo/test projects. There is also a folder containing the T4 templates discussed in the introduction to the code.

GenericDataLayer

This project contains all of the code you need to use this solution, it can be compiled and referenced in your projects or run as a project within your solutions.
It is made up of:
  • GenericRepository class - the main class which implements the generic CRUD methods as discussed above
  • GenericObjectDataSource - the custom ObjectDataSource to use in your aspx pages as discussed above. This control also uses the classes GenericObjectDataSourceView and ExposedSr
  • FileLogger and DebuggerWriter - classes for logging to text files and System.Debug.Out, see Logging section
  • GenericDataSection - a class to handle configuration data
  • Dynamic - some code to help in the sorting methods

NorthWindDataLayer

This project is an example of how to use the code with your database (using Microsoft's NorthWind database), it references the GenericRepository project and contains all the project specific database classes that your application needs to work with. These are:
  • Linq to Sql dbml file - In this case pointing to the NorthWind database
  • Partial Entity classes which implements a custom data validation rules (see section on Error Handling) - In this project there is just one class Product which implements one rule
  • Concrete Repository classes which inherit from GenericRepository and implement custom data access code for specific database entities - This project has two, ProductRepository and CategoryRepository

A test web project

This project has one page with a GridView and a FormView which use the data layer via GenericObjectDataSources to list all the products in the Northwind database, allow editing and deletion of those projects, and insert new products via the FormView.

A console app

To test functionality outside the context of a web application

To run the example project:

  • Check out the code or download and unzip the release
  • Open the solution in VS2008
  • Change the connection string in the web.config to point to a Northwind database
  • Run the web project

To use the code in your own projects

  • Create a class library to contain your Linq to Sql classes, partial entities and concrete repositories
  • Run the T4 template as described above to create concrete repository classes
  • Reference the GenericDataLayer project (either compiled or within your solution)
  • Optionally define any new entity specific data access operations in your entity repository classes
  • Make use of the GenericObjectDataSource to get easy inserts, updates, deletes, sorting and paging

Last edited Apr 14, 2009 at 1:10 PM by rbates, version 3

Comments

No comments yet.