VirtoCommerce/vc-platform

Add OverrideType version that accepts types through regular parameters.

j-mok opened this issue · 0 comments

j-mok commented

Problem:

AbstractTypeFactory.OverrideType uses generic parameters for both the overriding and overridden types which forces the caller to literally embed both types in the call and prevents scenarios where the old type (more likely) or both types are only referenced as Type objects. Workarounds include reflection or compiled lambda expressions, both an overkill.

Current signature:

public static TypeInfo<BaseType> OverrideType<OldType, NewType>() where NewType : BaseType

Solution:
Introduce and overload that either accepts old type as a Type parameter:

public static TypeInfo<BaseType> OverrideType<NewType>(Type oldType) where NewType : BaseType
{
    ...
}

or both old and new type:

public static TypeInfo<BaseType> OverrideType(Type oldType, Type newType)
{
    ...
}

In the latter case the where NewType : BaseType restriction would have to be replaced with a runtime check.

In both cases it doesn't actually make sense to pass OldType as generic param as it is immediately converted to a Type object anyway and not used in any other way whatsoever. NewType generic param has a little more sense as it is a part of the generic constraint, but I wouldn't consider that essential. Generally passing types as generic parameters when they are not used in method input or output is a code smell and it limits the ways the method can be called.