Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i refactor this code?

i have .net 3.5 and i would like to make a generic method. how do i refactor this code?

            case (int)Enums.SandwichesHoagies.Cheeses:
                if (this.Cheeses.Where(x => x.Id == product.ProductId).SingleOrDefault() == null)
                {
                    var newCheese = new Cheese
                    {
                        Id = product.ProductId,
                        Name = product.Name,
                        PriceValue = product.Price.HasValue ? (double)product.Price.Value : 0.00
                    };

                    this.Cheeses.Add(newCheese);
                }
                else
                {
                    foreach (var cheese in this.Cheeses.Where(cheese => cheese.Id == product.ProductId))
                    {
                        this.Cheeses.Remove(cheese);
                        break;
                    }
                }

                foreach (var cheese in Cheeses) cheese.Type = string.Empty;

                if (this.Cheeses.Count > 0) Cheeses.First().Type = "Cheeses:";

                break;

            case (int)Enums.SandwichesHoagies.Meats:
                if (this.Meats.Where(x => x.Id == product.ProductId).SingleOrDefault() == null)
                {
                    var newMeat = new Meat
                    {
                        Id = product.ProductId,
                        Name = product.Name,
                        PriceValue = product.Price.HasValue ? (double)product.Price.Value : 0.00
                    };

                    this.Meats.Add(newMeat);
                }
                else
                {
                    foreach (var meat in this.Meats.Where(meat => meat.Id == product.ProductId))
                    {
                        this.Meats.Remove(meat);
                        break;
                    }
                }

                foreach (var meat in Meats) meat.Type = string.Empty;

                if (this.Meats.Count > 0) Meats.First().Type = "Meats:";

                break;
like image 964
CurlyFro Avatar asked Jul 12 '26 05:07

CurlyFro


1 Answers

Assuming two things:

  1. Meat and Cheese inherit from Ingredient or implement IIngredient
  2. The Meats and Cheeses collections are IList<T>

Here we go:

private void OuterMethod()
{
   switch(something)
   {
       case (int)Enums.SandwichesHoagies.Cheeses:
           HandleCase(product, this.Cheeses);
           break;
       case (int)Enums.SandwichesHoagies.Meats:
           HandleCase(product, this.Meats);
           break;
   }
}

private void HandleCase<T>(Product product, List<T> list) where T : Ingredient, new()
{
    if(list.Any(i => i.Id == product.ProductId))
    {
        list.Add(new T {
            Id = product.ProductId,
            Name = product.Name,
            PriceValue = product.PriceValue ?? 0.0;
        });
    }
    else
    {
        list.RemoveAll(i => i.Id == product.ProductId);
    }

    //NOTE: this part seems like a bad idea. looks like code smell.
    foreach (var i in list)
    {
        i.Type = string.Empty;
    }
    if (list.Count > 0)
    {
        list.First().Type = "Cheeses:";
    }
}
like image 143
Rex M Avatar answered Jul 14 '26 17:07

Rex M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!