How to extend Database class

Topics: Data Access Application Block
Mar 30, 2007 at 4:46 PM
It is possible to extend Database class to add custom functionality whitout change EntLib source code?
I think somthing about this:

public class cyDataBase : Database
public void test()
// read from Database using provider model

void main()
cyDatabase db;

db = DatabaseFactory.CreateDatabase();

Can you help me to fins some documentation about it ?

Thank You!
Mar 30, 2007 at 4:55 PM
Hi Riccardo -

Yes you can definitely do this. There aren't any real tricks to subclassing Database in your own assemblies. The key part here is to get the DatabaseFactory to know how to return an instance of your class. You can do this by adding a "custom provider mapping* node using the configuration tool. This will tell the DAAB to use your database class for connections of a particular type.

Mar 30, 2007 at 5:41 PM
Do you know where can I find any samples or documentation?
My problem is that I want to create NEW properties or method in myDatabase class that are not in the Database interface
In my sample calling "db.test();" cause a NullObjectReference.

Mar 30, 2007 at 5:52 PM
I'm not sure why you would get a NullReferenceException. Provided your reference is of the dervied type, not the base Database type, it should be possible to call any methods on it. However the call to DatabaseFactory.CreateDatabase will return a Database, so you'll need to explicitly cast it to a cyDatabase. This should succeed if you correctly wired up your custom provider mappings.

The best example code is probably the SqlCeDatabase which is included in EntLib 3.0. It includes a derived database class with its own methods in a separate assembly, so it should be very much the same as what you are trying to do.

Mar 30, 2007 at 9:41 PM
OK! Now all works! but :-) in the cyDatabase constructor I had specify "SqlClientFactory.Instance" provider factory, it is not database indipendent.
I think that it is the only way for DAAB to know that the DataBase is an SQL Server database, then I this it is correct ???

Another question is how I implements "DeriveParameters(DbCommand discoveryCommand)" method ?

The last one :-)
My custom methods uses only generic methods like "LoadDataSet" then I like to use it also on a different type of database (MS Access, Oracle).
What is the best wat to do that ? Use another class to contain implementation an call it passing Database parameter ?

// Constructor
public cyDatabase(string connectionString)
: base(connectionString, SqlClientFactory.Instance)