CSLA CreateInstance BusinessListBase results in System.TypeLoadException

CSLA CreateInstance BusinessListBase results in System.TypeLoadException

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


meaningoflights posted on Friday, January 08, 2010

Hi,

I'm trying to load a BusinessBaseList class dynamically. Its for the purpose of unit testing, to be able to load child objects dynamically (so a validation rule passes in the parent object).

We have a parent "Subscription" object which holds SubscriptionRecipients and here's what the classes look like:

Public Class Subscription
    Inherits BusinessBase(Of Subscription)
    Private WithEvents _subscriptionRecipients As SubscriptionRecipients = SubscriptionRecipients.NewSubscriptionRecipients()
 .....
  Private Sub New()
        ' require use of factory methods
    End Sub
.....
End Class

Public Class SubscriptionRecipients
    Inherits BusinessListBase(Of SubscriptionRecipients, SubscriptionRecipient)
....
    Private Sub New()
        MarkAsChild()
    End Sub
...
End Class

Public Class SubscriptionRecipient
    Inherits BusinessBase(Of SubscriptionRecipient)
....



I can easily dynamically create a SubscriptionRecipient, eg:

oh = Activator.CreateInstance(Nothing, businessObjectName)
obj = oh.Unwrap()


But with every attempt at dynamically creating a SubscriptionRecipientS object I get a System.TypeLoadException.

I've tried various strategies and am banging my head against a wall. Rubbing broken glass in my eye's would be less painful at this stage... Any suggestions would much be appreciated!

Thanks in advance

ps I'm trying to get FusLogVW.exe going but haven't used it before and its not giving me any clues :(

RockfordLhotka replied on Friday, January 08, 2010

You are using Activator.CreateInstance() to replicate the data portal, because the data portal is failing to create the instance?

Is this in a 3-tier physicaly deployment? If so, are you sure you have the same business DLL on client and server?

meaningoflights replied on Friday, January 08, 2010

Hi Rocky, thanks for your response. I'm not at work and wont get to try anything till Monday but thought I'd ask one more Q.

I'm using Activator.CreateInstance() since I wish to be able to create objects dynamically to make the unit tests more re-usable. I'll have to do a bit more reading, is it even possible to use the DataPortal without hardcoding the type?  EG, this wont work but demonstrates the concept of a perfect solution:

DataPortal.Create(Of  Type.GetType("stringNameOfBusinessObject"))

?

The solution is a winform app, 3 tiers with BO's, CSLA and Presentation layer, its client only.

Any help much appreciated!




RockfordLhotka replied on Friday, January 08, 2010

The data portal works on Type objects or type information. So you can sort of be dynamic, but generics don’t quite work as you have in your post :)

 

If you are calling Activator.CreateInstance() in a unit test you need to be aware of the context in which the unit test is running. Unit test frameworks create special AppDomain environments in which the tests run, and dynamic type loading doesn’t always work as expected. It depends on how the unit test framework sets up the type search path for the .NET type loader. It could easily be the case that you are running into an issue along this line.

meaningoflights replied on Friday, January 08, 2010

Hi,

I haven't tried VS2010 (.net 4) but have a feeling the dynamic type would help out for this task.

We're using Nunit tests its not anything funky like RhinoMocks at this job, just test classes with test methods in the BO tier. I did think along your lines with the search path and this thread sheds some light on the context:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/aa6667e5-cd80-4414-ad4e-2ae799c347c3

I'm pretty sure I'm in the right context, seeing I can dynamically load a SubscriptionRecipient fine, its only the SubscriptionRecipientS object causes the TypeLoadException and I'm going to hunt that down with techniques like: http://www.eggheadcafe.com/software/aspnet/31984726/how-to-debug-systemtypel.aspx

I'll try to put together a bare bones repro and will post some more info on Monday, have a great weekend:)

meaningoflights replied on Sunday, January 10, 2010

Hi,

I've put together a cut down solution that repro's the problem:

ReproCreateInstance.zip

In the TestClass there is 2 test methods, the first test dynamically instantiates a BusinessBase successfully but the second unit test fails with a "System.LoadTypeException" when dynamically instantiating a BusinessLISTBase

I'd very much appreciate any advice, feedback, idea's, etc on how to dynamically instantiate a BusinessLISTBase?  Many thanks

meaningoflights replied on Monday, January 11, 2010

Ok for anyone else... I've got a workaround:

Dim businessObjectName As String = .PropertyType.FullName & "+Criteria"
'for example: businessObjectName = "xxx.xxx.BusinessObjects.SubscriptionRecipients+Criteria"
oh = Activator.CreateInstance(Nothing, businessObjectName)
obj = oh.Unwrap()
Dim childObj As Object = DataPortal.Fetch(obj)

Its not perfect, but for a unit test to get past a validation rule enforcing that a Parent Object containing collection(s) of Children must have atleast one Child Object per collection(s).. this will be fine.

Copyright (c) Marimer LLC