Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tiger Computer Services Ltd Using Interfaces in.NET Simplifying access to multiple DB providers in.NET Liam

Similar presentations


Presentation on theme: "Tiger Computer Services Ltd Using Interfaces in.NET Simplifying access to multiple DB providers in.NET Liam"— Presentation transcript:

1 http://www.tigernews.co.uk Tiger Computer Services Ltd Using Interfaces in.NET Simplifying access to multiple DB providers in.NET Liam Westleyliam.westley@tigernews.co.uk DeveloperDeveloperDeveloper! - 14 th May 2005

2 http://www.tigernews.co.uk Contents Quick overview of interfaces –What is an interface? –When and how to use an interface? Using interfaces for generic access to different DB Providers –Abstracting the differences –Creating and implementing the interface –MP3 library code walkthrough for SQL Server 2000 and mySQL 4.1

3 http://www.tigernews.co.uk Interfaces vs Inheritance? Inheritance –inheritance defines an ‘is a’ relationship –.NET only supports single inheritance –if a class does not override a member, the base class implementation is used Interfaces –interfaces define an ‘act as’ relationship –a class can implement multiple interfaces Note: Visual Basic 6.0 supported Interfaces with the Implements keyword but it involved ‘casting’ of objects to the required interface

4 http://www.tigernews.co.uk What is an Interface? Defines a set of properties, indexers, events and methods Contains no implementation code Other classes, which implement the interface, provide the implementation of the interface members Every member has to be implemented The interface does not define how the members should be implemented

5 http://www.tigernews.co.uk When to use an Interface? For an ‘acts as’ relationship To enforce a set of rules for a class –application plug-ins –to create a ‘lowest common denominator’ To support multi-‘platform’ solutions –client program uses the interface to access majority of functionality –each ‘platform’ has a class which implements the interface –Platforms include Windows App, ASP.NET,.NET Compact Framework, or different DB providers

6 http://www.tigernews.co.uk Creating an interface – VB.NET Public Interface IHelloWorld Function HelloWorld(ByVal myName As String) As String End Interface Public Class English Implements IHelloWorld Public Function HelloWorld(ByVal myName As String) _ As String Implements IHelloWorld.HelloWorld HelloWorld = "Hello " + myName + "!" End Function End Class Interface (IHelloWorld.vb) Implementation (English.vb) Public Class Francais Implements IHelloWorld Public Function HelloWorld(ByVal myName As String) _ As String Implements IHelloWorld.HelloWorld HelloWorld = “Bonjour " + myName + "!" End Function End Class Implementation (Francais.vb)

7 http://www.tigernews.co.uk Using that interface – VB.NET Private Sub btnSayHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnSayHello.Click Dim hw As IHelloWorld If (rbtnEnglish.Checked) Then hw = New English Else hw = New Francais End If MsgBox(hw.HelloWorld(txtName.Text)) End Sub Windows Form application

8 http://www.tigernews.co.uk Creating an interface – C# public interface IHelloWorld { string HelloWorld(string myName); } public class English : IHelloWorld { public string HelloWorld(string myName) { return "Hello " + myName + "!"; } Interface (IHelloWorld.cs) Implementation (English.cs) public class Francais : IHelloWorld { public string HelloWorld(string myName) { return “Bonjour " + myName + "!"; } Implementation (Francais.cs)

9 http://www.tigernews.co.uk Using that interface – C# private void btnSayHello_Click(object sender, System.EventArgs e) { IHelloWorld hw; if (rbtnEnglish.Checked) { hw = new English(); } else { hw = new Francais(); } MessageBox.Show(hw.HelloWorld(txtName.Text)); } Windows Form application

10 http://www.tigernews.co.uk Real World – Generic DB Providers First some facts ? –ADO ‘classic’ or ADOc provided a generic data access model –ADO.NET is not generic –we have namespaces for each DB provider  i.e. SQLConnection, OracleConnection etc.. Not true !! –Microsoft provided generic database access out of the box –Look in the MSDN Library carefully, i.e. IDBConnection, IDBCommand etc..

11 http://www.tigernews.co.uk How do we use these interfaces? Examine the interfaces within the System.Data namespace, –IDBConnection, IDBCommand, IDataReader, IDBTransaction etc.. Ensure the business layer members only utilise interface references public void GetItem(IDBConnection openDB) Create DB provider specific variables within the client, and pass these as interfaces to the business layer

12 http://www.tigernews.co.uk.. actually, this is not a real generic solution Interfaces are not enough, there are SQL syntax issues as well What does ‘generic’ really mean –Safely format data used in SQL statements –‘Format’ field and table names to allow reserved names (or spaces – urgh!) –Map.NET data types to different DB provider data types –Prevent provider specific properties being used (HasRows property)

13 http://www.tigernews.co.uk Quiz time SQL 2000 vs MySql 4.1

14 http://www.tigernews.co.uk … and.NET data connectors differ too Can only use methods and properties from System.Data interfaces –HasRows property not in base interfaces (absent in SQL Server for CE 2.0) –ExecuteXmlReader is only in SqlClient Some methods are not supported –MySql.NET connector doesn’t support the Cancel method on a IDBCommand –Auto incrementing identity fields are not always returned as System.Int32

15 http://www.tigernews.co.uk What do we lose ? For code to work with all databases we have to give up some stuff –Stored procedures are not available in MySql 4.1 (or MS Access, or SQL CE) –Generic data types, no UDTs –All commands must be raw SQL statements designed to work with all databases –Code can become less readable Not for everyone – losing SQL Server 2000 features may not be appropriate

16 http://www.tigernews.co.uk What could we gain ? Single code logic handling database operations in the business layer Multi platform deployment –Intranet might could use SQL Server, but a public web site implementation could use MySql Higher performance with native data connectors, rather than OLEDB connector Robust SQL, if you are methodical –LIKE handles quotes and wildcards –No UK/US Date format issues ever again –NULL replacement handled elegantly –Prevent SQL injection attacks

17 http://www.tigernews.co.uk MP3Library – ASP.NET application

18 http://www.tigernews.co.uk Only a starting point View this as a base –more reliable code –more secure code –target multiple platforms; SQL Server 2000, MySql 4.1 Oracle, IBM DB2, SQL Server CE, Microsoft Access Next step –create generic database schemas –automated code creation for business logic, and even web services

19 http://www.tigernews.co.uk Bengal RapidDB Origins –concept of database compatibility between SQL 2000, SQL CE and Microsoft Access –mobile data connectivity via web services, not via standard replication –automated generation of code libraries and web services Key features –lowest common denominator data types –code suitable for.NET CF

20 http://www.tigernews.co.uk Bengal RapidDB

21 http://www.tigernews.co.uk Thank you Any questions?


Download ppt "Tiger Computer Services Ltd Using Interfaces in.NET Simplifying access to multiple DB providers in.NET Liam"

Similar presentations


Ads by Google