{"record":{"id":"c8badd40f8021b15","repo":"LuckyPennySoftware/AutoMapper","slug":"type-objectconstructortype-name-does-not-imple","errorCode":null,"errorMessage":"Type '{objectConstructorType.Name}' does not implement IDestinationFactory<{SourceType.Name}, {DestinationType.Name}>","messagePattern":"Type '(.+?)' does not implement IDestinationFactory<(.+?), (.+?)>","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/TypeMap.cs","lineNumber":268,"sourceCode":"    public void IncludeBaseTypes(TypePair baseTypes)\n    {\n        CheckDifferent(baseTypes);\n        Details.IncludeBaseTypes(baseTypes);\n    }\n    public void AddBeforeMapAction(LambdaExpression beforeMap) => Details.AddBeforeMapAction(beforeMap);\n    public void AddAfterMapAction(LambdaExpression afterMap) => Details.AddAfterMapAction(afterMap);\n    public void AddValueTransformation(ValueTransformerConfiguration config) => Details.AddValueTransformation(config);\n    public void ConstructUsingServiceLocator() => CustomCtorFunction = Lambda(ServiceLocator(DestinationType));\n    public void ConstructUsingObjectConstructor(Type objectConstructorType)\n    {\n        var srcParam = Parameter(SourceType);\n        var ctxParam = Parameter(typeof(ResolutionContext));\n\n        var constructorInstance = ServiceLocator(objectConstructorType);\n        var expectedInterface = typeof(IDestinationFactory<,>).MakeGenericType(SourceType, DestinationType);\n        if (!expectedInterface.IsAssignableFrom(objectConstructorType))\n        {\n            throw new InvalidOperationException($\"Type '{objectConstructorType.Name}' does not implement IDestinationFactory<{SourceType.Name}, {DestinationType.Name}>\");\n        }\n        var constructMethod = expectedInterface.GetMethod(\"Construct\") ??\n            throw new InvalidOperationException($\"IDestinationFactory<{SourceType.Name}, {DestinationType.Name}> does not define a 'Construct' method.\");\n\n        var callExpression = Call(\n            Convert(constructorInstance, expectedInterface),\n            constructMethod,\n            srcParam, ctxParam\n        );\n\n        CustomCtorFunction = Lambda(callExpression, srcParam, ctxParam);\n    }\n    internal LambdaExpression CreateMapperLambda(IGlobalConfiguration configuration) =>\n        Types.ContainsGenericParameters ? null : new TypeMapPlanBuilder(configuration, this).CreateMapperLambda();\n    private PropertyMap GetPropertyMap(string name)\n    {\n        if (_propertyMaps == null)\n        {","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/TypeMap.cs#L250-L286","documentation":"ConstructUsingObjectConstructor(Type) wires destination construction to a factory object invoked as factory.Construct(source, context). It requires the supplied type to implement IDestinationFactory<TSource,TDestination> with the exact generic arguments of the map; expectedInterface.IsAssignableFrom(objectConstructorType) is checked and the throw names the required closed interface. The Construct method is then reflected off that interface.","triggerScenarios":"cfg.CreateMap<S,D>().ConstructUsingObjectConstructor(typeof(F)) where F does not implement IDestinationFactory<S,D> (generic args swapped to IDestinationFactory<D,S>, implements a different/base interface, or the destination type itself is passed instead of a factory).","commonSituations":"Swapped generic arguments on the factory interface; factory implementing only a non-generic marker interface; passing typeof(D) (the destination) by mistake; renaming types so the closed interface no longer matches.","solutions":["Make the factory implement IDestinationFactory<TSource, TDestination> with the exact same type arguments as the CreateMap<TSource,TDestination>.","Verify up front: typeof(IDestinationFactory<S,D>).IsAssignableFrom(typeof(Factory)).","If you do not need a reusable factory, use ConstructUsing((s, ctx) => new D(...)) for ad-hoc construction instead.","Double-check you are passing the factory type, not the destination type."],"exampleFix":"// before\npublic class DFactory : IDestinationFactory<Dest, Source> { ... } // wrong arg order\nCreateMap<Source, Dest>().ConstructUsingObjectConstructor(typeof(DFactory)); // throws\n\n// after\npublic class DFactory : IDestinationFactory<Source, Dest>\n{\n    public Dest Construct(Source source, ResolutionContext context) => new Dest(source.Id);\n}\nCreateMap<Source, Dest>().ConstructUsingObjectConstructor(typeof(DFactory));","handlingStrategy":"type-guard","validationCode":"var expected = typeof(IDestinationFactory<,>).MakeGenericType(typeof(TSrc), typeof(TDst));\nif (!expected.IsAssignableFrom(typeof(TFactory)))\n{\n    throw new InvalidOperationException(\n        $\"{typeof(TFactory)} must implement IDestinationFactory<{typeof(TSrc).Name}, {typeof(TDst).Name}.\");\n}\n\ncfg.CreateMap<TSrc, TDst>().ConstructUsingObjectConstructor(typeof(TFactory));","typeGuard":"static bool ImplementsFactoryFor<TSrc, TDst>(Type factoryType) =>\n    typeof(IDestinationFactory<TSrc, TDst>).IsAssignableFrom(factoryType);","tryCatchPattern":"try { cfg.CreateMap<S, D>().ConstructUsingObjectConstructor(typeof(F)); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not implement IDestinationFactory\"))\n{\n    // correct the generic arguments on the factory interface, then retry\n}","preventionTips":["Match the factory's IDestinationFactory<,> generic arguments exactly to the CreateMap<,> arguments.","Add a unit test asserting typeof(IDestinationFactory<S,D>).IsAssignableFrom(typeof(Factory)).","Prefer ConstructUsing((s, ctx) => ...) for one-off construction to avoid the interface contract."],"tags":["configuration","constructor","destination-factory","idestinationfactory"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}