I want to raise a little problem with the code you posted:
public static class DatabaseUtility { public static Read<T>( MySqlConnection conn, string query, Func<MySqlDataReader, T> callback); }
the method has no reverse signature! is it emptiness? If this is not valid, the code you posted will not work:
ErrorCode errorCode; errorCode = DatabaseUtility.Read<ErrorCode>( conn, DatabaseUtility.CreateSelectQuery(....), reader => { if(reader.Read()) return ErrorCode.None; return ErrorCode.InvalidParam; });
if it is already T, then there is no need to declare Func to pass the Read function.
I think that in this case the return value of the Read function should be T, and given that you want to perform the action with the data type that you are retrieving (in case of some error or for some other case), it would be better to pass the action instead of this:
public static class DatabaseUtility { public static T Read<T>( MySqlConnection conn, string query, Action<MySqlDataReader> callback); }
Assume that performance wise should not be a problem unless you are targeting a real-time application (which I think is not your business). However, that would be the best IMHO solution.
hope this helps.
source share