Click here to Skip to main content
16,010,918 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all. I need some help with generics...at least that's what I believe.

My goal here is to create a generic parameters class that has ParamName, ParamValue, and ParamType. This ParamType needs to be flexible enough to accept any data type from the one of these tyeps (SQL, Oracle, and DB2).

Is this even possible? Please help :-).
C#
Public class GenericParameters
{
   public string ParamName;
   public string ParamValue;
   public ???? ParamDataType;
}


Thank you to all who respond!!!!
Posted
Updated 24-Dec-14 19:03pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Dec-14 17:32pm    
Not clear. Not clear at all. What do you want to achieve. Which types should be the generic parameters, what are their roles, why? And what is your problem? From your question, I also cannot be sure that you understand what are those generic types. Is so, you need to learn it before you try to generate any ideas or offer any requirements.
—SA
Philippe Mori 3-Jan-15 17:33pm    
This is probably not a good idea. This is error prone. You are going against the intended usage and way of doing things.

Microsoft put a lot of effort to make type safe wrapper for databases and WCF. I would recommand that you learn to use existing infrastructure correctly.
RonnieDean77 5-Jan-15 11:21am    
Hi Philippe! I understand MS efforts, but to clarify; I am trying to write a WCF generic data layer that can accept any parameter data type. This is so the client only has to fill in ONE, generic, parameter object and not chose from 5-10+ different typed parameter objects. In the WCF service I will determine which one was sent and handle accordingly. I don't feel that this, in any way, is considered incorrect. I do appreciate your comments. Have a great new year!

if you trying to build your own data access class or layer you better check below article firstA Look at Dapper.NET[^]
Dapper
Quote:
is a single file[^] you can drop in to your project that will extend your IDbConnection interface.

you can check the code for how they handle parameters as well. hope this helps.
 
Share this answer
 
Thank you DamithSL for the potential solution! I reviewed the Dapper code and was able to get some ideas on how to move forward.

I ended up creating the parameter data type as an object. The client can set this to any data type (i.e. SqlDbType, OracleDbType, Db2Type, ect.) then on the WCF service, I check to see what type was sent and handle accordingly.


EXAMPLE:


Client:
...
(Service Client)
ParameterName = @"@Id";
ParameterType = SqlDbType.Int;
ParameterValue = 123456


WCF IService:
...
[DataContract]
public object ParameterType;


WCF Service:
...
if (dbConnection.Parameters[0].ParameterType.GetType() == typeof(SqlDbType))
{
// If parameters are included get each parameter data tyupe.
foreach (var parameter in dbConnection.Parameters)
{
if (String.IsNullOrEmpty(dbConnection.Parameters[0].ParameterValue)) return null;
var returnValue = TypeDescriptor.GetConverter((SqlDbType)parameter.ParameterType).ConvertFromString(parameter.ParameterValue);
}
}
 
Share this answer
 
Comments
Philippe Mori 3-Jan-15 17:24pm    
Put code in code blocks...

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