Click here to Skip to main content
15,075,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently trying to implement a generic method:

public int Save<T>(DomainObject model) where T : BaseConfig
{
...
}


T is a type of class that implements BaseConfig. I need to be able to instantiate a new class of the passed type T in the Save method.

The BaseConfig class populates it's own config values upon instantiation.

So within the generic Save method I need to say something like: T config = new T()

I'm pretty sure that the instantiation code that I provided above is not the correct way to do it but I think it's a pretty good illustration of what I'm trying to achieve.

So can you advise on the best way to implement this design?
Posted

It is not a good way of using generic. In your sample there is not use of T in the class implementation, but this is a key. If you do it correctly, type case is not needed.

There are many design way to achieve the results. In some cases you need to combine OOP with generics. Some ideas:

You can also have generic methods in DomainObject, then you will instantiate those methods with T type.

As a variant of the above you can make whole DomainObject class generic and instantiate it with T.

You can make an interface IConfig instead of BaseConfig and make it known to DomainObject. Instantiation of DomainObject will define only the implementation of IConfig; DomainObject will work with interface only but agnostic about its implementation defined at the moment of generic instantiation.

If I could learn more about your application and intended design, I would advise something else, but you need to show some essential methods and explain what they are supposed to do. ("Algorithms first!")

—SA
   
Hi,
Just to add the new() constraint to the method to specify that the class has a parameterless constructor . Without it the compiler error is "Cannot create an instance of the variable type 'T' because it does not have the new() constraint".
C#
public int Save<T>(DomainObject model) where T : BaseConfig, new() {
  T config = new T();
  return 0;
}


Alan.

[EDIT] have I understood the question correctly?
   
v2
Will this work inside your method?

C#
T realType = model as T;


EDIT ==========

I changed "obj" to "model".
   
v2
Comments
Member 3919049 2-Feb-11 15:05pm
   
Thanks John - Your code above isn't going to work as-is in my method in the way my method is defined above. If I copy and paste your code into my method above the compiler returns the following error:

The name 'obj' does not exist in the current context

Are you suggesting that I pass in an instance of the BaseConfig implementor as a method parameter instead of just the type spec?

I would rather not require the client to pass in an instance of the BaseConfig implementor if I can dynamically instantiate one in this method based on the T type...
#realJSOP 2-Feb-11 15:23pm
   
It was an example. Your code will most likely be different. Try "model" instead of "obj"...
Member 3919049 2-Feb-11 16:02pm
   
Hi John - Maybe I am using this generic method incorrectly.

The T represents a config class and the model is a domain object to be saved. So T and the model parameter represent 2 different things.

The T class stores a mapping of Model property names to database column names so my Repository knows which columns of the underlying database table the model property values should be set to when a database saved is performed.

The T class can be dynamically instantiated because all of the config information is populated within its constructor.

Is this a legitimate use of a generic method? If so then please let me know if dynamic instantiation is possible here and the best way to do it. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900