refactoring 4.1 to 5.0, provider inheriting from GenericDatabase

Topics: Building and extending application blocks, Data Access Application Block
Mar 9, 2011 at 6:11 AM
Edited Mar 9, 2011 at 6:13 AM

Been thru some threads on this as well as the 5.0 documentation, not having much luck.  Excuse the VB.Net, not an overly proficient C# developer.

I had an old class that inherited from GenericDatabase and did the whole DatabaseAssembler stuff to build a custom ODP.NET provider.  Since the EntLibContrib doesn't work w/ 5.0 yet, can't migrate to the contrib ODP.NET provider.  Just a GenericDatabase will work w/ ODP.NET connection string and Oracle.DataAccess, but I need to override DeriveParameters and add some additional functionality for PLSQL Assoc Array.

Long story short, need to refactor this and not having any luck.  My config looks the same as in 4.1, provider mapping an all, and the code is firing.

But I get an error on the type registration line (in RED) that states ...
Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration`1[Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase]' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration]'.

Any ideas?

Imports System.Data.Common
Imports Oracle.DataAccess.Client
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.Data.Configuration

<ConfigurationElementType(GetType(OracleOdpNetDatabaseData))> _
Public Class OracleOdpNetDatabase
    Inherits GenericDatabase

    Public Sub New(ByVal connectionString As String, ByVal dbProviderFactory As DbProviderFactory)

        MyBase.New(connectionString, dbProviderFactory)

    End Sub

    Protected Overrides Sub DeriveParameters(ByVal discoveryCommand As DbCommand)

...custom logic

    End Sub

    Public Shared Sub AssignArrayListToParameter(ByVal ValueArrayList As ArrayList, ByVal Parameter As DbParameter)

...custom logic

    End Sub

End Class

Public Class OracleOdpNetDatabaseData
    Inherits GenericDatabaseData

    Public Sub New(ByVal connectionStringSettings As System.Configuration.ConnectionStringSettings, ByVal configurationSource As IConfigurationSource)
        MyBase.New(connectionStringSettings, configurationSource)
    End Sub

    Public Overrides Function GetRegistrations() As IEnumerable(Of TypeRegistration)
        Dim providerFactory As DbProviderFactory = DbProviderFactories.GetFactory(ConnectionStringSettings.ProviderName)
        Return New TypeRegistration(Of GenericDatabase)(Function() New OracleOdpNetDatabase(ConnectionString, providerFactory)) With { _
         .Name = Name, _
         .Lifetime = TypeRegistrationLifetime.Transient _
        }
    End Function

End Class

Mar 9, 2011 at 7:33 AM

Ok, first and foremost, you should inherit from the Database class, not from the GenericDatabase class.  Next, the error is because the GetRegistrations return type is an IEnumerable of TypeRegistration but you're returning a single TypeRegistration object.  Modify your code to something like this:

Public Overrides Function GetRegistrations() As IEnumerable(Of TypeRegistration)
        Dim typeRegistrations As New List(Of TypeRegistration)
        
        Dim providerFactory As DbProviderFactory = DbProviderFactories.GetFactory(ConnectionStringSettings.ProviderName)
        typeRegistrations.Add(New TypeRegistration(Of Database(Function() New OracleOdpNetDatabase(ConnectionString, providerFactory)) With { _
         .Name = Name, _
         .Lifetime = TypeRegistrationLifetime.Transient _
        }

	Return typeRegistrations
End Function

Hope this helps.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Mar 9, 2011 at 3:31 PM

duh, thanks Sarah, that was it...another pair of eyes.  totally missed the return and the IEnumerable.

up and running, works perfectly.

Dec 21, 2011 at 9:03 PM

THANK YOU to both of you!!! I had the same dilemma and after 1.5 days, I was able to come accross this email. I'm also up and running.

 

You rock, Sarah!