This project is read-only.

This library aims to fully support Microsoft Sql Server and MySql.

The author does not posses the knowledge or the resources to support every possible DMBS  out there,  so it is considered important that the library is extendable.

The architecture builds upon a provider model that makes it fairly easy to extend the library and add support for just about any DBMS.

Building a custom provider

These are the steps necessary to create a new provider.

Extracting database metadata

The first thing we need to do is to create an implementation of the DbMetadataExtractor class.

This is an abstract class and there are four methods that we need to override.

 

ReadTables() Must return a list of TableInfo instances, one for each table or view in the database.
ReadColumns() Must return a list of ColumnInfo instances, one for each column in the database.
ReadForeignKeys() Must return a list of ForeignKeyInfo instances, one for each foreign key in the database.
ReadPrimaryKeys Must return a list of PrimaryKeyInfo instances, one for each primary key in the database.

 

As we can see from the implementation of SqlMetadataExtractor, this makes use of the Information Schema Views that is available to us in Sql Server.

Note that these views are not available in all database systems. For instance, Oracle does not support this although it is a part of the SQL-92 ANSI standard.

Anyway, most database systems offers a way of reading metadata, you just need to figure out how it works for your DBMS.

So if this was a provider for Oracle, the natural thing would be to name the extractor OracleMetadataExtractor.

Mapping datatypes

The provider needs to know how to map a database datatype to a CLR data type.

We need to implement the ITypeMapper interface to provide this information.

There is only one method that we need to implement.

MapType(string dataType, bool isNullable) Must return a CLR Type based on the type name and its null behavior.

Note that if the database type is nullable and maps to a CLR value type, this method should return its nullable version (e.g Nullable<int>).

Please refer to the SqlTypeMapper implementation for further details.

The Provider Factory

The architecture builds upon a variation of the Abstract Factory Pattern and next we need to create a concrete factory that knows how to create our new provider.

This is done by implementing the IDbMetadataProviderFactory interface.

An example of this can be reviewed by looking at the SqlMetadataProviderFactory.

The factory creates a new instance of the DbMetadataProvider class supplying its two dependencies, the extractor and the type mapper.

Register the new provider factory

There are two ways to register the new provider factory with the DbMetadata library

Using code

DbMetadataProviderFactories.RegisterMetadataProviderFactory(new DummyMetadataProviderFactory());

Using configuration

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="dbMetadata" type ="DbMetadata.Configuration.DbMetadataSettings, DbMetadata"/> 

  </configSections>

  <dbMetadata>

    <providerfactories>

      <providerfactory type ="DbMetadata.Tests.DummyMetadataProviderFactory, DbMetadata.Tests"></providerfactory>

    </providerfactories>   

  </dbMetadata

</configuration>

Last edited Jan 10, 2011 at 2:42 PM by seesharper, version 4

Comments

No comments yet.