Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing code to write your Data Services Layer Andrew Novick December 6, 2001.

Similar presentations


Presentation on theme: "Writing code to write your Data Services Layer Andrew Novick December 6, 2001."— Presentation transcript:

1 Writing code to write your Data Services Layer Andrew Novick December 6, 2001

2 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Agenda The Task: Creating a Data Services Layer Stored Procedure Based DSL Writing the code that writes the code Getting Metadata from SQL Server

3 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Task: Create a Data Services Layer Write a Data Services Layer for a medium to large database that is rapidly evolving Use the fastest possible ADO techniques

4 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group The N-Tier Model User Interface Layer Business Layer Data Services Layer Database

5 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Data Services Layer Responsible for all I/O with database Holds all the SQL Uses Stored Procedures for routine: Insert Update Delete Select by Key

6 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Why Stored Procedures for I/O Reduced round-trips to the database. About 1/4 th the trips used by ADO Recordsets Reduced client CPU and Memory

7 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Resource ADO Examples and Best Practices William R. Vaughn Apress ISBN 1-893115-16-X

8 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Alternative ways of getting there Buy a product Lockwood Tech – ProcBlaster OM Tool Carl Franklins Code Build VBPJ Article

9 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group VBPJ June 2001 Automate Writing Stored Procedures Use SQL Server's Distributed Management Objects to generate standardized stored procedures. by David Rabb URL: http://www.devx.com/premier/mgznarch/vbpj/2001/06jun01/dd010 6/dd0601.asp T ry not to give your users direct access to your database tablesthey can poke around in your database and cause all kinds of trouble. Microsoft SQL Server provides one of the best methods for isolating users from your tables: stored procedures. However, unless you own a middle-tier database-modeling tool, you've probably been building and maintaining your stored procedures by hand. I'll show you how to use the metadata stored in SQL Server to create and maintain a set of standardized stored procedures for any SQL Server database (see Figure 1). In this column's sample project, I'll show you how to create four procedures for each user table in the Pubs database: Select, Insert, Update, and Delete (download the code project). Select contains one parameter for each member of the table's primary key, and a select statement. It returns all columns for a single row in the table.download the code project URL: http://www.devx.com/premier/mgznarch/vbpj/2001/06jun01/dd0106/dd0601.asp

10 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Why Build Total control Interfaces the way you want them Naming Conventions the way you want them. Error handling the way you want it. Products require extensive customization and script writing. Might as well write it in VB

11 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Using Stored Procedures for IUSD Insert Update Select Delete

12 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Sample Stored Procedure CREATE PROCEDURE [dp_titles_ins] @title_idvarchar(6), @titlevarchar(80), @Booktypechar(12) = NULL OUTPUT, @pub_idchar(4) = NULL, @pricemoney = NULL, @advancemoney = NULL, @royaltyint = NULL, @ytd_salesint = NULL, @Booknotesvarchar(200) = NULL, @pubdatedatetime = NULL OUTPUT AS

13 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Sample Stored Procedure 2 IF @Booktype Is Null SET @Booktype = ('UNDECIDED') IF @pubdate Is Null SET @pubdate = (getdate()) INSERT INTO [titles] WITH (ROWLOCK) ( [title_id], [title], [Booktype], [pub_id], [price], [advance], [royalty], [ytd_sales], [Booknotes], [pubdate]) Values (@title_id, @title, @Booktype, @pub_id, @price, @advance, @royalty, @ytd_sales, @Booknotes, @pubdate) SELECT @Booktype = [Booktype], @pubdate = [pubdate] FROM [titles] WHERE [title_id] = @title_id

14 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group What do the VB Classes Look Like? To long to print here…. Multiple interfaces to the data.

15 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Some Caveats Naming convention for SQL Objects required 30 characters name limit No spaces in names No use of VB Reserved words Special Filed

16 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group What sort of Interface would you like? Properties Compact Load, Add, Update Irec Browse

17 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Property based Interface Property Get and Let pairs for each variable oTable.Field1 = a new value oTable.Field1 = another value ….. oTable.Update

18 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Compact Functions Load, Add, Update send all properties. Best when method calls cross machine or context boundaries. oTable.Add (my field1val, myField2Val myField3Val…..)

19 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group IRec Interface Generic, works with all tables. Dim oMyTable as cMyTable Dim oRec as IRec Set oRec = oMyTable nFieldIndex = oRec.FieldIdx(Name) myVariable = oRec.FieldValue(nFieldIndex)

20 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Browse Writes the SQL to do standard browse access to tables. Keeps SQL out of the UI and Business layers.

21 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Take a look at Views Similar to tables Usually not updateable

22 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Classes Stored Procedures 3 Types of stored procedures Does not return a record set Returns a record set Returns an XML stream Never updateable

23 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Does not return a record set oCMD.Execute RecordsAffected:=m_nRAd, Options:=adExecuteNoRecords

24 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Returns XML With oCMD.Dialect = "{5D531CB2-E6ED-11D2-B252-00C04F681B71}" Set.CommandStream = oStream.Properties("Output Stream") = oResultStream.Execute,, adExecuteStream m_sXML = oResultStream.ReadText() End With

25 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group 5 Minutes This is the true power of having a custom application Would work even better as a VB Add-In

26 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Alternatives for getting Schema Information SQL-DMO ADOX INFORMATION-SCHEMAs

27 Andrew Novick anovick@world.std.comDecember 6, 2001VB Pro User Group Real World Results 8000 Lines of Code in AppGenerator Writes 150,000 Lines of DSL Code 80 Hours vs. about 400 Hours Change in effort level takes away an important disincentive for using stored procedures.


Download ppt "Writing code to write your Data Services Layer Andrew Novick December 6, 2001."

Similar presentations


Ads by Google