Extending builder strategies and problems

Topics: CAB & Smart Client Software Factory
Nov 29, 2005 at 12:59 PM
originally posted by: sbia

Hello I've add strategies to implements dynamic proxy invocation. This proxy manage transactions between app and service layer.

I've inherited my class from BuilderStrategy as you can seen.


Public Class TransactionExecutionStrategy
Inherits BuilderStrategy

Public Overrides Function BuildUp(ByVal context As Microsoft.Practices.ObjectBuilder.IBuilderContext, ByVal typeToBuild As System.Type, ByVal existing As Object, ByVal idToBuild As String) As Object

Dim type As Type = existing.GetType()
Dim policy As ITransactionPolicy = context.Policies.Get(Of ITransactionPolicy)(type, idToBuild)

If Not policy Is Nothing Then
If policy.Transactions.Count > 0 Then
Dim gen As New ProxyGenerator
Return gen.CreateClassProxy(typeToBuild, New MyInvocationHandler())
End If
End If

Return MyBase.BuildUp(context, typeToBuild, existing, idToBuild)

End Function
End Class

The object's creation work fine and some method are intercepted (equals ...). It's OK.

But i've a controller with the proxyfied service declare with <ServiceDependency()> and when it build the controller it's doesnt locate the service in serviceCollection ????
And when i remove the strategy all instantiation works fine.

Where is my error.

Thanks
Nov 29, 2005 at 3:00 PM
originally posted by: BradWilsonMSFT

One thing I can see immediately is that when you make a proxy, you're short circuiting the rest of the dependency injection chain. It's hard to make a concrete recommendation, but it's likely that your code needs to look something like this:

Public Overrides Function BuildUp(ByVal context As Microsoft.Practices.ObjectBuilder.IBuilderContext, ByVal typeToBuild As System.Type, ByVal existing As Object, ByVal idToBuild As String) As Object

Dim type As Type = existing.GetType()
Dim policy As ITransactionPolicy = context.Policies.Get(Of ITransactionPolicy)(type, idToBuild)
Dim result As Object = existing

If Not policy Is Nothing Then
If policy.Transactions.Count > 0 Then
Dim gen As New ProxyGenerator
result = gen.CreateClassProxy(typeToBuild, New MyInvocationHandler())
End If
End If

Return MyBase.BuildUp(context, typeToBuild, result, idToBuild)

End Function
Nov 29, 2005 at 10:06 PM
originally posted by: sbia

Ok thanks for this help but it's doesn't work i've always ServiceMissingException in Get function in ServiceCollection Class.
Nov 30, 2005 at 2:30 PM
originally posted by: sbia

After some tests and lot of debugs i've see this. When it get service from service collection it locate the service in the inner collection but associate's weakreference is not available and target nothing.


thanks.
Dec 2, 2005 at 1:11 PM
originally posted by: BradWilsonMSFT

It's hard to guess what the problem is, since I'm not looking at your code (and really can't, even if I wanted to).

My best guess is that your object has not been placed into the lifetime container. The locator is essentially a dictionary that takes weak references, so for something whose lifetime is owned, it needs to go into the lifetime container so that something is holding a strong reference.
Dec 3, 2005 at 1:36 PM
originally posted by: sbia

Ok it's good, it work's well. thanks a lot for your help.