Binding an Interface

Binding an Interface

Old forum URL: forums.lhotka.net/forums/t/3629.aspx


Angel posted on Monday, October 01, 2007

This is a .net framework general question.

Is posilbe to bind an interface instead a object?

If I have this definition:

 

Public Class PruebaObjeto

Implements IPruebaInterface

Public ReadOnly Property Nombre() As String

Get

Return "Prueba Objeto"

End Get

End Property

Private ReadOnly Property IPruebaInterface_Nombre() As String Implements IPruebaInterface.Nombre

Get

Return "Prueba Interface"

End Get

End Property

End Class

Public Interface IPruebaInterface

ReadOnly Property Nombre() As String

End Interface

 

I Want to do this

 

Dim x As IPruebaInterface

x = New PruebaObjeto

Me.NombreTextEdit.DataBindings.Add(New System.Windows.Forms.Binding("EditValue", Me.IPruebaInterfaceBindingSource, "Nombre", True))

Me.IPruebaInterfaceBindingSource.DataSource = CType(x, IPruebaInterface)

 

I want to bind the property from the interface but I always got the object definition.

There is a way to bind an interface?

RockfordLhotka replied on Monday, October 01, 2007

Not that I'm aware of. All three UI technologies bind to the "native interface" of an object, not any secondary interfaces implemented by that object.

Angel replied on Tuesday, October 02, 2007

Bad news...

Any pattern to work arround this issue?

I have 3 objects that implements an interface, and are interface properties that I want to manage

Thanks

RockfordLhotka replied on Tuesday, October 02, 2007

Nothing easy. This is just the way .NET works…

 

The only real workaround I know of, is to create another class (in the UI layer) that looks like the interface, but doesn’t implement it. Does that make sense? This class would have all the properties you want to bind, and it is THIS object you bind to the UI.

 

This object accepts an IWhatever in its ctor, and all its properties delegate to the IWhatever properties of the _real_ object.

 

Public Class Customer

  Private _iCustomer As ICustomer

 

  Public Property Name() As String

    Get

      Return _iCustomer.Name

    End Get

    Set(ByVal value As String)

      _iCustomer.Name = value

    End Set

  End Property

 

  ‘ …

 

  Public Sub New(ByVal iCust As ICustomer)

    _iCustomer = iCust

  End Sub

End Class

 

This “de-interfaces” the interface, allowing your business layer to be interface-based, and the UI to bind to something with the right shape.

 

If you want to be fancy, you could make this UI class implement ICustomTypeDescriptor and use reflection to do this indirection against the interface.

 

I know, the instinct is to shudder and cringe at the mention of reflection. But remember that all the data binding technologies use reflection to do what THEY do, so if you use data binding you are already hip deep in reflection anyway, so a little more can’t really hurt all that much can it? ;)

 

Rocky

 

triplea replied on Tuesday, October 02, 2007

I have done something a bit different... I have something like a CustomerBase object (builder class) and some concrete sub classes e.g. VipCustomer, RepeatCustomer etc (out of context note: I know that inheritance is not encouraged but in this case I had shared behaviour and think it was a good call). CustomerBase implements an interface lets call it IMyInterface.

I have some customer specific user controls (e.g. TextBox, ComboBox etc) and in there (in my case in OnPaint) I do the following (I ommit a lot of checks which you can implement):

Library.IMyInterface boundObj = ((BindingSource)this.DataBindings[0].DataSource).DataSource as Library.IMyInterface;

Thats the closes example I have and am not sure if its a good practice but it works. I had some trouble with the designer but after I fixed that all went smooth.

Hope this helps.

Copyright (c) Marimer LLC