How to handle a massive factory in a cleaner way

My development team ran into a design problem. I hope someone can help me clear this part of the architecture a bit.

In my system, I have an enumeration with 250 members [one member is a separate snapshot]. To fill in the drop-down lists in any window, this form sends enumeration elements that are associated with the necessary drop-down lists to the items and the drop-down information is returned.

In other words, say, for example, we have 3 windows. Window A has drop-downs X, Y and Z. Window B has drop-down lists W, X and Y, and window C has drop-down lists T, U and W. My DropDownType enumeration will consist of T, U, W, X, Y, Y, and Z. Thus, for the specified window, given the pop-ups in this window, I request that the data appear in these drop-down lists.

This is a simplified example because my application consists of> 250 separate drop-down lists.

As you can imagine, I have a factory setting to return data for each drop-down list. And this factory is called down for each requested request.

    switch (dropDownType)
    {
        case DropDownType.T:
            return (from t in dataContext.GetTable<TableOne>() 
                    select new DropDownDto 
                               { 
                                   DropDownDisplayName = t.ColumnA,
                                   DropDownValue = t.ColumnB
                               }).ToList();
        case DropDownType.U:
            return (from u in dataContext.GetTable<TableTwo>() 
                    select new DropDownDto 
                               { 
                                   DropDownDisplayName = u.ColumnC,
                                   DropDownValue = u.ColumnD
                               }).ToList();
        // etc...
    }

, - ? , factory ( 250 ...)? , ? HUGE .

. !

+3
4

Dictionary<DropDownType, DropDownDtoDelegate> , , . , switch. , .

+4

. , / DTO. "" , , .

0

DynamicMethod . .

Entity, , 2 .

I can imagine something like this.

enum DropDownType
{
   [Configuration(Type = typeof(TableOne), DisplayNameProperty = "ColumnA", ValueProperty = "ColumnB")]
   T,

   [Configuration(Type = typeof(TableTwo), DisplayNameProperty = "ColumnC", ValueProperty = "ColumnD")]
   U
}

When you have an enumeration value, you check the dictionary for the cached method; if it is absent, you create it.

Using reflection, you get information about properties and with a small amount of IL skills, this can be done easily.

0
source

Source: https://habr.com/ru/post/1705065/


All Articles