gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:20:00 24 4
gpt4 key购买 nike

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

  1. 无/默认分组:返回所有产品的列表。

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

  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>

现在我通过以下方式解决了这个问题:首先,我为我的响应确定了各个模型类。

1.  Product
- Name
- Taxability
- IList<Manufacturer>
2. Taxability
- Value
- IList<Product>
3. Manufacturer
- Name
- City
- IList<Product>

对于用例 1(默认分组),我将 ManufacturerList 和 TaxabilityList 标签都关闭以作为响应并仅从数据库中填充 ProductList 对象。对于用例 2(基于制造商的分组),我将产品 ProductList 和 TaxabilityList 关闭作为响应并仅填充 ManufacturerList 对象。

用例 3 的示例响应:

<ResponseStruct>
<ProductList/>
<ManufacturerList/>
<TaxabilityList>...</TaxabilityList>
</ResponseStruct>

需要帮助:

上面是我创建的示例,它演示了我的问题。实际上有 5 个分组选项,用户输入请求可以有 2 级分组(他可以在请求中将其发送到分组对象数组中),这导致 20 种可能的响应格式。

There is an issue with my implementation. I have three layers in my service and when I receive input, I set a flag after looking at grouping attribute in input request under input validation layer which is later used by business layer where I format response based grouping attribute from input, this was getting messy because of lot ‘if’ statement, so I created different response formatter classes which implements an interface but my concern is that I have created too many classes because I have to build multiple response formats(20).

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

我可能会以完全错误的方式解决我的问题,因此将不胜感激任何一种解决方案。

最佳答案

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

首先,从现在开始一切都是GroupedCollection .如果用户没有选择任何东西也没关系,它仍然是 grouped by nothing。我们的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<Product> class - 我们需要一些东西来容纳所有的组。我们可以创建下面的类:

/*
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
}
}

这样你就可以扩展/重用 GroupedCollection类,同时支持多个 GroupingTypes,并且仍然具有单一类型的响应。

你怎么看?

关于c# - 基于分组的WCF服务响应设计c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53684218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com