views:

482

answers:

3

i'll use some sample code to demonstrate my problem...

this is an entity

public class Channel : EntityBase
{

    [DataMember]
    public virtual IList<LocalChannel> LocalChannels { get; set; }
}

local channel has a string property.

this 2 classes mapped fluently and works fine with the has many relation.

the problem is in the wcf service.

when i'm selecting a channel or all channels.

the localChannels list is fixed size. (the type of ILIst that returns is typed array)

i want i to be a List.

Nhibernate wont let me to write this:

public virtual List<LocalChannel> LocalChannels { get; set; }

becuase it cant cast his collections to List

and my proxy is written in code and not generated with svcutil so i cant change the collection type.

any solutions?

+2  A: 

See my answer to http://stackoverflow.com/questions/1537958/manually-change-the-clientbase-collection-type-from-array-to-list

Does the NHibernate projection and DataContract projection have to be the same? I don't know much about NHibernate, but can you do something like this?

public class Channel : EntityBase{

  //For WCF
  [DataMember(Name="LocalChannel")]
  private List<LocalChannel> LocalChannelsPrivate {
     get {return new List<LocalChannel>(LocalChannels);}
    set {LocalChannels=value;}
  }

  //For NHibernate
  public virtual IList<LocalChannel> LocalChannels {get; set;}
}
Eugene Osovetsky
10x nice idea..i think i just use the svc util and avoid patch.
Chen Kinnrot
A: 
private IList<LocalChannel>channels;

public List<LocalChannel>Channels{ get { return this.channels as List<LocalChannel>; } set{ this.channels = value;}

NHibernate will be able to use IList but your public interface can use List

Adam Fyles
this can't be done cause nhibernate implement IList<> but not inherit List, the Channels is actually a Persistance bag or some other collection implementation of nhibernate.so the cast is impossible, thats why i cant use List<>
Chen Kinnrot
A: 

There is an alternative to the accepted answer if you don't want to use multiple properties. It takes advantage of how WCF deserializes properties. Using the technique described in this post, you could code the class as follows:

        public class Channel : EntityBase{

            //Initialize backing var to an empty list or null as desired.
            private IList<LocalChannel> _localChannels = new List<LocalChannel>();

            //For WCF & NHibernate:
            [DataMember]
            public virtual IList<LocalChannel> LocalChannels
            {
                get {return _localChannels;}
                set {_localChannels = new List<LocalChannel>(value);}
            }
        }
Sixto Saez