-
Notifications
You must be signed in to change notification settings - Fork 0
Query Parameter Mapping
Insight does a lot of magic to map objects to the parameters of a query. It takes the following steps:
- Determine the parameters of the query.
- Determine the properties of the parameter object. This includes internal and private fields and properties.
- Create a mapping between them by name.
- Generate IL to build a list of parameters from an instance of the object. This function is cached for future calls.
If the Query is a StoredProcedure call Insight will use the provider to derive the parameters and determine the name and type of each parameter. This is the most efficient method, as it allows Insight to properly convert parameters to the proper type before sending it to the database. It also allows Insight to use the proper schemas for table-valued parameters.
Otherwise, Insight looks at the text of the Query for parameters that match the parameter regex [?@:]([a-zA-Z0-9_]+)
. Therefore you can use @Param
or ?Param
syntax, depending on your database provider. These parameters are assumed to be text values and are converted at the database. Note that this can cause some conversion issues with certain non-native types.
If the parameter object is a DynamicObject (e.g. dynamic
or FastExpando
), then the available properties are any property of the DynamicObject. Note that the DynamicObject must support the IDictionary<string, object>
interface.
Otherwise, Insight uses reflection to find all of the public, internal, and private members of the parameter object. It reflects against the actual type of the object, so even if you use a variable of a "BaseClass", but pass in a value of "DerivedClass", the properties will reflect off the "DerivedClass".
The mapping is performed from an exact case-insensitive string match for the property to the parameter.
You can also override the default mapping logic. See Customizing Object Mapping.
If you pass an IEnumerable in as the parameter object, Insight will assume that you want to turn it into a table-valued parameter. So it will look at the parameter list to find a TVP, and map the enumerable to that parameter. See Identity Inserts for an example.
Insight uses dynamic IL method generation to efficiently access private properties and fields, and to provide the fastest performance.
Output Parameters - BACK || NEXT- Mapping Results to Objects
- Home
- About
- Getting Started
- Connections
- Execute
- Getting Results
- Advanced Results & Mapping
- Insert/Update Considerations
- Data Types
- Write Even Less Code
- Performance & Speed
- Other Topics
- Supported Databases
- Working with the Code