{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/2562","repository_url":"https://api.github.com/repos/AutoMapper/AutoMapper","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/2562/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/2562/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/2562/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/2562","id":304172979,"number":2562,"title":"Question: How to configure real application to allow unit testing","user":{"login":"JonPSmith","id":4509872,"avatar_url":"https://avatars1.githubusercontent.com/u/4509872?v=4","gravatar_id":"","url":"https://api.github.com/users/JonPSmith","html_url":"https://github.com/JonPSmith","followers_url":"https://api.github.com/users/JonPSmith/followers","following_url":"https://api.github.com/users/JonPSmith/following{/other_user}","gists_url":"https://api.github.com/users/JonPSmith/gists{/gist_id}","starred_url":"https://api.github.com/users/JonPSmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JonPSmith/subscriptions","organizations_url":"https://api.github.com/users/JonPSmith/orgs","repos_url":"https://api.github.com/users/JonPSmith/repos","events_url":"https://api.github.com/users/JonPSmith/events{/privacy}","received_events_url":"https://api.github.com/users/JonPSmith/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-03-11T16:40:49Z","updated_at":"2018-03-11T21:04:01Z","closed_at":"2018-03-11T18:20:10Z","author_association":"NONE","body":"Can I get some advice on using AutoMapper, as if feels I'm going about it the wrong way.\r\n\r\n**Summary**: I am trying to write an ASP.NET Core application with DTOs that use both object-to-object mapping and LINQ projection. I want to structure my code such that I can unit test my code. I will be using dependency injection (DI) to provide an instance of AutoMapper's `MapperConfiguration`/`IMapper` types for my code.\r\n\r\nThe issue I am hitting is that the `Map` method needs an instance of the `IMapper` while the `ProjectTo` method needs an instance of `MapperConfiguration` (if I provide an instance of `IMapper` in a unit test that uses `ProjectTo` it throws an exception \"Mapper not initialized....\").\r\n\r\nMy question: I plan to inject `MapperConfiguration` and use `config.CreateMapper()` when I am using the `Map` method. Is that correct approach, or is there something I should change in my unit tests?\r\n\r\n## Supplementary information\r\nThis shows my unit test in case I have gone about things in the wrong way.\r\n\r\n### Object-to-Object mapping\r\n\r\n```csharp\r\nvar config = new MapperConfiguration(cfg =>\r\n{\r\n    cfg.AddProfile(new ServiceLayerBizInDto());\r\n});\r\n\r\n//ATTEMPT\r\nvar input = new ServiceLayerBizInDto { Num = 234 };\r\nvar data = config.CreateMapper().Map<BizDataIn>(input);\r\n\r\n//VERIFY\r\ndata.Num.ShouldEqual(234);\r\n```\r\n\r\n### ProjectTo mapping\r\n\r\n```csharp\r\npublic void TestListBloggersAutoMapperMapperConfigDto()\r\n{\r\n    //SETUP\r\n    var config = new MapperConfiguration(cfg => { cfg.CreateMap<Blogger, ListBloggersDto>(); });\r\n\r\n    var options = SqliteInMemory.CreateOptions<Chapter10DbContext>();\r\n    using (var context = new Chapter10DbContext(options))\r\n    {\r\n        context.Database.EnsureCreated();\r\n        context.AddRange(SetupPosts());\r\n        context.SaveChanges();\r\n        var logs = new List<string>();\r\n        SqliteInMemory.SetupLogging(context, logs);\r\n\r\n        //ATTEMPT\r\n        var dtos = context.Bloggers\r\n            .ProjectTo<ListBloggersDto>(config)\r\n            .ToList();\r\n\r\n        //VERIFY\r\n        dtos.Count.ShouldEqual(2);\r\n        dtos.Select(x => x.Name).ShouldEqual(new[] { AdaName, SherlockName });\r\n        dtos.Select(x => x.PostsCount).ShouldEqual(new[] { 2, 1 });\r\n\r\n        foreach (var log in logs)\r\n        {\r\n            _output.WriteLine(log);\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n### Version: x.y.z\r\nAutoMapper 6.2.2.\r\nNet Core: 2.0\r\n"},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/372148979","html_url":"https://github.com/AutoMapper/AutoMapper/issues/2562#issuecomment-372148979","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/2562","id":372148979,"user":{"login":"jbogard","id":104498,"avatar_url":"https://avatars1.githubusercontent.com/u/104498?v=4","gravatar_id":"","url":"https://api.github.com/users/jbogard","html_url":"https://github.com/jbogard","followers_url":"https://api.github.com/users/jbogard/followers","following_url":"https://api.github.com/users/jbogard/following{/other_user}","gists_url":"https://api.github.com/users/jbogard/gists{/gist_id}","starred_url":"https://api.github.com/users/jbogard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbogard/subscriptions","organizations_url":"https://api.github.com/users/jbogard/orgs","repos_url":"https://api.github.com/users/jbogard/repos","events_url":"https://api.github.com/users/jbogard/events{/privacy}","received_events_url":"https://api.github.com/users/jbogard/received_events","type":"User","site_admin":false},"created_at":"2018-03-11T21:04:01Z","updated_at":"2018-03-11T21:04:01Z","author_association":"OWNER","body":"Um what?\n\nOn Sun, Mar 11, 2018 at 1:20 PM Jon P Smith <notifications@github.com>\nwrote:\n\n> OK. Getting the configuration using mapper.ConfigurationProvider sounds\n> better than config.CreateMapper().Map.\n>\n> PS. Some feedback on the Profile approach to configuration. I'm not going\n> to use the Profile approach because inheriting Profile class adds a\n> number of public properties to the class, which makes ASP.NET scaffolding\n> produce razor output for those properties.\n>\n> —\n> You are receiving this because you are subscribed to this thread.\n> Reply to this email directly, view it on GitHub\n> <https://github.com/AutoMapper/AutoMapper/issues/2562#issuecomment-372136626>,\n> or mute the thread\n> <https://github.com/notifications/unsubscribe-auth/AAGYMk1YJx31T0Lb0O9vnwb4YHreYuMpks5tdWragaJpZM4SlwkZ>\n> .\n>\n"}}
{"actor":{"display_login":"jbogard"}}