Extend a legacy or third party application?
Once you have created a legacy schema
you can then extend your legacy application easily. You simply create a new Schema
that references the legacy schema and types.
In the above example
imagine you now want to track time spent against a bug by each person.
Neither the legacy [bugs]
database nor the legacy bug tracking application support this, but Base4 does with
ease.
Essentially you need a new table
for your Timesheet. Conceptually we want something like this:
The first step is to decide on what
columns we need for our Timesheet type. The requirement is very simple (isn’t it
always in samples!!!) so columns for the Bug, the Person, the work description,
the number of minutes, the date of the work and whether it has been invoiced should
be sufficient.
To do this we simply create another
Schema like this:
<Schema
Name="Bugs.Ex.dll" uri="http://www.base4.net/Schemas/Bugs.Ex.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" />
<!-- Also references the Bugs.dll schema
to access the Bug and Person types -->
<Reference
Name="Bugs.dll" dirty="True"
inDatabase="False"
uri="http://www.base4.net/schemas/Bugs.xml"
version="1" />
</References>
<Types>
<!-- Declare
a Timesheet type that re-uses the Bug and Person types from Bugs.dll
-->
<Type
FullName="Bugs.Ex.Timesheet" baseName="Base4.Storage.ItemImpl"
keyProperty="ID"
version="1" readOnly="True" >
<Property
name="ID" typeName="System.Guid"/>
<Property
name="Person" typeName="Bugs.Person"/>
<Property
name="Bug" typeName="Bugs.Bug"/>
<Property
name="Description" typeName="System.String"
length="256"/>
<Property
name="MinutesWorked" typeName="System.Int32"/>
<Property
name="WorkDate" typeName="System.DateTime"/>
<Property
name="Billed" typeName="System.Boolean"/>
</Type>
</Types>
</Schema>
Notice that this schema
references the legacy schema ‘Bugs.dll’,
also of interest is lack of a source
of the Timesheet type or for the properties of the Timesheet type, this approach
leaves the decision up to Base4. Once this schema is registered it will now be possible
to write code like this:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Base4.Storage;
using Bugs;
using Bugs.Ex;
namespace Base4.Storage.Quickstart
{
static class
Program
{
/// <summary>
/// Create a Timesheet
line attach a person and a bug and save
/// </summary>
static void
CreateATimesheet()
{
Timesheet sheet =
new Timesheet();
//Find a bug from the legacy database
sheet.Bug = StorageContext.FindOne<Bug>(Bug.Fields.Title
== "Problem with
XML Serialization");
//Find a person from the legacy database
sheet.Person = StorageContext.FindOne<Person>(Person.Fields.Firstname ==
"Alex");
sheet.Description = "Trying to reproduce
problem and writing tests";
sheet.MinutesWorked = 45;
sheet.DateWorked =
DateTime.Now;
sheet.Billed = false;
sheet.Save();
}
}
}
Imagine in fact that
you had just finished a Project and now needed to bill all unbilled time for all
Bugs in that project. You could easily do this too:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Base4.Storage;
using Bugs;
using Bugs.Ex;
namespace Base4.Storage.Quickstart
{
static class
Program
{
/// <summary>
/// Find all Unbilled
time for a particular project (number 25)
/// </summary>
static void
FindUnbilledTimesheets()
{
int unbilled = 0;
foreach (Timesheet
sheet in StorageContext.Find<Timesheet>(
Timesheet.Fields.Billed == false &&
Timesheet.Fields.Bug.Project == 25)
)
{
Console.WriteLine("{0} took {1} minutes and is unbilled.", sheet.Description,
sheet.MinutesWorked);
unbilled += sheet.MinutesWorked;
}
Console.WriteLine("A
total of {0} minutes unbilled on Project 25", unbilled);
}
}
}
As you can see once you have a legacy
application described in a Base4 schema it is very simple to re-expose and extend
that application. The examples above while very simple illustrate the approach that
can be taken.