Skip to content

Please expose DynamicParameters.parameters #849

Open

Description

@zhangleixp

My scenario :
Logging the database access, including parameters infos, like this:

// business logic layer
public class DemoBLL
{
  public void M1(p1, p2, p3)
  {
    DynamicParameters dynPars = new DynamicParameters();
    if(...)
      dynPars.Add(parname1, parvalue1);
    if(...)
      dynPars.Add(parname2, parvalue2);

    QueryL(conn.Query(sql, dynPars,...); 
  }

  public void M2(p4, ...)
  {
    DynamicParameters dynPars = new DynamicParameters();
    if(...)
      dynPars.Add(...);
    if(...)
      dynPars.Add(...);

    QueryL(conn.Query(sql, dynPars,...); 
  }
}

// data access layer
public class DemoDAL
{
  public void QueryL(sql, dynPars,...)
  {
    // log the query infos
    logger.Info("Sql Text:" + sql);
    
    // I wish i could do like this
    foreach (var dp in dynPars.Parameters) // but there's no public 'Parameters' member, so these code won't effect
    {
      sb.AppendFormat("{{Name:{0}, Type:{1}, Value:{2}, Direction:{3}}}, ", 
          dp.Value.Name, dp.Value.DbType, 
          Convert.ToString(dp.Value.Value), 
          dp.Value.ParameterDirection);
    }
    
    conn.Query(sql, dynPars)
  }
}

I hope if we could foreach DynamicParameters.parameters, that will be very very useful!

Another scenarios:
#304

Activity

  1. mgravell commented on Sep 24, 2017

    @mgravell
    Member
  2. etmendz commented on Sep 24, 2017

    @etmendz

    I have the same issue actually. Just like how I can use DbParameterCollection to loop through the list of parameters, it would be nice if I can also loop through the DynamicParameters' collection of parameters. At the very least, please make it iterable for readonly access. My use case is for logging and/or messaging purposes (ex. constructing messages that includes raw input and output values from a stored procedure call). Thanks!

  3. zhangleixp commented on Sep 25, 2017

    @zhangleixp
    Author

    @mgravell

    Thanks for for reply.

    Making DynamicParameters.parameters iterable is enough for my scenario, and same as @etmendz , just readonly access.

    BTW, DynamicParameters is a very useful. The short of anonymous types is that once it is created it will be hard to add more fields.

  4. added this to the milestone on May 21, 2018
  5. Dana2917 commented on Sep 14, 2020

    @Dana2917

    To me that strongly suggests you're over-using the dynamic parameters... It is far more efficient to use simple argument types (usually anonymous types) than dynamic parameters.

    @mgravell : "More efficient" meaning "less typing" or "executes faster"? The terse style of using an anonymous for parameters looks very nice... but in my experience there is rarely an occasion to use it, because you need DynamicParameters just to check the return value.

    I would also like to be able to iterate parameters in DynamicParameters for logging with type (including string vs. ansi string), size, and direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions