2
\$\begingroup\$

I am trying to refactor a service by providing some options on what to bring back. I can't decide what is best way to go between the below

A. Use a class for the options Create a new class for the options like

    public class Options
    {
        public bool Addresses { get; set; }
        public bool Documents { get; set; }
    }

and call the services as below

    public List<Entity> GetEntitiesByOptions(List<int> entityIds, Options options)
    {
        if (options.Addresses)
        {
            // Add the addresses
        }

        if (options.Documents)
        {
            // Add the Documents
        }
    }

or B. Use the type of class as a parameter

and use with something like that

    public List<Entity> GetEntitiesByOptions(List<int> entityIds, List<Type> options)
    {
        if (options.Any(x => x == typeof(Address)))
        {
            // Add the addresses
        }

        if (options.Any(x => x == typeof(Document)))
        {
            // Add the Documents
        }
    }

Would there be a big impact in the performance if I use reflection?

\$\endgroup\$
3
  • \$\begingroup\$ Have you considered to use OneOf or Either constructs? \$\endgroup\$ Oct 6, 2020 at 8:12
  • 1
    \$\begingroup\$ @PeterCsala thanks for the suggestion. I will take a look at them. \$\endgroup\$ Oct 6, 2020 at 14:29
  • 1
    \$\begingroup\$ Looks like a regular question for StackOverflow, not Code Review. You may test the performance easily with Stopwatch and accurately with Benchmark.NET. \$\endgroup\$
    – aepot
    Oct 7, 2020 at 9:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.