Need help choosing a design template

I currently have an if else command to set the CategoryId based on the number of items in each collection.

For instance,

public class TeamWork
{
    public string EmployeeName { get; set; }
    public int CategoryId { get; set; }
}

public class BLL
{
    public void SetCategoryId(ICollection<TeamWork> Converted, ICollection<TeamWork> Sourced)
    {
        if (Converted.Count == 1 && Sourced.Count == 1)
        {                
            if (String.Compare(Sourced.First().EmployeeName, Converted.First().EmployeeName) == 0)
            {
                // set category id to 1
                Converted.First().CategoryId = 1;
                Sourced.First().CategoryId = 1;                                            
            }
            else
            {
                // set category id to something                  
            }
        }
        else if (Sourced.Rows.Count == 1 && Converted.Rows.Count > 1)
        {
            // set category id to something           
        }
        // more if else statements...
    }
}

I think the best way to do this is perhaps by applying some design pattern. Any suggestions? Thank you

+3
source share
2 answers

The chain of responsibility is the way to go.

Thus, this object is transferred to a number of objects of the team until you can act and establish the status.

+4
source

. " , - ". , List<Func<ICollection<TeamWork>, ICollection<TeamWork>, bool>> . SetCategoryId() :

public void SetCategoryId(ICollection<TeamWork> Converted, ICollection<TeamWork> Sourced)
{
    foreach(var categoryRule in CategoryRules)
    {
       var category = test(Converted, Sourced);
       if(category != 0)
       {
          Converted.First().CategoryId = Sourced.First().CategoryId = category;
          break;
       }
    }
}

, . , if-else if , , , , .

+1

All Articles