转变? 域对象到 DTO - 无法创建接口实例

发布于 2024-07-13 15:43:35 字数 962 浏览 5 评论 0原文

您好,我需要在 .Net 和 Flex 表示层之间传递一些对象。

我需要传递并接收以下对象。

 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; }
    }

目前,我只是将上述(域对象)传递到表示层,这很好。 但是,当我想将对象发送回 .Net 时,我遇到了问题。

因为,我使用 NHibernate 作为 orm 工具,所以它要求我在本例中使用 IList 接口来映射集合。 当我尝试将对象传递回 .Net 时,问题就出现了 - 网关(闪存远程处理 - florineFX)阻止设备被键入为 IList 并引发错误。 “无法创建接口的实例”。

显然我需要将设备键入 List 而不是 IList。

有什么想法可以解决这个问题? 转换为 dto 会更好吗?

有人有这方面的经验吗?

我对 .Net 相当陌生,因此非常感谢任何帮助/指示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡寂寞咖啡 2024-07-20 15:43:35

列表需要有设置器吗? 通常,集合属性是只读的 - 您只需添加/删除/清除它们...

虚拟使这变得更棘手 - 通常我会这样做:

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

在这种情况下,也许(因为我们不想在构造函数中调用虚拟方法):

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;
    }
}

如果这仍然不起作用,请尝试具体的列表类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文