Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API Gert E.R.

Similar presentations


Presentation on theme: "DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API Gert E.R."— Presentation transcript:

1 DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API Gert E.R. Drapers Architect Microsoft Corp.

2 Agenda ADO.NET Provider Architecture ADO.NET Provider Architecture Purpose of a Data Factory Purpose of a Data Factory Abstracting Statements Abstracting Statements Using Caching Using Caching

3 ADO.NET Concepts ADO.NET Architecture ADO.NET Architecture – Split into DataSet and.NET Data Providers – Exposes two distinct programming models Disconnected data Disconnected data Streamed data Streamed data – Use of these models affects your design decisions

4 .NET Data Provider IDataReader ExceptionError CommandBuilder IDbCommand IDataParameter IDbConnection Data Source IDbTransaction IDbDataAdapterDbDataAdapterIDataAdapterDataAdapter DataSet ADO.NET Architecture

5 SqlClient.NET Data Provider SqlDataReader DataSet SqlExceptionSqlError SqlCommandBuilder SqlCommand SqlParameter SqlConnection SQLServer SqlTransaction SqlDataAdapter ADO.NET Architecture

6 ADO.NET In Context Assume a multi-tier architecture Assume a multi-tier architecture – Separation of UI from data access code Role based development (data access specialist) Role based development (data access specialist) – Encapsulation of data access code into distinct classes Reusability and maintainability Reusability and maintainability Abstraction of the data source Abstraction of the data source – Data Access classes have both an external and internal view

7 Data Access Architecture External View External View – You choose how to represent the data Data Services (black box) Client (business or presentation services) ? = o.GetTitles(isbn) return ? DataSet Data Reader Custom Object

8 Data Access Architecture Internal View Internal View – Use one of these choices inside your data access classes Data Services Method Invoked from Client.NET Data Provider GetTitlesMethodBody Direct? ProviderFactory ? Data Factory DataSet Data readers ? Caching, other objects DataSet DataReaderCustomObject

9 demo demo Provider Factory

10 Purpose Of A Data Factory Reduce the amount of code you have to write Reduce the amount of code you have to write – By creating command objects and populating parameters automatically Provider Independence Provider Independence – Not locked into one.NET Data Provider Database Independence Database Independence – Target application for SQL Server or Oracle without changing any code! Performance Performance

11 Data Factory API CacheFilePath CacheFilePath Connection Connection Provider Provider UseCache UseCache BeginTransaction BeginTransaction CreateDataAdapter CreateDataAdapter CreateSqlFiles CreateSqlFiles CreateSqlFile CreateSqlFile ExecuteDataReader ExecuteDataReader ExecuteNonQuery ExecuteNonQuery ExecuteScalar ExecuteScalar ExecuteSqlXmlReader ExecuteSqlXmlReader GetDataSet GetDataSet GetDataTable GetDataTable GetProviders GetProviders GetXsltResults GetXsltResults RemoveStatement RemoveStatement SyncDataSet SyncDataSet SqlFilesCreated (event) SqlFilesCreated (event)

12 Data Factory Features Abstracts providers Abstracts providers – Hardcoded SqlClient and OleDb but configurable through DataFactory.config file Database Independent Database Independent – Uses statement files (XML) that abstract the SQL Caches Command Objects Caches Command Objects – Uses a synchronized Hashtable and relies on ICloneable interface

13 Provider Abstraction Relies on the Provider property Relies on the Provider property Uses System.Reflection to dynamically create the appropriate objects Uses System.Reflection to dynamically create the appropriate objects Can be extended for custom providers Can be extended for custom providers

14 demo demo Provider Abstraction

15 Database Independence Relies on statement files and two internal structures Relies on statement files and two internal structures – Synchronized provider-specific Hashtable for caching usp_GetTitles usp_GetTitles

16 demo demo Using Statement Files And Caching

17 Statement Files Can be created automatically using the CreateSqlFile and CreateSqlFiles shared methods Can be created automatically using the CreateSqlFile and CreateSqlFiles shared methods – Based on a procedure prefix – Works for SQL Server Uses sp_procedure_params_rowset Uses sp_procedure_params_rowset – CreateSqlFiles uses asynchronous delegate to run on a background thread

18 demo demo Creating Statement Files

19 Statement Flow Using a shared Hashtable Using a shared Hashtable Is statement in the cache? Load and parse statement file Pull out the command object and clone it Populate the parameters Return Command Execute a statement Create the command object with parameters No Yes

20 Using The Data Factory Public Function GetTitles(ByVal author As String, _ ByVal title As String) As DataSet dim cn As New SqlConnection(_connect) Dim daSql As SqlDataAdapter Dim parmSql As SqlParameter Dim ds As New DataSet() Try daSql = New SqlDataAdapter("usp_GetTitle", cn) daSql.SelectCommand.CommandType = CommandType.StoredProcedure parmSql = daSql.SelectCommand.Parameters.Add( _ New SqlParameter("@author", SqlDbType.varchar, 50)) parmSql.Value = author parmSql = daSql.SelectCommand.Parameters.Add( _ New SqlParameter("@title", SqlDbType.varchar, 200)) parmSql.Value = title daSql.Fill(ds) Return ds Catch e As SqlException ‘ Handle Error End Try End Function Try daSql = New SqlDataAdapter("usp_GetTitle", cn) daSql.SelectCommand.CommandType = CommandType.StoredProcedure parmSql = daSql.SelectCommand.Parameters.Add( _ New SqlParameter("@author", SqlDbType.varchar, 50)) parmSql.Value = author parmSql = daSql.SelectCommand.Parameters.Add( _ New SqlParameter("@title", SqlDbType.varchar, 200)) parmSql.Value = title daSql.Fill(ds) Return ds Catch e As SqlException ‘ Handle Error End Try End Function Client code before and after Client code before and after – Overall 30-40% reduction in code

21 Public Function GetTitles(ByVal author As String, _ ByVal title As String) As DataSet Dim parms As New HybridDictionary() Dim ds As New DataSet() Try parms.Add(“author”, author) parms.add(“title”, title) ds = _df.GetDataSet(“GetTitles”,parms) Return ds Catch e As DataFactoryException ‘ Handle Error End Try End Function Try parms.Add(“author”, author) parms.add(“title”, title) ds = _df.GetDataSet(“GetTitles”,parms) Return ds Catch e As DataFactoryException ‘ Handle Error End Try End Function Client code before and after Client code before and after – Overall 30-40% reduction in code Using The Data Factory

22 Performance and Futures Results from Application Center Test Results from Application Center Test – In an ASP.NET application the site performed slightly better using the DataFactory – Could be improved by caching the creation of the type objects

23 Questions?

24 Resources from Microsoft Press For more information please visit the TechEd Bookshop. www.microsoft.com/mspress ADO.NET

25 Don’t forget to complete the on-line Session Feedback form on the Attendee Web site https://web.mseventseurope.com/teched/ https://web.mseventseurope.com/teched/

26


Download ppt "DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API DAT430 Extending Microsoft ® ADO.NET Building a Custom Data Factory API Gert E.R."

Similar presentations


Ads by Google