1

我有 2 个实体,“交易”和“商店”。我想将这些组合在一个列表中,以及一个整数或其他指示类型的东西。

我做了一些谷歌搜索并提出了一个解决方案,但由于它非常新且令人困惑,我不确定它是否是最好的解决方案。

我已经使用了一个界面来执行此操作 - 下面的代码,我很感激一些关于这是否是正确和合理的方法的建议。

public interface IBookmarkWrapper
{
    int DataType { get; private set; }
    object Data { get; private set; }
}

public class DealBookmarkWrapper : IBookmarkWrapper
{
    public int DataType { get; private set; }
    public object Data { get; private set; }

    public DealBookmarkWrapper(Deal deal)
    {
        deal.ThrowNull("deal");

        DataType = 1;
        Data = deal;
    }
}

并在使用中:

    var list = new List<IBookmarkWrapper>();
    list.Add(new DealBookmarkWrapper(deal));
4

3 回答 3

2

您是否考虑过使用 C# Struct

struct DealBookmarker
    {
        public Deal Dl;
        public Store St;
        public int Type;
    }
List<DealBookmarker> DBM = new List<DealBookmarker>();

DBM.add(new DealBookmarker)

如有必要,您还可以向 Struct 添加构造函数

与作为 ref 类型的类不同,结构是值类型,它允许您创建行为类似于内置数据类型的对象,因为它是在堆栈而不是堆上启动的,它会产生性能提升。

在这种情况下,在类上使用 struct 的好处:

  1. 更好的性能
  2. 通过值而不是引用传递
  3. 可以有一个构造函数(必须参数化)
  4. 仍然可以实现一个接口
  5. 和我最喜欢的简单版!!!
于 2012-09-17T20:10:42.870 回答
1

如果您打算在转发器中使用它(我希望您实际上是指 ListView),那么只需在接口中定义您的属性,然后在 Deal 和 Store 类中实现接口。然后您可以将列表绑定到转发器/列表视图并按名称调用属性。不需要任何诡计。通过这样做,接口保证您的属性可用(否则 DataTextvalue 将在绑定期间中断)。

换句话说,如果您要绑定到 ListView,则显示属性需要在 Store 和 Deal 类中命名相同。因此,您不妨以最基本的形式使用该接口:

protected Page_Load(object sender, EventArgs e)
{
    var list = new List<IWatchamacallit>();
    list.Add(new Store { Property1 = "Store1", Property2 = "StoreInfo"});
    list.Add(new Store { Property1 = "Store2", Property2 = "StoreInfo" });
    list.Add(new Deal { Property1 = "Deal1", Property2 = "DealInfo" });
    list.Add(new Deal { Property1 = "Deal2", Property2 = "DealInfo" });

    myListView.DataSource = list;
    myListView.DataBind();

    /*  from here just set your page controls to call the properties
        for instance:
            <asp:Label Text='<%# Eval("Property1") %>' />
            <asp:Label text='<%# Eval("Property2") %>' />   
    */
}

public interface IWatchamacallit
{
    string Property1 { get; set; }
    string Property2 { get; set; }
}

public class Store : IWatchamacallit
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class Deal : IWatchamacallit
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

您的输入将绑定看起来像:

Property1   Property1
=====================
Deal1       DealInfo
Deal2       DealInfo
Store1      StoreInfo
Store2      StoreInfo

您需要保留的任何其他值(如 dealId 或 storeId)都可以作为属性添加到您的类中。只需确保在界面中定义它们并使用一致的命名即可。通过这样做,您可以在保持类结构的同时用两种不同的类型填充列表。如果您稍后需要从列表中选择它们,您可以像这样抛出:

foreach (var item in list)
{
    var tempContainer = Activator.CreateInstance(item.GetType());
    tempContainer = item;
}

或其他几种方式中的任何一种,具体取决于您要完成的工作。

于 2012-09-17T21:59:49.133 回答
1

你可以实现Factory method(GOF的模式)

链接: http: //www.dofactory.com/Patterns/PatternFactory.aspx

注意:

“交易”和“商店”是您的Concrete Product

IBookmarkWrapper 是你的Product

并定义Creator

于 2012-09-17T13:38:38.040 回答