As promised in this previous blog, I have created a custom Database implementation for Firebird in the EntLib 2.0 framework.
First I implemented this interface: Microsoft.Practices.EnterpriseLibrary.Data.Configuration.IDatabaseAssembler
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
namespace FirebirdSql.Data.EntLib
{
public class FbDatabaseAssembler : IDatabaseAssembler
{
public Microsoft.Practices.EnterpriseLibrary.Data.Database Assemble(string name, ConnectionStringSettings connectionStringSettings, IConfigurationSource configurationSource)
{
return new FbDatabase(connectionStringSettings.ConnectionString);
}
}
}
Then I derived from Microsoft.Practices.EnterpriseLibrary.Data.Database (we should do more deligence here to ensure we take care of FbClient's idiosyncracies by implementing appropriate overrides or provide additional functions, as in SqlDatabase and OracleDatabase):
using System;
using System.Data.Common;
using System.Security.Permissions;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using FirebirdSql.Data.FirebirdClient;
namespace FirebirdSql.Data.EntLib
{
[FirebirdClientPermission(SecurityAction.Demand)]
[DatabaseAssembler(typeof(FbDatabaseAssembler))]
public class FbDatabase : Database
{
public FbDatabase(string connectionString)
: base(connectionString, FirebirdClientFactory.Instance)
{
}
protected override void DeriveParameters(System.Data.Common.DbCommand discoveryCommand)
{
FbCommandBuilder.DeriveParameters((FbCommand)discoveryCommand);
}
}
}
And now I set up a winform test application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
namespace FirebirdSql.Data.EntLib.Test
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnDataset_Click(object sender, EventArgs e)
{
Database db = DatabaseFactory.CreateDatabase();
DbCommand comm = db.GetSqlStringCommand("SELECT JOB_CODE, JOB_REQUIREMENT FROM JOB");
DataSet ds = db.ExecuteDataSet(comm);
this.dgv.DataSource = ds.Tables[0];
}
}
}
Note that I can now execute SQL and bind to the control in only 4 lines of code. The connection string and the mapping between provider and FbDatabase is taken care of by the EntLib configuration console:
So you see it's really easy to implement a custom Database object for EntLib DAAB if its ADO.NET native providers exist. With this, developers can use consistent API to access the databases in an elegant way. It's especially useful when you need to access multiple databases (from different database types) in the same application.
8 comments:
Hugh,
Great article - thanks. Could you post the source files?
sdeepak, This blog host doesn't support posting non-image files. Send me an email (hugh.ang at gmail dot com). I will send you the zip file.
Hi! Could you send to me sources? Or could you help to resolve problem - how to add provider?
I'm trying something like this, but does not work:
< system.data >
< DbProviderFactories >
< add name="Firebird Data Provider" invariant="FirebirdSql.Data.EntLib" description=".Net Framework Data Provider for Firebird"
type="FirebirdSql.Data.EntLib.FbDatabase, SomeAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12345..." / >
< /DbProviderFactories >
< /system.data >
To the last comment, you can find the configuration in this Firebird ADO.NET Provider blog. Email me(hugh.ang at gmail dot com) if you need the source.
Post a Comment