Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Method - Unknown parameters

Tags:

c#

wpf

I'm attempting to use the command pattern for the first time, and with it create a command factory, I'm following guidance from a pluralsight.com course where he implements an interface for the factory which includes a MakeCommand method.

Now my issue comes from the fact that he simply passes an array of string as the arguments for this method (his is a command line app), however my commands will use a variety of arguments of a variety of types, my plan was to use these commands to store updates to models, so if the application cannot connect to the services, the commands will be queued for when the connection comes back.

This has always been a little bit of a sticking point for me with generic interfaces, how do i handle the multitude of possible arguments?

My first thought was to pass the model itself, with a simple string argument with the command type (Delete, Update, etc) however since my models do not have any common base class or interface i'm left with a similar problem.

Am i missing something basic here?

EDIT : Example of my problem was requested.

I have a CommandFactory Interface as such

 public interface ICommandFactory
{
    string CommandName { get; }
    string Description { get; }

    ICommand MakeCommand( ..arguments.. )
}

And i have simple models such as (pure example)

public class Model1
{
   public string Name {get;set;}
   public int Age {get;set;}
}


public class Model2
{
    public DateTime Time {get;set;}
    public double Price {get;set}
}

If i wanted to create a command that for example updated a model1, i'm left wondering how the Interface's MakeCommand should look, i can't do MakeCommand(string cmdType, Model1 model) because i have multiple different models which share no common baseclass/interface

like image 627
Ben Avatar asked Nov 09 '22 07:11

Ben


1 Answers

It looks like you want the individual models to define how they can be updated. In that case, can you not pass a Function/Action from the Model to the MakeCommand?

public class Model
{
   public string Name {get;set;}
   public int Age {get;set;}
   public void UpdateModel() {...}
}

 public interface ICommandFactory
{
    string CommandName { get; }
    string Description { get; }

    ICommand MakeCommand(Action<Model>);
    ICommand MakeCommandFunc(Func<Model, bool>);
}

public class Command : ICommand
{
    Action<Model> _action;
    Command(Action<Model> action)
    {
       _action = action;
    }

    Execute()
    {
        _action();
    }
}

EDIT: As requested, use common interface class to model all classes

public interface IModel
{
    void UpdateModel();
}

public class Model1 : IModel
{
   public string Name {get;set;}
   public int Age {get;set;}

   // implement updating of the model
   public void UpdateModel() {...do model update...}
}


public class Model2 : IModel
{
    public DateTime Time {get;set;}
    public double Price {get;set}

   // 'dummy' implement updating of the model if this model does not supports updating
   public void UpdateModel() { // do nothing or throw NotImplementedException(); }
}

public interface ICommandFactory
{
    string CommandName { get; }
    string Description { get; }

    ICommand MakeCommand( IModel model );
}
like image 85
sppc42 Avatar answered Nov 15 '22 13:11

sppc42