Strange Validation Issue with Dependency Injection

Posts   
 
    
Posts: 112
Joined: 09-Aug-2004
# Posted on: 06-Nov-2008 23:05:05   

LLBLGen Pro. Version: 2.6 Final (October 6th, 2008 ) Runtime Issue

I am having a strange issue while using Dependency Injection. Consider the following classes


CustomerEntity : CommonEntityBase  //generated

MessageValidator : ValidatorBase

[DependencyInjectionInfo(typeof(ChangeRequestEntity), "Validator")]
[Serializable]
CustomerValidator : MessageValidator


With Dependency Injection when casting to MessageValidator I am getting a cannot cast exception


CustomerEntity cust = new CustomerEntity();
cust.ValidateEntity();

MessageValidator validator = (MessageValidator)cust.Validator;

With Dependency Injection disabled, the following code works fine


CustomerEntity cust = new CustomerEntity();
cust.Validator = new CustomerValidator();
cust.ValidateEntity();

MessageValidator validator = (MessageValidator)cust.Validator;

Why is this? I am able to cast in the immediate window and even able to see the base class information through Watch (with DI).

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Nov-2008 10:45:38   

[DependencyInjectionInfo(typeof(ChangeRequestEntity), "Validator")] [Serializable] CustomerValidator : MessageValidator

If the above posted code is real, then the issue is that using DI, the Customer.Validator is not set to CustomerValidator, as the DependencyInjectionInfo attribute of the CustomerValidator defines another entity type to be injected in.

Posts: 112
Joined: 09-Aug-2004
# Posted on: 07-Nov-2008 15:28:11   

Sorry, I copy and pasted from the wrong entity when I went back and edited.

I took the Validator example and was able to reproduce this situation. Please see the attached file. If you look in the Validators project you will see the MessageValidator. The OrderValidator now inherits from the MessageValidator. In the web project there is now a Test.aspx which gets a new order and tries to cast the validator.

Please also notice that you can use the VS debugger to see the value of message by adding it to a watch or typing it into the immediate window.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 08-Nov-2008 16:04:05   

Will look into it. It might be due to ASP.NET which creates perhaps multiple appdomains, so a type is in a different appdomain, that's the only explanation I can give.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 10-Nov-2008 13:49:13   

When breaking in Test.aspx.cs, on line 18, the cast to MessageValidator:


?typeof(MessageValidator).IsAssignableFrom(order.Validator.GetType())
false
?order.Validator.GetType().IsAssignableFrom(typeof(MessageValidator))
false

Which means the type of the validator referred by order.Validator isn't casteable to MessageValidator.

When I look at the order.Validator.GetType() type, I check the type's assembly's location: C:\temp\Adapter_Example\GUI\bin\SD.LLBLGen.Pro.Examples.Validation.Validators.dll

When I now check typeof(MessageValidator).Assembly.Location, I get: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\ f495527c\86294345\assembly\dl3\b6f6ca6e\e78a563a_3043c901 \SD.LLBLGen.Pro.Examples.Validation.Validators.DLL

I.o.w.: a different path, and therefore incompatible types as they're located in different assemblies.

I'll see if I can come up with some way to make asp.net use the same assembly.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 10-Nov-2008 14:06:48   

Well, whaddoyaknow! -> http://www.infini-tec.de/post/2008/05/InvalidCastException-Unable-to-cast-object-of-Type-X-to-X.aspx Changing LoadFile into LoadFrom fixes it.

I've now to check whether this has sideeffects for other elements.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 10-Nov-2008 14:20:43   

Ok, I rolled back the LoadFile -> LoadFrom change, as the side effects can be too big.

But! in the documentation the solution is already given. simple_smile Click on that link and scroll UP a bit. You'll see an 'Important' box. Follow the advice.

You'll then get:


<dependencyInjectionInformation>
    <additionalAssemblies>
        <assembly fullName="SD.LLBLGen.Pro.Examples.Validation.Validators, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </additionalAssemblies>
</dependencyInjectionInformation>

et voila, it works.

Frans Bouma | Lead developer LLBLGen Pro