Skip Navigation LinksBase4.NET : Quickstarts : Cool Stuff : Wrap a legacy database?

Skip Navigation Links.

Wrap a legacy or third party database?

Base4 is ideal for taking a legacy or third party database and re-exposing it with a simple and consistent API. To do this you need to create a legacy schema, which simply involves registering all the types from your old database with Base4.

 

To illustrate how this works imagine you have a very simple database called [bugs] database. In that database you have a number of tables but only 2 that are of interest at the moment, namely the [bugs].[dbo].[bug] table and the [bugs].[dbo].[person] table. On the same SQL server you also have a Base4 database called [base4_default].

 

So logically things look like this: 

 

You need to start by registering a Schema with Base4 that includes 2 Types one for the [bugs].[dbo].[bug] table and one for the [bugs].[dbo].[person] table.

 

This is done like this:

 

<Schema Name="Bugs.dll" uri="http://www.base4.net/Schemas/Bugs.xml" version="1">

<!-- A list of all the other schemas that this depends on or references -->

<References>

      <!-- References the builtin Base4.Storage.dll schema-->

      <Reference Name="Base4.Storage.dll" dirty="True" inDatabase="False"

                  uri="http://www.base4.net/schemas/Base4.Storage.xml" version="1" />

</References>    

 

<Types>

<!-- Register the [bugs].[dbo].[Person] table with Base4 -->

<Type FullName="Bugs.Person" source="[bugs].[dbo].[Person]" baseName="Base4.Storage.ItemImpl" keyProperty="ID" version="1" readOnly="True" >

      <Property name="ID" typeName="System.Int32" nullable="False" unique="True" source="iID"/>

      <Property name="Firstname" typeName="System.String" length="100" source="sFirstname"/>

      <Property name="Surname" typeName="System.String" length="100" source="sSurname"/>

</Type>

<!-- Register the [bugs].[dbo].[Bug] table with Base4 -->        

<Type FullName="Bugs.Bug" source="[bugs].[dbo].[Bug]" baseName="Base4.Storage.ItemImpl" keyProperty="ID" version="1" readOnly="True" >

      <Property name="ID" typeName="System.Int32" nullable="False" unique="True" source="ixBug"/>

      <Property name="Open" typeName="System.Int32" source="fOpen" nullable="True"/>

      <Property name="Opened" typeName="System.DateTime" source="dtOpened" nullable="True"/>

      <Property name="Title" typeName="System.String" source="sTitle" nullable="True" length="128"/>

      <Property name="Project" typeName=" System.Int32" source="ixProject" nullable="True"/>

      <Property name="Area" typeName=" System.Int32" source="ixArea" nullable="True"/>

      <Property name="OpenedBy" typeName="Bugs.Person" source="ixPersonOpenedBy" nullable="True"/>

      <Property name="AssignedTo" typeName="Bugs.Person" source="ixPersonAssignedTo" nullable="True"/>

      <Property name="Status" typeName="System.Int32" source="ixStatus" nullable="True"/>

      <Property name="Priority" typeName="System.Int32" source="ixPriority" nullable="True"/>

</Type>

</Types>

</Schema>

 

Notice the use of the source attribute in the Type element to map a new Type to an existing table. The source is in an MSSQL full table path in the form [database].[owner].[table] and is used by Base4 to link from the Base4 database to another database on the same server.

Also in the above example we have taken this opportunity to ensure the resulting Base4 types are more readable and friendly by using the source attribute of the property to point to the existing field but to use the new name specified instead.

 

Once we have done this Base4 will create views in the Base4 database that encapsulate all access to the two tables from the [bugs] database, like this:

 

At this point is completely possible to query the Base4 for information in the legacy database like this:

 

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using Base4.Storage;

using Bugs;

 

namespace Base4.Storage.Quickstart

{

    static class Program

    {

        /// <summary>

        /// Find all Bugs assigned to Alex

        /// </summary>

        static void QueryTheBugsDatabase()

        {

            foreach (Bug bug in StorageContext.Find<Bug>(Bug.Fields.AssignedTo.Firstname == "Alex"))

            {

                Console.WriteLine(bug.Title);

            }

        }

    }

}