views:

478

answers:

1

Hi I need to pass some objects to and from .Net and a Flex presentation layer.

I need to pass and recieve the following object.

 public class Room: BasicRoom
{

    private int _seatingCap; 
    private RoomType _roomType;
    private IList<Equipment> _equipment;

public virtual RoomType roomType
    {
        get { return _roomType; }
        set { _roomType = value; }
    }
    public virtual IList<Equipment> equipment
    {
        get { return _equipment; }
        set { _equipment = value; }
    }
    public virtual int seatingCap
    {
        get { return _seatingCap; }
        set { _seatingCap = value; }
    }

Currently, I am just passing the above (domain object) to the presentation layer and that is fine. However, when I want to send the object back to .Net I run into a problem.

As, I'm using NHibernate as the orm tool it requires me to use an interface in this case IList to map collections. The problem arises when I try to pass the object back to .Net - the gateway (flash remoting - fluorineFX) baulks at equipment being typed as an IList and throws the error. "Cannot create an instance of an interface".

I obvously need to type equipment to List and not IList.

What are some ideas to get around this? Would it be better to convert to dto's?

Has anyone had experience with this?

I am fairly new to .Net so any help/pointers much appreciated.

A: 

Is there any reason the list needs to have a setter? Often, collection properties are read-only - you just Add/Remove/Clear them...

The virtual makes this trickier - normally I'd just do:

public IList<Foo> Foos {get; private set;}
public Bar() { // ctor
    Foos = new List<Foo>();
}

In this case, perhaps (since we don't want to call a virtual method in the ctor):

private IList<Foo> foos;
protected virtual IList<Foo> CreateFooList() {
    return new List<Foo>();
}
public IList<Foo> Foos {
    get {
        if(foos == null) foos = CreateFooList();
        return foos;
    }
}

If this still doesn't work, try a concrete list type.

Marc Gravell
Thanks,I think I will need a setter because equipment may be added to the room in the ui tool. Then when I update the room the room_equipment table will also be updated.
Sounds like a job for a collection with notification events...
Marc Gravell
Or a custom collection (subclass Collection<T>) that knows its owner (from the ctor) and calls Parent.OnUpdate() etc
Marc Gravell