{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1686","repository_url":"https://api.github.com/repos/AutoMapper/AutoMapper","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1686/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1686/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1686/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1686","id":177652077,"number":1686,"title":"Create a map configuration at runtime","user":{"login":"aoancea","id":4282532,"avatar_url":"https://avatars.githubusercontent.com/u/4282532?v=3","gravatar_id":"","url":"https://api.github.com/users/aoancea","html_url":"https://github.com/aoancea","followers_url":"https://api.github.com/users/aoancea/followers","following_url":"https://api.github.com/users/aoancea/following{/other_user}","gists_url":"https://api.github.com/users/aoancea/gists{/gist_id}","starred_url":"https://api.github.com/users/aoancea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aoancea/subscriptions","organizations_url":"https://api.github.com/users/aoancea/orgs","repos_url":"https://api.github.com/users/aoancea/repos","events_url":"https://api.github.com/users/aoancea/events{/privacy}","received_events_url":"https://api.github.com/users/aoancea/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2016-09-18T13:33:00Z","updated_at":"2016-09-21T04:44:57Z","closed_at":null,"body":"I am wondering if there is a way of creating a mapping configuration at runtime. This can be used for situations where you want to **streamline** the mapping process which in this case can be a **1:1** match and otherwise you don't care.\r\n\r\n**Having the following classes**\r\n\r\n```c#\r\npublic class FirstClass\r\n{\r\n    public int Int { get; set; }\r\n}\r\n\r\npublic class SecondClass\r\n{\r\n    public int Int { get; set; }\r\n}\r\n```\r\n\r\n**and this extension method**\r\n\r\n```c#\r\npublic static class AutoMapperExtension\r\n{\r\n    public static TDestination DeepCopyTo<TDestination>(this object source)\r\n    {\r\n        Type sourceType = source.GetType();\r\n\r\n        // Create a mapping configuration(the question is this)\r\n\r\n        return (TDestination)AutoMapper.Mapper.Map(source, sourceType, typeof(TDestination));\r\n    }\r\n}\r\n```\r\n\r\n**would it be possible for me to do something like**\r\n\r\n```c#\r\npublic void Process()\r\n{\r\n    FirstClass firstClass = new FirstClass() { Int = 5 };\r\n\r\n    // no mapping going on in here and anywhere else for that matter(inside .DeepCopyTo<> only)\r\n\r\n    SecondClass secondClass = firstClass.DeepCopyTo<SecondClass>(); \r\n}\r\n```\r\n\r\nI have a working implementation for what I described above which can be found [here](https://github.com/QubizSolutions/quiz-engine/blob/master/src/Infrastructure/Mapper.cs). Unit tests can also be found [here](https://github.com/QubizSolutions/quiz-engine/blob/master/src/UnitTesting/Infrastructure/MapperTest.cs).\r\n\r\nI know that it lacks some caching so that it doesn't try to create a map which already exists :bomb:\r\n\r\n\r\nIf there isn't anything yet like this for **AutoMapper** I would gladly help place it in as I also need it badly in multiple projects. Having it through the NuGet package would make my life easier and probably others' as well.\r\n\r\n@jbogard "},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/248510310","html_url":"https://github.com/AutoMapper/AutoMapper/issues/1686#issuecomment-248510310","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/1686","id":248510310,"user":{"login":"aoancea","id":4282532,"avatar_url":"https://avatars.githubusercontent.com/u/4282532?v=3","gravatar_id":"","url":"https://api.github.com/users/aoancea","html_url":"https://github.com/aoancea","followers_url":"https://api.github.com/users/aoancea/followers","following_url":"https://api.github.com/users/aoancea/following{/other_user}","gists_url":"https://api.github.com/users/aoancea/gists{/gist_id}","starred_url":"https://api.github.com/users/aoancea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aoancea/subscriptions","organizations_url":"https://api.github.com/users/aoancea/orgs","repos_url":"https://api.github.com/users/aoancea/repos","events_url":"https://api.github.com/users/aoancea/events{/privacy}","received_events_url":"https://api.github.com/users/aoancea/received_events","type":"User","site_admin":false},"created_at":"2016-09-21T04:44:57Z","updated_at":"2016-09-21T04:44:57Z","body":"If I can let the machine do my work for me, I would always put in the extra effort to achieve that.\r\n\r\nI am working on an implementation right now, backed with unit tests, and you can find it [here](https://github.com/QubizSolutions/quiz-engine/blob/master/src/Infrastructure/Mapper.cs) and tests [here](https://github.com/QubizSolutions/quiz-engine/blob/master/src/UnitTesting/Infrastructure/MapperTest.cs)\r\n\r\nIt might be a little messy atm, but it will be given a final touch in the end. The only thing that I don't like, but which is kinda necessary for certain scenarios, is regarding the `circular reference` and that's because here I need to construct the mapping of the whole object graph directly since **AutoMapper** won't be using internally my extension method which could be called from every lower level mappings which will also create the mapping configuration.\r\n\r\nI'm also thinking to move this extension to a NuGet package since I'm it in several projects. Do you have any suggestions around that?(I haven't put anything before on NuGet so I would appreciate a tip even though I know it would prolly take 1h to get me started with the information on the internet :) )"}}
{"actor":{"display_login":"aoancea"}}