samedi 8 décembre 2018

WCF service response design based on grouping c#

I need help with design of my WCF service. I have method which returns Products from database. On application user interface I have option to where I can see the products grouped by following option.

  1. No/Default Grouping: Method will return list of Products where each product will all contain information about all the manufacturers creating that product.

  2. Manufacturer Grouping: Now method will return list of Manufacturer where each manufacturer will contain list of products which they create.

  3. Taxability Grouping: Method will return List of Taxability object (for now only two) where each object will contain List of Products which are taxable and each Product will contain its manufacturer information.

Below is my entire response structure:

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

Right now I solved this problem in following way: First I have identified the individual Model classes for my response.

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

For use case 1(default grouping) I leave both ManufacturerList and TaxabilityList tag closed in response and fill the ProductList object only from database. For use case 2(Grouping based on manufacturer) I leave product ProductList and TaxabilityList closed in response and only fill ManufacturerList object.

Sample response for use case 3:

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

Need help:

Above is the sample I have created which demonstrate my problem. In reality there are 5 Grouping option and User input request can have 2 level grouping(he can send it in array of grouping object in request) and this results in 20 possible response format.

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).

Is there any design pattern of subject matter or any suggestion which I can read to solve my problem?

I might be approaching my problem in completely wrong way, so will appreciate any kind of solution.

Aucun commentaire:

Enregistrer un commentaire