Building a Testable Data Access Layer

Slides:



Advertisements
Similar presentations
Svetlin Nakov Director Training and Consulting Activities National Academy for Software Development (NASD) ASP.NET 3.5 New Features.
Advertisements

Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
LINQ 2 SQL By, Shahzad Sarwar.
Unit Testing in SQL Richard Fennell Engineering Director SqlBits 6 th October 2007.
Implementing Tableau Server in an Enterprise Environment
Sofia, Bulgaria | 9-10 October Datasets, Business Objects or Just LINQ Sahil Malik Prerequisites for this presentation: 1).NET 2) Lots.
Connecting to a Database through Programs Database Systems.
Entity Framework Code First Migrations
November 11, MCT, MCITP, MCTS, MCP SharePoint Architect for Planet Technologies. Working with SharePoint since – Administrator / Developer.
Final Presentation WINTER 2009 – SUMMER 2009 PRESENTED BY: George Kour Hany Danial SUPERVISOR: Victor Kulikov Networked Software Systems Laboratory DEPARTMENT.
Simplified Management using the Enterprise Policy Management Framework
Writing Unit Tests with Microsoft Fakes Copyright © Intertech, Inc Slide 1 Writing Unit Tests with Microsoft Fakes.
SharePoint Forms All you ever wanted to know about forms but were afraid to ask.
Oracle SQL Developer Data Modeler 3.0: Technical Overview March 2011.
Jacinto Limjap, Jr. Senior Software Design Engineer, Cormant Technologies Inc. LINQ for NHibernate { Easy OR Mapping with C# and Visual Studio 2008 }
Using.NET Platform Note: Most of the material of these slides have been adapted from Nakov’s excellent overview for.NET framework, MSDN and Wikipedia Muhammad.
.NET 3.5 SP1 New features Enhancements Visual Studio 2008 SP1 New features Enhancements Additional features/enhancements.
Introduction to ASP.NET MVC
Accelerating Development with ECO Tim Jarvis

Creating Dashboards with Performance Point Monitoring & Analytics Tim Toennies Covenant Technology Partners.
Application Generator Merrill Networking Services.
NHibernate Object/Relational Persistence for.NET.
Object Relational Mapping – ORM Entity Framework
Small Business Server and Windows SharePoint Services David Overton
Automated Testing with Sitecore V2
Apache Struts Technology
INTRODUCTION TO ASP.NET MVC AND EXAMPLE WALKTHROUGH RAJAT ARYA EFECS - OIM DAWG – 4/21/2009 ASP.NET MVC.
{ Model View Controller ASP.NET By Scott Crooks & Maggie Wettergreen.
.NET Database Technologies: Open-Source Frameworks.
Computer Monitoring System for EE Faculty By Yaroslav Ross And Denis Zakrevsky Supervisor: Viktor Kulikov.
DEVELOPING APPLICATIONS WITH LINQ 2 SQL Sidar Ok
The Jukebox Orian Paz & Yair Cleper Instructor: Viktor Kulikov Semester: Spring 2009 Final Presentation.
#sqlsatPordenone #sqlsat367 February 28, 2015 Testing your databases Alessandro
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
Who am I? ● Catalin Comanici ● QA for 10 years, doing test automation for about 6 years ● fun guy and rock star wannabe.
By Bob Bunson  Simulation of software development project  Fictitious system from Concept to Code  Oriented around the.
Web Application Architecture: multi-tier (2-tier, 3-tier) & mvc
N-Tier Architecture.
Ori Calvo, 2010 “If people want to have maximum reach across *all* devices then HTML will provide the broadest reach” Scott Guthrie,
Entity Framework Code First End to End
Meir Botner David Ben-David. Project Goal Build a messenger that allows a customer to communicate with a service provider for a fee.
What is Architecture  Architecture is a subjective thing, a shared understanding of a system’s design by the expert developers on a project  In the.
Enticy GROUP THE A Framework for Web and WinForms (Client-Server) Applications “Enterprise Software Architecture”
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Testing Web Services Unit Testing, Data Layer Testing, Web API Controllers Testing, Integration Testing Web Services & Cloud SoftUni Team testing Technical.
Building an Offline Smart Client using Domain-Driven Design Principles Tim McCarthy.
1 Another group of Patterns Architectural Patterns.
Effective Test Driven Database Development Gojko Adzic
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Architecture.
Architectural Patterns Support Lecture. Software Architecture l Architecture is OVERLOADED System architecture Application architecture l Architecture.
Domain and Persistence Patterns. Fundamental Pattern Types Design Patterns Business Logic Patterns.
Oct * Brad Tutterow. VS 2008.NET 3.5LINQ Entity Framework  The ADO.NET Entity Framework is part of Microsoft’s next generation of.NET technologies.
Roy Osherove Sela Group ISerializable.com (blog)
SEA Side – Extreme Programming 1 SEA Side Software Engineering Annotations Architectural Patterns Professor Sara Stoecklin Director of Software Engineering-
ASP.NET MVC An Introduction. What is MVC The Model-View-Controller (MVC) is an architectural pattern separates an application into three main components:
2 Behind every great site, there is great data Eric Nelson Developer Evangelist Microsoft UK
Submitted by: Moran Mishan. Instructed by: Osnat (Ossi) Mokryn, Dr.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Unit testing of the Services Telerik Software Academy Web Services and Cloud.
DAL, Three-tier architecture
Chris Menegay Sr. Consultant TECHSYS Business Solutions
Server Concepts Dr. Charles W. Kann.
Entity Framework By: Casey Griffin.
Unit Testing & Test-Driven Development for Mere Mortals
Unit Testing & Test-Driven Development for Mere Mortals
Lecture 1: Multi-tier Architecture Overview
Unit Testing & Test-Driven Development for Mere Mortals
Developing and testing enterprise Java applications
Presentation transcript:

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.

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

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

what is a “data layer”?

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.

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

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

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.

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* Easy = Easy to build if you’re not careful

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 Microsoft LinqToSQL Entity Framework Open Source nHibernate Commercial Telerik OpenAccess LLBGenPro EntitySpaces

Build data layer with LinqToSql & EF & OpenAccess Demo

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

Add data access pattern to project Demo

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

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

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.

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

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

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

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 DEMO: MOCKING DATABASE

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

hard parts Creating test schema/data Speed DbUnit 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 DEMO: TESTinG with real DB

should you test your DAL?

Q&A anglin@telerik.com @toddanglin telerikwatch.com

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/