alkampfer on May 22nd, 2009

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

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.

<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.

public static Type GetConcreteTypeFor(Type service)
{
    return ActualContainer.Kernel
      .GetHandler(service).ComponentModel.Implementation;
}

Alk.

Tags:

kick it on DotNetKicks.com

3 Responses to “Asp.Net, ObjectDataSource and Castle Windsor”

  1. In my layered web application I have used the method you described to obtain isolation.

    I have no problem with get method, but when I tried with update method I have some problems.

    Can you post an example on how you can make an update with your layered structure?

    P.S.: I want to pass to update function or the concrete object or the interface and then “translate” it into concrete.

    I tried to place the interface in objectdatasource but I obtained an error because it tried to instantiate it.

    Then I tried to specify the DataObjectTypeName adding the following in objectdatasource Init:

    protected override void OnInit(EventArgs e)
    {
    RefreshTypeName();
    base.OnInit(e);
    }
    public void RefreshTypeName() {
    base.TypeName = GetConcreteTypeFor(Type.GetType(TypeName + “, MachinaWeb.Services”)).FullName;

    if (!String.IsNullOrEmpty(DataObjectTypeName))
    {
    base.DataObjectTypeName = GetConcreteTypeFor(Type.GetType(DataObjectTypeName + “, MachinaWeb.Services”)).FullName;
    }

    }

    but I cannot obtain isolation because with this lines it needs and update method with Concrete class as argument of update function (I want interface as argument).

  2. Actually in that site I do not need to use Update of ObjectDataSource :( . All object data source are used only to GET data from the services, then all the pages communicates with the service with jQuery, so it never used the update part of objectdatasource.

    If I have time I’ll investigate this situation.

    Alk.

Trackbacks/Pingbacks

  1. Arjan`s World » LINKBLOG for May 23, 2009