Home > Mobile >  Encapsulating Parameters and Arguments in classes
Encapsulating Parameters and Arguments in classes

Time:12-10

I have sort of an open question, but I have tried quite a few solution and none of them are really satisfactory I would say so hopefully someone here has an idea of whether I am missing some solution.

As you can see below I have a class ExposedMethod, which defines a method that can be called (Just to clarify, it is not an actual C# method, it is a class representing some operation or task). This method has any number of potential parameters (all parameter types currently inherits from baseclass parameter which just has a ToString() and Validate() method. enter image description here

All that should be fine. My problem arises when you look at the bottom of the drawing where I have also encapsulated a call to a ExposedMethod using MethodCall. I want this call to contain the values that should be provided for the parameters defined in the ExposedMethod. I don't how to make sure that the provided Call types matches the types of the parameters. Say that I have an ExposedMethod that has 2 parameters Name and Id of type String ParamType and Int ParamType respectively. How do I design my MethodCall class so that the value for Name can only be provided as a string at compile time (I really want to avoid having to do the checks manually at runtime).

So far I have tried:

  1. Making ParameterDefinition Generic (so ParameterDefinition where T : ParamType, but that, afaik, requires me to have N different lists in Exposed Method (one for each ParamType) which is kind of ugly if I have to add more types at some point. This however seems to be the "easiest" approach.
  2. Having no generics but simply having a ParamType in ParameterDefinition and then a Dictionary of <int, ParameterValue> in MethodCall, where the key is the id of the parameter I want to set a value for and ParameterValue is the actual value. This however requires me to check manually that the ParameterValue actually has the correct type.

I realize there probably isn't a great solution for this, but the concept of ExposedMethod and MethodCall is such an integral part of my design so I want to have as few nasty surprises down the line as possible.

Hopefully someone has had experience with something similar and has an idea of how to ensure that params and args matches or at least can tell me if I am going completely in the wrong direction :)

CodePudding user response:

This however requires me to check manually that the ParameterValue actually has the correct type

That's what you're going to need to do in the end anyway. For any "MethodCall" instance, if you don't use inheritance (like "FooStringIntMethodCall" representing a "Foo" method with a string and int parameter), the compiler doesn't know that the first "ParameterValue" has to be a StringParameter.

Just that your domain happens to represent something that a compiler (/lexer/parser) knows about, doesn't mean the compiler can help you with your domain.

  • Related