0

I am trying to implement a static function in my Product class to "Get" products.

The outcome I got to work is to nest a Get subclass like this:

Product.Get.ByName("Cool Product")

But I am feeling a subclass ist not the best practice for this.

I would like to accomplish it like this what i think would be right implementation:

Product.Get().ByName(x => x.Name = "Cool Product");

How could I create this kind of sub sub method(is this even the right word?)?

josef_skywalker
  • 1,047
  • 1
  • 9
  • 16
  • Unclear what you are trying to do. Do you want to do something like the [pipes and filters pattern described here](https://stackoverflow.com/questions/679027/how-do-you-implement-pipes-and-filters-pattern-with-linqtosql-entity-framework-n)? – stephen.vakil Dec 05 '18 at 21:54
  • @stephen.vakil yes this looks like what I am thinking I want to accomplish. For example when you call a mehtod that returns a List u can directly use this List with like List.foreach() – josef_skywalker Dec 05 '18 at 22:00

2 Answers2

2

If you want to add . after get, you will need a subclass however you can make this subclass itself static:

class Product
{
     public static class Get
     {
          public static Product ByName()
          {
                //some code to return a product (or may be products)
          }
     }
}

Now it can be accessed like:

Product.Get.ByName();

Live Demo

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
1

Why do you need a Get class, is this simpler:

    public class Product
    {
        public static Product GetByName()
        {
            //some code to return a product (or maybe products)
        }
    }

Usage: Product.GetByName();

josef_skywalker
  • 1,047
  • 1
  • 9
  • 16
Xiaosu
  • 605
  • 1
  • 9
  • 21