0

In my C#, MVVM application, I took over code that is generating a class object. Given the requirement that I was handed, I might need to create multiple objects based on a value. So I decided to refactor class generation by using the factory pattern and that worked for that requirement. As I proceeded in the task more requirement have come up, which includes creating an object not only by the original string variable but also by a boolean variable. In addition, to those variables, a parameter needs to be passed into the object, which I think I should do by dependency injection. My question is should I be using the Factory pattern at this point or something else. How do I incorporate the boolean variable and the parameter in whatever recommended design pattern. The code example of what I am using is below.

 public abstract class PivotRequestFactory
 {
    public abstract PivotRequest GetPivotRequest();
 }

public class ConcretePivotRequestFactory : PivotRequestFactory
{

    public ConcretePivotRequestFactory()
    {

    }
    public override PivotRequest GetPivotRequest()
    {
        try
        {

            return new PivotRequest();
        }
        catch (Exception ex)
        {

            throw;
        }
    }
}

private PivotRequest GetPivotRequest(string DataSetName)
    {
        PivotRequestFactory factory = null;
        try
        {
            switch (DataSetName)
            {
                case DatasetNames.Default:
                    if (IsTrue) { factory = new ConcretePivotRequestFactory(); }
                    else { factory = new ConcretePivotRequestFactory2); }
                    break;
                case DatasetNames.SoSo:
                    if (IsTrue) { factory = new ConcretePivotRequestFactory3(); }
                    else { factory = new ConcretePivotRequestFactory4(); }
                    break;
                default:
                    break;
            }
        }
        catch (Exception)
        {

            throw;
        }
        return factory.GetPivotRequest();
    }
AC25
  • 413
  • 1
  • 7
  • 23
  • Just curious: what is idea behind catching an exception and re-throwing it again? – Fabio May 16 '19 at 20:27
  • What does `GetPivotRequest(string)` belong to, in your example it is just floating there alone as a private method? – Ryan Wilson May 16 '19 at 20:27
  • There's nothing wrong with using or not using a factory or even violating a given pattern in some way, in fact that's the wrong question altogether. The right question is: will this make my code easier to understand? NEVER USE A PATTERN JUST TO USE A PATTERN!!! – Display name May 16 '19 at 20:36
  • @Fabio I am asking about what pattern I should use the exception has nothing to do with this, I put in a try catch and didnt finish it out – AC25 May 16 '19 at 20:37
  • 1
    @Stefan I know the boolean is a bit odd that is why I am asking the question – AC25 May 16 '19 at 20:38
  • @Ryan Wilson GetPivotRequest is what I call to generate the object – AC25 May 16 '19 at 20:39
  • @DisplayName: you can shout it, and it's true, but mostly I hear this as an excuse to "never use a pattern" which is awfull. Use of patterns should be encouraged. It's better to use them, fail and learn then to be afraid of them and not implement them when you should. – Stefan May 16 '19 at 20:56
  • 1
    @Stefan no thats not possible – AC25 May 16 '19 at 20:59
  • Another thought, some IoC containers are able to use named registrations; this would give you factory behaviour and IoC/DI in one single kind of abstraction. – Stefan May 16 '19 at 21:00
  • @Stefan we'll just have to disagree. For simple problems use simple strategies. Adding in code that does not benefit readability or maintenance should always be suspect. However, if one needs a factory (mostly for well used/referenced/tested objects) then go ahead. – Display name May 16 '19 at 21:02
  • 1
    _My question is should I be using the Factory pattern at this point or something else_ - it depends on how this object will be consumed. Show the code which uses object you need to create. – Fabio May 16 '19 at 21:37
  • _the exception has nothing to do with this_ - can you remove them, will help to understand code quicker ;) – Fabio May 16 '19 at 21:40
  • @AC25 It looks like you are making lots of `concretepivotrequestfactory` classes, I think the `Bridge` design pattern may help you. It can cut down on you having to make x number of concrete factory classes. (https://stackoverflow.com/questions/319728/when-do-you-use-the-bridge-pattern-how-is-it-different-from-adapter-pattern/37514779#37514779) – Ryan Wilson May 17 '19 at 12:43

0 Answers0