-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping Generic.List NOT Generic.IList?
PostPosted: Tue Jun 16, 2009 10:58 am 
Newbie

Joined: Tue Jun 16, 2009 10:36 am
Posts: 2
Hi, I'm relatively new to NHibernate and seem to find myself in bit of situation.

1. I've got business this entity:
[Serializable]
public class Location
{
private String postCode = default(String);
public virtual String PostCode
{
get { return postCode; }
set { postCode = value; }
}

private IList<SubLocation> subLocations = default(IList<SubLocation>);
public virtual IList<SubLocation> SubLocations
{
get { return subLocations; }
set { subLocations = value; }
}

}
2. I can popluate Location object with SubLocations via mapping document (all works fine upto this point)
3. Then i need seriliaze Location to be able deseriliaze as data contract which is exposed via wcf service.
4. The return type for SubLocation is IList which is an interface therefore cannot be seriliazed, If i change it to Generic.List NHibernate throws exception(System.InvalidCastException : Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag to Generic.List')

Just wondering if anyone else has came across similar problem, or if there is any neat solution so NHibernate returns List instead of IList?

Help is much appreciated


Top
 Profile  
 
 Post subject: Re: Mapping Generic.List NOT Generic.IList? (SOLVED)
PostPosted: Thu Jun 18, 2009 5:49 am 
Newbie

Joined: Tue Jun 16, 2009 10:36 am
Posts: 2
I have manage to solve the problem in following way:

Code:
[Serializable]
public class Location
{
  private IList subLocations = new ArrayList();

  [XmlIgnore]
  protected virtual IList SubLocations
  {
     get { return subLocations; }
     set { subLocations = value; }
   }

  public virtual List<SubLocation> SubLocationCollection
    {
      get { return GetSubLocations(); }
    }

  public virtual List<SubLocation> GetSubLocations()
    {
        SubLocation[] locs = null;

        if (SubLocations.Count > 0)
        {
            locs = new SubLocation[SubLocations.Count];
            SubLocations.CopyTo(locs, 0);
        }

        return (new List<SubLocation>(locs));
      }
}


Obviously, you can always make it neater


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