AspNet ObjectDataSource and Castle Windsor

I’m restructuring a portion of a site, it is well structured, and all logic is inside business classes in another assembly, and they are accessed with ObjectDataSources. My problem is that with the new structure I cannot refer to the concrete classes anymore, but I need to resolve them with an IoC container, like Castle Windsor.

The solution to this problem was really simple, I created this simple class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Public Class IoCObjectDataSource : Inherits ObjectDataSource
   Public Shadows Property TypeName() As String
       Get
           Return MyBase.TypeName
       End Get
       Set(ByVal value As String)
           MyBase.TypeName = IoC.GetConcreteTypeFor(Type.GetType(value)).FullName
       End Set
   End Property
End Class

It inherits from the basic ObjectDataSource, but it shadows the TypeName property; in setter part of TypeName it call a IoC wrapper to get the concrete name of the component configured for a given interface. Now I can specify an interface instead of the real type.

1
2
3
<rmWeb:IoCObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
   SelectMethod="GetLogTypes" TypeName="MyProject.DataService.IXmlLogService, MyProject.DataService" >
</rmWeb:IoCObjectDataSource>

Thanks to Castle Windsor I can easily recover the concrete type registered for a given interface.

1
2
3
4
5
public static Type GetConcreteTypeFor(Type service)
{
    return ActualContainer.Kernel
     .GetHandler(service).ComponentModel.Implementation;
}

Alk.

Tags: Castle Windsor .Net Framework IoC Asp.Net