Home > front end >  How to pass a anonymous object into a generic function?
How to pass a anonymous object into a generic function?

Time:02-28

Going from on of Tim Corey's ASP.NET and dapper tutorials, I have adapted a generic function to store data in a SQL Server database to return the ID of the stored object:

public async Task<T> SaveData<T, U>(
    string storedProcedure,
    U parameters,
    string connectionId = "DefaultDataConnection")
{
    using IDbConnection connection = new SqlConnection(_configuration.GetConnectionString(connectionId));

    T ID = await connection.ExecuteScalarAsync<T>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);

    return ID;
}

The question is the following: in the original code from the tutorials the method can be called without declaring the type of the generic parameter. However with the additional generic return type, this is no necessary. The problem is, that these methods are usually called with anonymous type objects and I don't know how to call the method:

// this is within the insert method of my class, which is responsible for handling the images table:
int insertedID = await _db.SaveData<int, ????>("dbo.spImage_Insert", new { image.UserID, image.FileName, image.UploadDate });

How should the be done? Do I have to use an explicit type instead of the anonymous object?

Edit: the reason the return value is generic, because the ID can be for example an int, long or a string GUID.

CodePudding user response:

I have to agree with the comments saying "just use object for the parameters here", but; in the more general case:


To do what you want here with anonymous objects, you'd need to split the API in two; for example, instead of SaveData<T, U> consider:

int x = Save(new { image.UserID, image.FileName, image.UploadDate })
          .Expect<int>();

which could be implemented as:

NamingIsHard<T> Save<T>(T x);

with

public readonly struct NamingIsHard<T>
{
    T theT; // and other things; possibly the connection, etc
    // ...
    public U Expect<U>() {
       // here you have a T and a U; do your thing!
    }
}
  • Related