Create a parameter mapper for more than 5-6 parameters

Topics: Data Access Application Block
Jun 14, 2013 at 2:11 PM
Hi,
Can you pls send a sample of how to create a Parameter mapper which need more than 5-6 parameters also, If wanted to know how can you pass an entire object which has more than 10 properties as parameters ,we have a reqt to filter based on 10-15 properties.is it possible via Parameter mapper ?pls suggest

Kind Regards
Krishna
Jun 14, 2013 at 2:37 PM
A Parameter Mapper is just an implementation of IParameterMapper:
    public interface IParameterMapper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameterValues"></param>
        void AssignParameters(DbCommand command, object[] parameterValues);
    }
Here is the default implementation used by Enterprise Library.
private class DefaultParameterMapper : IParameterMapper
{
    readonly Database database;
    public DefaultParameterMapper(Database database)
    {
        this.database = database;
    }

    public void AssignParameters(DbCommand command, object[] parameterValues)
    {
        if (parameterValues.Length > 0)
        {
            GuardParameterDiscoverySupported();
            database.AssignParameters(command, parameterValues);
        }
    }

    private void GuardParameterDiscoverySupported()
    {
        if (!database.SupportsParemeterDiscovery)
        {
            throw new InvalidOperationException(
                string.Format(CultureInfo.CurrentCulture,
                                Resources.ExceptionParameterDiscoveryNotSupported,
                                database.GetType().FullName));
        }
    }
}
Since the interface is so simple there isn't much that can be done except set the parameters based on parameter order without writing some custom Accessor code. Passing the parameters in the correct order means that you don't need to create a custom IParameterMapper.

Here is an example of a custom parameter mapper to use named parameters (which are optional) along with some accessor code: http://entlib.codeplex.com/discussions/443904.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Jun 14, 2013 at 2:49 PM
Thanks for your prompt reply.
var results2 = db.ExecuteSprocAccessor<Product>("ABC", paramMapper, rowMapper);
I have an object like Person which has ' ID, Name, Country,state, pincode etc etc .
You mean we just need to mention this ,
ParamMapper = new ParameterMapper();

How willl the object[] params get constructed. I guess I should be assiging all the properties like person.Id = '1' ,person.Name= 'ABC' , etc..where do we do that and how will parameter mapper knows about it , Should we pass the parameters in the same order as present in storedproc , should they have the same name and type?

Regards
Krishna
Jun 14, 2013 at 4:55 PM
The easiest way is to not use a custom parameter mapper (it's rarely required) and use the default parameter mapper:
var results2 = db.ExecuteSprocAccessor<Product>("ABC", person.ID, person.Name, person.Country, person.State);
You pass in the parameters directly without using the IParameterMapper overload. The order of the parameters passed in must be the same order as the stored procedure accepts and the object types should be compatible with the stored procedure's SQL Server Types.

If you need a custom row mapper then create the mapper:
IRowMapper<Product> rowMapper = MapBuilder<Product>.MapAllProperties() 
                    .DoNotMap (x => x.PrivateVendorId)
                    .Build();

var results2 = db.ExecuteSprocAccessor<Product>("ABC", rowMapper, person.ID, person.Name, person.Country, person.State);

See Returning Data as Objects for Client Side Querying and the related topics in that section for more information and examples.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to