{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1206","repository_url":"https://api.github.com/repos/AutoMapper/AutoMapper","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1206/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1206/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1206/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1206","id":144248717,"number":1206,"title":"Leverage c# dynamic binding to dynamically infer destination type for mapping","user":{"login":"rsheptolut","id":16118228,"avatar_url":"https://avatars.githubusercontent.com/u/16118228?v=3","gravatar_id":"","url":"https://api.github.com/users/rsheptolut","html_url":"https://github.com/rsheptolut","followers_url":"https://api.github.com/users/rsheptolut/followers","following_url":"https://api.github.com/users/rsheptolut/following{/other_user}","gists_url":"https://api.github.com/users/rsheptolut/gists{/gist_id}","starred_url":"https://api.github.com/users/rsheptolut/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsheptolut/subscriptions","organizations_url":"https://api.github.com/users/rsheptolut/orgs","repos_url":"https://api.github.com/users/rsheptolut/repos","events_url":"https://api.github.com/users/rsheptolut/events{/privacy}","received_events_url":"https://api.github.com/users/rsheptolut/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-03-29T12:25:23Z","updated_at":"2016-03-29T12:34:20Z","closed_at":null,"body":"I guess this is a feature request. This functionality may well be in Automapper and I've just missed it.\r\n\r\n**DISCLAIMER**: Something about the **dynamic** keyword scares some people, so I welcome any feedback, only if it does not stem from irrational fears.\r\n\r\nFirst of all, I'm new to Automapper, but I've instantly loved it! Although I felt that I would've wanted it to be **even simpler**. I want to say \"Map\" to a mapper and see it map whatever object to whatever type that is required in the context **without actually specifying that type**. I want it to be automatically inferred. Let me demonstrate in three steps.\r\n\r\nSimplest example use case: we want to map EF object to a DTO object in an MVC controller for whatever reason.\r\n\r\n**Step 1**. Regular implementation:\r\n```c#\r\npublic class Startup\r\n{\r\n    public void Configure()\r\n    {\r\n        var mapperConfiguration = new MapperConfiguration(cfg => \r\n        {\r\n            // You have to do it for every type that you will want to map\r\n            cfg.CreateMap<Entity, EntityDto>();\r\n        });\r\n    }\r\n}\r\n\r\npublic class Controller\r\n{\r\n    public EntityDto Action(int id, IMapper mapper)\r\n    {\r\n        using (var context = new MyDbContext())\r\n        {\r\n            var entity = context.dbSet<Entity>().Find(id);\r\n            return mapper.Map<EntityDto>(entity);\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n**Step 2**. Simplify with CreateMissingTypeMaps\r\n```c#\r\npublic class Startup\r\n{\r\n    public void Configure()\r\n    {\r\n        var mapperConfiguration = new MapperConfiguration(cfg => \r\n        {\r\n            // Thank you for this setting! Now we only have to create mappings for complex things, and the simple ones just work\r\n            cfg.CreateMissingTypeMaps = true;\r\n        });\r\n    }\r\n}\r\n\r\n// Code in controller unchanged\r\n```\r\n\r\n**Step 3**. Simplify even more with dynamic type\r\n```c#\r\n// Code in configuration unchanged\r\n\r\npublic class Controller\r\n{\r\n    public EntityDto Action(int id, IMapper mapper)\r\n    {\r\n        using (var context = new MyDbContext())\r\n        {\r\n            var entity = context.dbSet<Entity>().Find(id);\r\n\r\n            // The only thing that's changed is this line\r\n            return mapper.Map(entity);\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nTo summarize, it would be great if the following would be possible:\r\n```c#\r\nEntity entity;\r\n// Here I don't specify explicitly what type I want it to be converted to\r\nEntityDto dto = mapper.Map(entity); \r\n```\r\n\r\n**The destination type could be inferred automatically**, not only from local variable type, but return type, property type, field type, parameter type, etc.\r\n\r\nIt may not seem like a huge improvement in this example, but it saves a lot of typing and repetition (kinda violates DRY just a little bit), because C# is perfectly able to infer what type I want my object to be mapped to.\r\n\r\nTo clarify, **I already got that working in my project** with a little bit of code. So, if someone's interested, for step 3 to work you need something like what I've quickly put together:\r\n```c#\r\npublic static class AutoMapperIMapperExtensions\r\n{\r\n    internal static dynamic Map(this IMapper mapper, object source)\r\n    {\r\n        return source != null ? new DynamicMapper(mapper, source) : null;\r\n    }\r\n}\r\n    \r\n/* Note: I could just inherit from DynamicObject, but it is implemented in such way that it\r\n   will throw and swallow RuntimeBinderExceptions every time the conversion is performed.\r\n   It's normal and cannot be prevented without implementing IDynamicMetaObjectProvider yourself,\r\n   so I did just that */\r\npublic class DynamicMapper : DynamicConverter\r\n{\r\n    private readonly IMapper mapper;\r\n    private readonly object source;\r\n\r\n    public DynamicMapper(IMapper mapper, object source)\r\n    {\r\n        this.mapper = mapper;\r\n        this.source = source;\r\n    }\r\n\r\n    public override bool TryConvert(ConvertBinder binder, out object result)\r\n    {\r\n        result = mapper.Map(source, binder.Type);\r\n        return true;\r\n    }\r\n}\r\n\r\npublic class DynamicConverter : IDynamicMetaObjectProvider\r\n{\r\n    public DynamicMetaObject GetMetaObject(Expression parameter)\r\n    {\r\n        return new DynamicConverterMetaObject(parameter, this);\r\n    }\r\n    public virtual bool TryConvert(ConvertBinder binder, out object result)\r\n    {\r\n        result = null;\r\n        return false;\r\n    }\r\n}\r\n\r\npublic class DynamicConverterMetaObject : DynamicMetaObject\r\n{\r\n    internal DynamicConverterMetaObject(Expression expression, DynamicConverter value)\r\n            : base(expression, BindingRestrictions.Empty, value) {}\r\n\r\n    public override DynamicMetaObject BindConvert(ConvertBinder binder)\r\n    {\r\n        object result;\r\n        if (!(Value as DynamicConverter).TryConvert(binder, out result))\r\n            throw new InvalidCastException();\r\n\r\n        return new DynamicMetaObject(Expression.Convert(Expression.Constant(result), binder.Type),\r\n            BindingRestrictions.GetTypeRestriction(Expression, LimitType));\r\n    }\r\n}\r\n```\r\n\r\nAt first, the `Map` method only gives off a dynamic object, which in turn has the capability to be cast to any type, leveraging AutoMapper. When CLR requests the conversion it specifies what type it would like to convert to, and that's what I carry over to Automapper.\r\n\r\nThat improvement is just a little thing that can help to simplify your \"getting started\" guide, maybe save some keystrokes and stop enraging the DRY gods. \r\n"},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/202873265","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1206#issuecomment-202873265","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1206","id":202873265,"user":{"login":"rsheptolut","id":16118228,"avatar_url":"https://avatars.githubusercontent.com/u/16118228?v=3","gravatar_id":"","url":"https://api.github.com/users/rsheptolut","html_url":"https://github.com/rsheptolut","followers_url":"https://api.github.com/users/rsheptolut/followers","following_url":"https://api.github.com/users/rsheptolut/following{/other_user}","gists_url":"https://api.github.com/users/rsheptolut/gists{/gist_id}","starred_url":"https://api.github.com/users/rsheptolut/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsheptolut/subscriptions","organizations_url":"https://api.github.com/users/rsheptolut/orgs","repos_url":"https://api.github.com/users/rsheptolut/repos","events_url":"https://api.github.com/users/rsheptolut/events{/privacy}","received_events_url":"https://api.github.com/users/rsheptolut/received_events","type":"User","site_admin":false},"created_at":"2016-03-29T12:34:20Z","updated_at":"2016-03-29T12:34:20Z","body":"@lbargaoanu: Thanks for the tip. The thing is, I'm not talking about mapping **to** dynamic or expando objects. I'm talking about the **regular, concrete to concrete** mapping (entity to dto, or dto to viewmodel). It could be simplified by a feature of C# and CLR: dynamic binding."}}