These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Base abstract class with template
PostPosted: Fri Jan 19, 2007 2:07 pm 
Newbie

Joined: Fri Jan 19, 2007 1:42 pm
Posts: 6
Hi all,

Ive just started using NHibernate so please forgive me if this issue has already been addressed (I did search but could find no answers)

I am using NHibernate v 1.2.0 and VS2005 sp1

I have a base abstract class that has a template

e.g
public abstract class BaseApplicationClass<Identifier>
{
public IList<Identifier> someStuff;
}

I inherit from this and supply the template in my subclasses

e,g
public class UpdateApplication : BaseApplicationClass<int>
public class ViewApplication: BaseApplicationClass<string>

so now the list someStuff in my UpdateApplication is an array of ints while the list someStuff in my ViewApplication is an array of strings

I am using a single table that has a row for each of the concrete classes (in this case different types of application)

e.g
ApplicationTable
(
ApplicationId int,
ApplicationName varchar(50)
)


so the rows in my DB would be

e.g
ApplicationId ApplicationName
--------------- -------------------
1 UpdateApplication
2 ViewApplication

I want to create a mapping file for the base class (I believe NHibernate wont try and instantiate abstract classes) and then use a subclass with a discriminator for each of the concrete class mapping files with each one extending my BaseApplicationClass based on the ApplicationName in the database



When I do this I get an error message saying that
"
NHibernate.MappingException:- Could not compile the mapping document 'BaseApplicationClass.hbm.xml'
NHibernate.MappingException:- persistent class BaseApplicationClass not found
System.TypeLoadException : Could not load type BaseApplicationClass

If I remove the template from the BaseApplicationClass it works a treat

I could skip using subclass and just have a class with a where statement for each subclass

e.g where="ApplicationName='UpdateApplication'"

but that feels a little "hacky"

Is this a known issue (or am I being a fool) and if so is there a fix ( I cant remove the template from my baseclass in the final solution BTW)

many thanks

W


Top
 Profile  
 
 Post subject: Re:Base abstract class with template
PostPosted: Mon Jan 22, 2007 10:30 am 
Newbie

Joined: Fri Jan 19, 2007 1:42 pm
Posts: 6
After some digging around in the source Ive found what the problem is

Firstly MS mangles the class names for Templated classes

so for

public abstract class TemplatedClass<T1,T2>

the actual name of the type is TemplatedClass`2.

This is not a problem as I simply changed the name of the mapping file and the name attribute accordingly BUT it still failed

After further digging Ive found that a new DynamicMethod object is being created with the templated class as the owner....but unfortunately according to the MS documentation an ArgumentException is thrown if

"owner is an interface, an array, an open generic type, or a type parameter of a generic type or method."

So it looks like it is impossible in NHibernate to use a subclass of an abstract templated class (where the subclass supplies the template)

In my app I have a bunch of applications which contain an IList<UserType> - each application then specifies the type of user it can hold when deriving from the base abstract Application<UserType> class

anyone any suggestions ????

thanks

W


Top
 Profile  
 
 Post subject: Fixed
PostPosted: Mon Jan 22, 2007 2:35 pm 
Newbie

Joined: Fri Jan 19, 2007 1:42 pm
Posts: 6
I added this line to the first line in the NHibernate.ReflectionOptimizer class

if (mappedType.IsGenericType && mappedType.IsAbstract) return;

which solved my problem


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.