Link Search Menu Expand Document

Average Type Resolver


This object is used within the BaseStatementBuilder class as a resolver of which system type to be used for conversion when calling the Average and AverageAll operations.

To implement, simply create a class that implements the IResolver.

public class AverageableTypeResolver : IResolver<Type, Type>
{
    ...
}

Then, implement the Resolve() method like below.

public Type Resolve(Type type)
{
    if (type == null)
    {
        throw new NullReferenceException("The type must not be null.");
    }

    // Get the type
    type = type?.GetUnderlyingType();

    // Only convert those numerics
    if (type == typeof(short) ||
        type == typeof(int) ||
        type == typeof(long) ||
        type == typeof(UInt16) ||
        type == typeof(UInt32) ||
        type == typeof(UInt64))
    {
        type = typeof(double);
    }

    // Return the type
    return type;
}

Once implemented, simply pass it when inheritting the BaseStatementBuilder class.

internal sealed class OptimizedSqlServerStatementBuilder : BaseStatementBuilder
{
	public OptimizedSqlServerStatementBuilder()
        : base(DbSettingMapper.Get(typeof(SqlConnection)),
            null, // See the ConvertFieldResolver page
            new AverageableTypeResolver())
	{ }
}

Please see the Statement Builder for more information. In addition to this note, there is already an existing resolver that does the same named RepoDb.Resolvers.ClientTypeToAverageableClientTypeResolver. It can be used and inheritted immediately.