C# 基于分组c的WCF服务响应设计#

C# 基于分组c的WCF服务响应设计#,c#,wcf,design-patterns,C#,Wcf,Design Patterns,我需要帮助设计我的WCF服务。我有一个从数据库返回产品的方法。在用户界面上,我有以下选项来分组产品: 否/默认分组:返回所有产品的列表 制造商分组:根据特定制造商返回所有产品的列表 应税分组:返回所有应税产品的列表 下面是我的整个回答结构: <ResponseStruct> <!-- Grouping is done on product--> <ProductList> <Product> &l

我需要帮助设计我的WCF服务。我有一个从数据库返回
产品的方法。在用户界面上,我有以下选项来分组产品:

  • 否/默认分组:返回所有
    产品的列表

  • 制造商分组:根据特定制造商返回所有
    产品的列表

  • 应税分组:返回所有应税产品的列表

  • 下面是我的整个回答结构:

    <ResponseStruct>
      <!-- Grouping is done on product-->
        <ProductList>
            <Product>
                <Name></Name>
                <Taxability>
                    <Value></Value>
                </Taxability>
                <ManufacturerList>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                </ManufacturerList>
            </Product>
            <Product>
                <Name></Name>
                <Taxability>
                    <Value></Value>
                </Taxability>
                <ManufacturerList>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                </ManufacturerList>
            </Product>
        </ProductList>
    
      <!-- Grouping is done on taxability of product-->
        <TaxabilityList>
            <Taxability>
                <Value></Value>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                </ProductList>
            </Taxability>
            <Taxability>
                <Value></Value>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                </ProductList>
            </Taxability>
        </TaxabilityList>
    
      <!-- Grouping is done on manufacterur-->
        <ManufacturerList>
            <Manufacturer>
                <Name></Name>
                <City></City>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                </ProductList>
            </Manufacturer>
            <Manufacturer>
                <Name></Name>
                <City></City>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                </ProductList>
            </Manufacturer>
        </ManufacturerList>
    </ResponseStruct>
    
    
    
    现在我用以下方法解决了这个问题: 首先,我为我的回答确定了各个模型类

    1.  Product
    -   Name
    -   Taxability
    -   IList<Manufacturer>
    2.  Taxability
    -   Value
    -   IList<Product>
    3.  Manufacturer
    -   Name
    -   City
    -   IList<Product>
    
    1。产品
    -名字
    -可征税性
    -伊里斯特
    2.可征税性
    -价值观
    -伊里斯特
    3.制造商
    -名字
    -城市
    -伊里斯特
    
    对于用例1(默认分组),我将ManufacturerList和TaxabilityList标记都保留为关闭状态以响应,并仅从数据库填充ProductList对象。对于用例2(基于制造商的分组),我将关闭ProductProductList和TaxabilityList作为响应,并且只填充ManufacturerList对象

    用例3的示例响应:

    
    ...
    
    需要帮助:

    <ResponseStruct>
      <!-- Grouping is done on product-->
        <ProductList>
            <Product>
                <Name></Name>
                <Taxability>
                    <Value></Value>
                </Taxability>
                <ManufacturerList>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                </ManufacturerList>
            </Product>
            <Product>
                <Name></Name>
                <Taxability>
                    <Value></Value>
                </Taxability>
                <ManufacturerList>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                    <Manufacturer>
                        <Name></Name>
                        <City></City>
                    </Manufacturer>
                </ManufacturerList>
            </Product>
        </ProductList>
    
      <!-- Grouping is done on taxability of product-->
        <TaxabilityList>
            <Taxability>
                <Value></Value>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                </ProductList>
            </Taxability>
            <Taxability>
                <Value></Value>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                    <Product>
                        <Name></Name>
                        <ManufacturerList>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                            <Manufacturer>
                                <Name></Name>
                                <City></City>
                            </Manufacturer>
                        </ManufacturerList>
                    </Product>
                </ProductList>
            </Taxability>
        </TaxabilityList>
    
      <!-- Grouping is done on manufacterur-->
        <ManufacturerList>
            <Manufacturer>
                <Name></Name>
                <City></City>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                </ProductList>
            </Manufacturer>
            <Manufacturer>
                <Name></Name>
                <City></City>
                <ProductList>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                    <Product>
                        <Name></Name>
                        <Taxability>
                            <Value></Value>
                        </Taxability>
                    </Product>
                </ProductList>
            </Manufacturer>
        </ManufacturerList>
    </ResponseStruct>
    
    上面是我创建的示例,它演示了我的问题。实际上,有5个分组选项,用户输入请求可以有2级分组(他可以在请求中以分组对象数组的形式发送),这将导致20种可能的响应格式

    我的实现有一个问题。我的身体有三层 服务,当我收到输入时,我在查看 输入验证层下输入请求中的分组属性 稍后由业务层使用,我在其中格式化基于响应的分组 属性,这变得很混乱,因为有很多“如果” 语句,所以我创建了不同的响应格式化程序类 实现一个接口,但我担心我创建了太多的接口 类,因为我必须构建多种响应格式(20)

    是否有任何主题的设计模式或任何我可以阅读的建议来解决我的问题


    我可能以完全错误的方式处理我的问题,因此我将感谢任何类型的解决方案。

    我认为这个问题的复杂性之一在于每个组表示具有不同的属性。例如,当你按制造商分组时,你会有分组者的名称/地址,但当你按可征税性分组时,你会有值。我考虑了一下,我将提出的解决方案很可能会导致轻微的UI更改,但它可能会为您提供一些类来处理此类数据

    首先,从现在开始,一切都是
    GroupedCollection
    。如果用户没有选择任何内容,这并不重要,它仍然是按“无”分组的。我们的
    GroupedCollection
    类应该是集合的集合

    内部集合将包含详细信息-外部集合将只是集合

    因此,要将其放入代码中,它将如下所示

    // This class is responsible from carrying information + items belonging to a single group.
    public class SingleGroupCollection<N> : Collection<N>
        {
            //
            // The property bag is where you carry the per-group data.
            // Like your grouped-by Manufacturer <"name", "G. Manufacturer">
            // You should expose methods so you could retrieve all of its 
            // keys-values and display the group-specific data in your UI.
    
            Dictionary<string, string> PropertyBag;
            public SingleGroupCollection()
            {
                 // Classic .Add() / .Remove() methods should work since 
                 //   we inherit from a Collection.
            }
        }
    
    //此类负责携带属于单个组的信息+项。
    公共类SingleGroupCollection:集合
    {
    //
    //属性包是您携带每组数据的地方。
    //就像你的产品一样
    
    /*
        This class will hold all of the data that you're tranferring across.
        Depending on the grouping type - which I've imagined to be an enum, 
        you can set to be only 1 group / vs multiple groups,
        fill the class as you see fit with the information that you've retrieved from the DB.
     */
    public class GroupedCollection<N> : Collection<SingleGroupCollection<N>>
        {
            public GroupedCollection()
            {
                // default
            }
    
            public GroupedCollection(GroupingType[] type)
            {
                // set types
                // set collection
            }
        }
    
    interface MyService
    {
        IList<Product> GetProducts();
        IList<Group<Product>> GetProductsGroupedByTaxability();
        IList<Group<Product>> GetProductsGroupedByManufacturer();
    }
    
    class Group<T>
    {
        public String Name { get; set; }
        public IList<T> Items { get; set; }
    }
    
    interface MyService
    {
        IList<Product> GetProducts();
        IList<Group<Product>> GetGroupedProducts(Field level1);
        IList<Group<IList<Group<Product>>> GetGroupedProducts(Field level1, Field level2);
    }
    
    enum Field { Manufacturer, Taxability }
    
    <Groups>
        <Group>
            <Name>Massey Fergusson</Name>
            <Items>
                <Product>...</Product>
                <Product>...</Product>
                <Product>...</Product>
            </Items>
        </Group>
        <Group>
            <Name>John Deere</Name>
            <Items>
                <Product>...</Product>
                <Product>...</Product>
                <Product>...</Product>
            </Items>
        </Group>
    </Groups>
    
    <Groups>
        <Group>
            <Name>Massey Fergusson</Name>
            <Items>
                <Group>
                    <Name>20%</Name>
                    <Items>
                        <Product>...</Product>
                        <Product>...</Product>
                    </Items>
                </Group>
                <Group>
                    <Name>0%</Name>
                    <Items>
                        <Product>...</Product>
                        <Product>...</Product>
                    </Items>
                </Group>
            </Items>
        </Group>
        <Group>
            <Name>John Deere</Name>
            <Items>
                ...
            </Items>
        </Group>
    </Groups>