Override CreateDatabase() method

Topics: Data Access Application Block
Jan 3, 2012 at 3:43 AM


I am passing the connection string at runtime to the DAAB. Thatswhy I want to override the CreateDatabase() method. 

I have created a custom method & its working fine 


Public Shared Function CreateDatabaseCustom(ByVal connectionString As String) As Database
        Dim connectionString As String = Nothing
        Dim objDbProviderFactory As System.Data.Common.DbProviderFactory

        objDbProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient")
        Return New GenericDatabase(connectionString, objDbProviderFactory)

    End Function

Note that the method name is CreateDatabaseCustom(). But I want to implement this method with the same name as CreateDatabase() and thus want
to override the implementation of DatabaseFactory.CreateDatabase()

How do I achieve this?
Jan 3, 2012 at 4:51 AM
Edited Jan 3, 2012 at 4:59 AM

What class does your CreateDatabaseCustom currently belong to?

You can't inherit and override the DatabaseFactory because it is a static class.  Also, you can't create any extension methods because it's static.

One option would be to create a facade around the DatabaseFactory where your class would call the DatabaseFactory.Create method unless a connection string was passed as an argument in which case you would execute your CreateDatabaseCustom logic.  

Perhaps if you elaborate more on your scenario we could make some other suggestions?  E.g. is the runtime connection string determined once at startup and then doesn't change or does it change (potentially) for every call?  etc.

Randy Levy
Enterprise Library support engineer

Jan 3, 2012 at 8:00 AM

Ya.. the scenario is that the connection string is determined/set once @start up. The connection string is getting formed based on some input parameters passed by another program And hence is not a hard-coded one. By this way I am constructing the conn string.

And then I m passing this conn str to the method which I hv posted in my earlier thread.

So, could u suggest me how a facade can be created here around DatabaseFactory where my class would call DatabaseFactory.Create method and subsequently return the Database object. Some code would help me out.






Jan 3, 2012 at 4:27 PM

If the connectionString is only set once and is known at startup then you could configure Enterprise Library using the Fluent API to configure the block programmatically at runtime and supply your connection string.

After configuration you can just use all of the standard Enterprise Library APIs.

Randy Levy
Enterprise Library support engineer