0

I have several classes that implement something like an IAdd interface/class:

public class AddClass<T> where T : ISomething
{
   //Calculated Properties
   //Member Variables

   public Add(T t)
   {
       //do stuff thats complicated
   }
}

Now in some instances the t in Add(T t) just needs to be a simple struct with basically input variables; something like:

int ID;
string Name;
int Age;

From there I do some complicated stuff within the Add() method, but once I have 3 three inputs, I don't need anything else. I can probably just use a struct that implements ISomething. Is there a design pattern or anything that relates to this? Do I declare the struct within AddClass? What do I name it? Is there a standard approach for something like this?

5
  • 3
    ... What is your question? From what I can gather, you are asking "can I do these things?" The answer is yes. Whether or not it's a good idea to do those things depends on specifics that you have not mentioned. Tell us what you're actually trying to do instead of using placeholders like ISomething. Commented Aug 16, 2011 at 2:02
  • 1
    I don't understand what the goal is here. Commented Aug 16, 2011 at 2:24
  • I asked 4 questions at the end...Is there a design pattern or anything that relates to this? Do I declare the struct within AddClass? What do I name it? Is there a standard approach for something like this? Commented Aug 16, 2011 at 2:25
  • I dont think there is any design pattern just for a struct usage in generics. And are you sure that this struct wont be used else where? Because if you embed this inside a class, it has become context of that class only. So the whole meaning and what it represent changes. So think about it. Commented Aug 16, 2011 at 3:07
  • I quess you are saying you want a generic solution for your class AddClass to accept different types of structs? Cant you just use public Add(ISomething item) instead of some generic solution? Generics and different value types is not really a good idea to begin with Commented Aug 16, 2011 at 3:08

2 Answers 2

1

t in this case would be considered a Parameter Object.

As for how you get an instance of ISomething, a DefaultSomething (either class or struct is fine, you probably want a class unless you have a good reason) is pretty normal to have. If the DefaultSomething is meant to only be used in the context of AddClass you could nest it, otherwise I would declare it on its own.

Sign up to request clarification or add additional context in comments.

Comments

0

There are no type restriction which can define a struct. However, if you use the following restriction you have limited quite a lot:

 public class AddStruct<T> where T : struct, IConvertible, IFormattable, IComparable

I've blogged about it: http://blog.gauffin.org/2011/07/generic-type-restriction-for-enums/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.