-->
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.  [ 7 posts ] 
Author Message
 Post subject: Custom collection
PostPosted: Tue Jan 08, 2008 10:01 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hello,

I've made a class that inherits List<> and adds some new properties. I cannot use it directly in the mapped class as nhibernate only accept interfaces. Why such a limitation ? Is there a way to get around it ? I know it is possible to use an IPropertyAccessor implementation to load custom collection but is there an easier way ?

Regards,

mathmax


Top
 Profile  
 
 Post subject: Example custom collection
PostPosted: Tue Jan 08, 2008 12:56 pm 
Beginner
Beginner

Joined: Fri Sep 02, 2005 12:13 pm
Posts: 44
Location: Denver, CO
mathmax,

I have written a series of posts concerning the use of custom collections with NHibernate. I believe that the solution I present is a good approach for keeping the doman layer very clean and simple while still enabling compatibility with NHibernate. As you implied, there isn't an easy way to get around using interfaces but I hope this solution is relatively easy for you to use.

The posts are as follows:
Your feedback on this approach would be great to have...thanks!

Billy McCafferty


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 11:15 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Thank you for your answer. It seems very interesting. One thing I'm not sure to understand. Should I write a persistent collection for each new custom collection I create or is there a generic class to use instead ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 12:37 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Your solution doesn't fit me as I want to have a IDictionary<TKey, TValue>
:-(.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 12:40 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Perhabs you can explain your problem a little further, for instance provide your specialization of List<T> (that behaves like a IDictionary<TKey, TValue> ???? that sounds a litlle strange)

But lets see some code, im not sure i get your problem :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 1:02 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Sorry. It's a general problem. I need to easily create custom collection usable with nhibernate. At the beginning, I gave an example with a collection that inherits List<T>, but now I have a problem with a collection that hinerits Dictionary<T, U>.

I tried to write a IPropertyAccessor implementation :

public class DefaultAccessor : IPropertyAccessor
{
#region IPropertyAccessor Members

public IGetter GetGetter(System.Type theClass, string propertyName)
{
return new Getter(GetField(propertyName, theClass));
}

public ISetter GetSetter(System.Type theClass, string propertyName)
{
return new Setter(GetField(propertyName, theClass));
}

private FieldInfo GetField(string propertyName, System.Type theClass)
{
string fieldName = new HiddenFieldStrategy().GetFieldName(propertyName);
if(fieldName == null)
fieldName = new PascalCaseUnderscoreStrategy().GetFieldName(propertyName);

FieldInfo field = GetFieldInternal(theClass, fieldName);
if (field == null) throw new PropertyNotFoundException(theClass, fieldName);

return field;
}

private static FieldInfo GetFieldInternal(System.Type type, string fieldName)
{
if (type == null || type == typeof(object)) return null;

FieldInfo field = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
return (field != null) ? field : GetFieldInternal(type.BaseType, fieldName);
}

#endregion

private class Setter : ISetter
{
private FieldInfo fieldInfo;

internal Setter(FieldInfo fieldInfo)
{
this.fieldInfo = fieldInfo;
}

public void Set(object target, object value)
{
fieldInfo.SetValue(target, Activator.CreateInstance(fieldInfo.FieldType,new IDictionaryToGenericIDictionaryAdaptator<Iso, string>((IDictionary)value)));
}

public string PropertyName
{
get { return null; }
}

public MethodInfo Method
{
get { return null; }
}
}

private class Getter : IGetter
{
private FieldInfo fieldInfo;
private System.Type returnType;

public Getter(FieldInfo fieldInfo)
{
this.fieldInfo = fieldInfo;
returnType = GetReturnType(fieldInfo);
}

public object Get(object target)
{
var obj = fieldInfo.GetValue(target);
if (obj == null) return null;
return new Dictionary<Iso, string>((IDictionary<Iso, string>)obj);
}

public System.Type ReturnType
{
get { return returnType; }
}

public string PropertyName
{
get { return null; }
}

public MethodInfo Method
{
get { return null; }
}

private static System.Type GetReturnType(FieldInfo field)
{
System.Type type = field.FieldType;

// pretend that it is still a collection, otherwise lazy initialization stuff won't work
if (type.IsGenericType && typeof(ICollection<>).IsAssignableFrom(type.GetGenericTypeDefinition()))
return typeof(ICollection<>).MakeGenericType(type.GetGenericArguments());

return type;
}
}
}

The data are loaded without problem in my objects. However, it doesn't succeed on commit(). I get the following error :

could not insert collection: [TestNhibernate.Test.Name#1]
Inner exception : {"Specified cast is not valid."}
Message : "could not insert collection: [TestNhibernate.Test.Name#1]"

Do you know what is the problem ? I don't insist on doing this way, I just want an easy solution to implement custom collections with nhibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 12, 2008 9:27 am 
Contributor
Contributor

Joined: Tue May 30, 2006 1:25 am
Posts: 29
In the client side you must declare an interface because NH need to proxy your collection.
In a soon future (NH2.0) you can use your tuplizer to do what you want.
So far something that work similar of what you need is "dynamic-component".
http://www.hibernate.org/hib_docs/nhibe ... -component

Bye.

_________________
Fabio Maulo.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.