Building a Testable Data Access Layer Todd Anglin Chief Evangelist, Telerik Level:  Intermediate
Introductions Todd Anglin Chief Evangelist, Telerik Microsoft MVP ASP Insider President NHDNUG & O’Reilly Author @toddanglin TelerikWatch.com
today’s plan
what is a “data layer”?
Data Layer Responsible for talking to “persistence layer” Presentation Web Desktop Domain Logic (“the code that makes you money”) Data / Model Service Persistence Database Cloud XML Etc.
why build a data layer? what’s the benefit?
importance of data layer Decouple application from persistence = easier maintenance = improved testability = greater reusability
Presentation Web Desktop Domain Logic (“the code that makes you money”) Data Persistence Database Cloud XML Etc. Service
a  good  data layer…
Handles  all  data access Hides implementation Flexible Easy to refactor
a  bad  data layer is…
Does not centralize data access Makes application very dependent on persistent store Easy*
how do we build “data layers”?
By Hand Pros POCO YAGNI No-RTFM Cons Time No FM ORM Pros Time Flexible Cons Learning Limits Trust*
popular .NET ORMs
DEMO Build data layer with LinqToSql & EF & OpenAccess
data layer patterns Domain Driven Design (DDD) Key concepts: Repositories act on model ActiveRecord Key concepts: Model objects act on themselves Data Mapper Key concepts: Objects mapped to tables
DEMO Add data access pattern to project
testing the data layer
two testing camps Concepts: Test against “real” database Use set-up/tear-down code to create test data Good When… You put lots of logic in your database Concepts: Test against “fake” database Isolates your code from database behavior Good When… You want fast unit tests and you put most logic in code A Test Database B Mock Database
picking a camp Mocking Pros Unit test Isolate concerns Fast Cons “ Hides” issues Does not test database logic “ Test” Database Pros Catches  more  issues Familiar Cons Slow Not a “unit” test
testing considerations What is a unit test? What are other types of testing?
UNIT TEST “ Isolated. Repeatable. Fast.” INTEGRATION TEST “ Test interaction between units.” FUNCTIONAL TEST “ Test behavior from user perspective.”
mock testing Goal: Test your business logic B Database Communication (ORM, ADO.NET, etc.) Repository Business Code UI Behaviors Services Database
mocking Stunt doubles for real objects Look the same on the outside Mocking Tools: JustMock (by Telerik) Isolator (by TypeMock) MOQ (OSS) RhinoMocks (OSS)
AAA mocking pattern //Arrange Set-up your test variables and mocks //Act Execute your code like normal //Assert Verify what happened
DEMO: MOCKING DATABASE Testing L2S with Mock Objects
test database Goal: Test your business logic + database behavior A Steps for every test: Create database schema + test data (Optional) Test database setup correctly Execute unit test code Verify database behaved correctly
hard parts Creating test schema/data DbUnit Speed In memory database SQL Lite, SQL CE, etc.
rules for test database tests Prior to running tests, schema should be redeployed to test DB (+ test data) Tests should not change existing data Edits, Deletes should be on records created by test Original data should be read-only Tests should not depend on changes from previous tests
DEMO: TESTING WITH REAL DB Creating integration tests to talk to real database
should you test your DAL?
[email_address] @toddanglin telerikwatch.com Q & A
Links 4GuysFromRolla on Testing DAL (2005) http:// aspnet.4guysfromrolla.com/articles/040605-1.2.aspx Unit Testing the DAL (Java, but great discussion of DAL data testing) http:// www.buunguyen.net/blog/unit-testing-the-data-access-layer.html Roy Osherove on using Mocks for DAL testing http://weblogs.asp.net/rosherove/archive/2003/09/30/29734.aspx SQL Lite project page http:// www.sqlite.org/docs.html System.Data.SQLite:  http://sqlite.phxsoftware.com / http ://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins / http://hendryluk.wordpress.com/2008/10/07/data-access-test-with-sqlite/ DbUnit.NET (last updated 2006 – still alpha) http:// dbunit-net.sourceforge.net/GettingStarted.html SQL Server Compact 4 CTP1 (2010) http:// www.microsoft.com/downloads/details.aspx?FamilyID=0d2357ea-324f-46fd-88fc-7364c80e4fdb&displaylang=en http:// robtiffany.com/sql-server-compact/here-comes-sql-server-compact-4-0 SQL Script to clear all tables in database http:// www.smallworkarounds.net/2009/02/script-to-delete-all-data-from-sql.html Microsoft.SqlServer.Management.Smo primer http:// davidhayden.com/blog/dave/archive/2006/01/27/2774.aspx http:// social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/43e8bc3a-1132-453b-b950-09427e970f31 Multi-tier L2S architecture ideas http://blog.stevensanderson.com/2007/11/29/linq-to-sql-the-multi-tier-story/

Building a Testable Data Access Layer

  • 1.
    Building a TestableData Access Layer Todd Anglin Chief Evangelist, Telerik Level: Intermediate
  • 2.
    Introductions Todd AnglinChief Evangelist, Telerik Microsoft MVP ASP Insider President NHDNUG & O’Reilly Author @toddanglin TelerikWatch.com
  • 3.
  • 4.
    what is a“data layer”?
  • 5.
    Data Layer Responsiblefor talking to “persistence layer” Presentation Web Desktop Domain Logic (“the code that makes you money”) Data / Model Service Persistence Database Cloud XML Etc.
  • 6.
    why build adata layer? what’s the benefit?
  • 7.
    importance of datalayer Decouple application from persistence = easier maintenance = improved testability = greater reusability
  • 8.
    Presentation Web DesktopDomain Logic (“the code that makes you money”) Data Persistence Database Cloud XML Etc. Service
  • 9.
    a good data layer…
  • 10.
    Handles all data access Hides implementation Flexible Easy to refactor
  • 11.
    a bad data layer is…
  • 12.
    Does not centralizedata access Makes application very dependent on persistent store Easy*
  • 13.
    how do webuild “data layers”?
  • 14.
    By Hand ProsPOCO YAGNI No-RTFM Cons Time No FM ORM Pros Time Flexible Cons Learning Limits Trust*
  • 15.
  • 16.
    DEMO Build datalayer with LinqToSql & EF & OpenAccess
  • 17.
    data layer patternsDomain Driven Design (DDD) Key concepts: Repositories act on model ActiveRecord Key concepts: Model objects act on themselves Data Mapper Key concepts: Objects mapped to tables
  • 18.
    DEMO Add dataaccess pattern to project
  • 19.
  • 20.
    two testing campsConcepts: Test against “real” database Use set-up/tear-down code to create test data Good When… You put lots of logic in your database Concepts: Test against “fake” database Isolates your code from database behavior Good When… You want fast unit tests and you put most logic in code A Test Database B Mock Database
  • 21.
    picking a campMocking Pros Unit test Isolate concerns Fast Cons “ Hides” issues Does not test database logic “ Test” Database Pros Catches more issues Familiar Cons Slow Not a “unit” test
  • 22.
    testing considerations Whatis a unit test? What are other types of testing?
  • 23.
    UNIT TEST “Isolated. Repeatable. Fast.” INTEGRATION TEST “ Test interaction between units.” FUNCTIONAL TEST “ Test behavior from user perspective.”
  • 24.
    mock testing Goal:Test your business logic B Database Communication (ORM, ADO.NET, etc.) Repository Business Code UI Behaviors Services Database
  • 25.
    mocking Stunt doublesfor real objects Look the same on the outside Mocking Tools: JustMock (by Telerik) Isolator (by TypeMock) MOQ (OSS) RhinoMocks (OSS)
  • 26.
    AAA mocking pattern//Arrange Set-up your test variables and mocks //Act Execute your code like normal //Assert Verify what happened
  • 27.
    DEMO: MOCKING DATABASETesting L2S with Mock Objects
  • 28.
    test database Goal:Test your business logic + database behavior A Steps for every test: Create database schema + test data (Optional) Test database setup correctly Execute unit test code Verify database behaved correctly
  • 29.
    hard parts Creatingtest schema/data DbUnit Speed In memory database SQL Lite, SQL CE, etc.
  • 30.
    rules for testdatabase tests Prior to running tests, schema should be redeployed to test DB (+ test data) Tests should not change existing data Edits, Deletes should be on records created by test Original data should be read-only Tests should not depend on changes from previous tests
  • 31.
    DEMO: TESTING WITHREAL DB Creating integration tests to talk to real database
  • 32.
    should you testyour DAL?
  • 33.
  • 34.
    Links 4GuysFromRolla onTesting DAL (2005) http:// aspnet.4guysfromrolla.com/articles/040605-1.2.aspx Unit Testing the DAL (Java, but great discussion of DAL data testing) http:// www.buunguyen.net/blog/unit-testing-the-data-access-layer.html Roy Osherove on using Mocks for DAL testing http://weblogs.asp.net/rosherove/archive/2003/09/30/29734.aspx SQL Lite project page http:// www.sqlite.org/docs.html System.Data.SQLite: http://sqlite.phxsoftware.com / http ://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins / http://hendryluk.wordpress.com/2008/10/07/data-access-test-with-sqlite/ DbUnit.NET (last updated 2006 – still alpha) http:// dbunit-net.sourceforge.net/GettingStarted.html SQL Server Compact 4 CTP1 (2010) http:// www.microsoft.com/downloads/details.aspx?FamilyID=0d2357ea-324f-46fd-88fc-7364c80e4fdb&displaylang=en http:// robtiffany.com/sql-server-compact/here-comes-sql-server-compact-4-0 SQL Script to clear all tables in database http:// www.smallworkarounds.net/2009/02/script-to-delete-all-data-from-sql.html Microsoft.SqlServer.Management.Smo primer http:// davidhayden.com/blog/dave/archive/2006/01/27/2774.aspx http:// social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/43e8bc3a-1132-453b-b950-09427e970f31 Multi-tier L2S architecture ideas http://blog.stevensanderson.com/2007/11/29/linq-to-sql-the-multi-tier-story/

Editor's Notes

  • #2 Visual Studio Live Orlando 2010MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  • #13 Easy = Easy to build if you’re not careful
  • #20 Everything is familiar up to this point. Now the dreaded step may of us avoid: Creating tests for the data layer
  • #22 Hides issues example = concurrency, auto-gen DB values, etc.