Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building a Testable Data Access Layer

Similar presentations


Presentation on theme: "Building a Testable Data Access Layer"— Presentation transcript:

1 Building a Testable Data Access Layer
Visual Studio Live Orlando 2010MGB 2003 Building a Testable Data Access Layer Todd Anglin Chief Evangelist, Telerik Level: Intermediate © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

2 Introductions Chief Evangelist, Telerik Todd Anglin Microsoft MVP
ASP Insider President NHDNUG & O’Reilly Author TelerikWatch.com @toddanglin

3 Testing Data Operations
Define “Data Layer” Data Layer Options Build Data Layer Testing Data Operations today’s plan

4 what is a “data layer”?

5 Domain Logic (“the code that makes you money”)
Data Layer Responsible for talking to “persistence layer” Presentation Web Desktop Service Domain Logic (“the code that makes you money”) Data / Model Persistence Database Cloud XML Etc.

6 why build a data layer? what’s the benefit?

7 importance of data layer
Decouple application from persistence = easier maintenance = improved testability = greater reusability

8 Domain Logic (“the code that makes you money”)
Presentation Web Desktop Service Domain Logic (“the code that makes you money”) Data Persistence Database Cloud XML Etc.

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 centralize data access
Makes application very dependent on persistent store Easy* Easy = Easy to build if you’re not careful

13 how do we build “data layers”?

14 By Hand Pros POCO YAGNI No-RTFM Cons Time No FM ORM Pros Time Flexible Cons Learning Limits Trust*

15 popular .NET ORMs Microsoft LinqToSQL Entity Framework Open Source
nHibernate Commercial Telerik OpenAccess LLBGenPro EntitySpaces

16 Build data layer with LinqToSql & EF & OpenAccess
Demo

17 data layer patterns Domain Driven Design (DDD) ActiveRecord
Key concepts: Repositories act on model ActiveRecord Key concepts: Model objects act on themselves Data Mapper Key concepts: Objects mapped to tables

18 Add data access pattern to project
Demo

19 testing the data layer Everything is familiar up to this point.
Now the dreaded step may of us avoid: Creating tests for the data layer

20 A B two testing camps Test Database Mock Database Concepts: 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

21 picking a camp “Test” Database Mocking Pros Pros Cons Cons 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 Hides issues example = concurrency, auto-gen DB values, etc.

22 testing considerations
What is a unit test? What are other types of testing?

23 UNIT TEST INTEGRATION TEST FUNCTIONAL TEST
“Isolated. Repeatable. Fast.” INTEGRATION TEST “Test interaction between units.” FUNCTIONAL TEST “Test behavior from user perspective.”

24 Database Communication (ORM, ADO.NET, etc.)
mock testing Goal: Test your business logic UI Behaviors Services Business Code Repository Database Communication (ORM, ADO.NET, etc.) Database

25 mocking Stunt doubles for 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 DATABASE
Testing L2S with Mock Objects DEMO: MOCKING DATABASE

28 A Steps for every test: test database
Goal: Test your business logic + database behavior 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 Creating test schema/data Speed DbUnit In memory database
SQL Lite, SQL CE, etc.

30 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

31 DEMO: TESTinG with real DB
Creating integration tests to talk to real database DEMO: TESTinG with real DB

32 should you test your DAL?

33 Q&A @toddanglin telerikwatch.com

34 Links 4GuysFromRolla on Testing DAL (2005) Unit Testing the DAL (Java, but great discussion of DAL data testing) Roy Osherove on using Mocks for DAL testing SQL Lite project page System.Data.SQLite: DbUnit.NET (last updated 2006 – still alpha) SQL Server Compact 4 CTP1 (2010) SQL Script to clear all tables in database Microsoft.SqlServer.Management.Smo primer Multi-tier L2S architecture ideas


Download ppt "Building a Testable Data Access Layer"

Similar presentations


Ads by Google