tag:blogger.com,1999:blog-6691994129222744759.post8921357788740430487..comments2024-03-21T11:36:24.769-07:00Comments on Dynamics GP Land: ASP.NET Core and EF Core with Dynamics GP: Trim trailing spaces from char fieldsChristina Phillipshttp://www.blogger.com/profile/03332221198245457747noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-6691994129222744759.post-78243154012536137562018-12-17T03:03:58.164-08:002018-12-17T03:03:58.164-08:00Hi
Has anyone had any success with the AutoMapper...Hi<br /><br />Has anyone had any success with the AutoMapper approach when it comes to complex types? It works fine for simple DTOs but doesn't seem to work when you have nested objects.<br /><br />Take the following representation of a person<br />First Name: "Pete "<br />Surname: "Littlewood "<br /><br />Json contains a name object and a display name property that is simply a concatenation of the two fields. The above AutoMapper configuration will only trim the display name property<br /><br />{<br /> "name": {<br /> "first": "Pete ",<br /> "surname": "Littlewood "<br /> },<br /> "displayname": "Pete Littlewood"<br />}<br /><br />Is there a simple way to hit all string properties regardless of DTO complexity?<br />petelittlewoodhttps://www.blogger.com/profile/09031358318466605874noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-39612146092372550952017-11-08T13:57:05.613-08:002017-11-08T13:57:05.613-08:00Hi Steve, i wouldn't want to place blame squar...Hi Steve, i wouldn't want to place blame squarely, as i never did track down if it was Entity Framework or AutoMapper. I'd lean towards AutoMapper being the culprit however.Anonymoushttps://www.blogger.com/profile/17507368683384280326noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-68124861900897334242017-11-02T18:34:28.786-07:002017-11-02T18:34:28.786-07:00Hi Daniel,
Thanks for the update. So you attribut...Hi Daniel,<br /><br />Thanks for the update. So you attribute the inconsistent SQL to AutoMapper ProjectUsing, and not something weird with Entity Framework? (which I've seen does sometimes behave inexplicably)<br /><br />Steve Endowhttps://www.blogger.com/profile/03950475674093020502noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-66497972385805177902017-11-02T16:42:09.235-07:002017-11-02T16:42:09.235-07:00I recently ran afoul of using an AutoMapper Conver...I recently ran afoul of using an AutoMapper ConvertUsing for string trimming when refactoring my EntityFramework query objects to take advantage of AutoMapper's QueryableExtensions; Namely, the ProjectTo extension method. <br /><br />My first attempt at a fix was to change ConvertUsing to ProjectUsing, as suggested in the documentation, although the generated SQL changed for each time i executed the query. What i mean is, the exact same query for a static dataset would produce different results for each query execution (strange).<br /><br />Resorted to using a related method to what you first proposed Steve. I'll post link my Autofac extension method for registering AutoMapper, which includes string trimming below. It'll be useful for anyone attempting to do this, whilst still taking advantage of ProjectTo.<br /><br /><a rel="nofollow">https://gist.github.com/TheDanielDoyle/ac3ce0e5c9ea5a88c75df3872170b8a0</a>Anonymoushttps://www.blogger.com/profile/17507368683384280326noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-68719302443509511232017-11-02T16:19:24.068-07:002017-11-02T16:19:24.068-07:00Yes Allan, i'd use that also. Although that re...Yes Allan, i'd use that also. Although that requires targeting C# 6.0 and greater. A more generally supported method would be CreateMap().ConvertUsing(str => str == null ? null : str.Trim());Anonymoushttps://www.blogger.com/profile/17507368683384280326noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-54118514863887708032017-10-17T22:53:35.077-07:002017-10-17T22:53:35.077-07:00Hi, great answer but I'd use:
CreateMap().Co...Hi, great answer but I'd use:<br /> CreateMap().ConvertUsing(str => str?.Trim());<br />instead as this won't make a null string into an empty string.Anonymoushttps://www.blogger.com/profile/05695822880274217718noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-86918497545910693922017-06-12T03:05:19.971-07:002017-06-12T03:05:19.971-07:00Glad i could help Steve :)Glad i could help Steve :)Anonymoushttps://www.blogger.com/profile/17507368683384280326noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-1402827274068027732017-06-11T07:53:09.222-07:002017-06-11T07:53:09.222-07:00Hi Daniel! THANK YOU! That is genius!
This is the...Hi Daniel! THANK YOU! That is genius!<br /><br />This is the first time I've used AutoMapper, so I would have never thought to use it to manipulate or trim the property values--but it makes complete sense. It's touching all of the class data anyway, so it seems like the perfect place to trim the strings. Much better than having to call a string trim method every time I return data.<br /><br />Awesome, thank you very much for the tip.<br /><br />SteveSteve Endowhttps://www.blogger.com/profile/03950475674093020502noreply@blogger.comtag:blogger.com,1999:blog-6691994129222744759.post-46646148201654531022017-06-11T04:23:14.867-07:002017-06-11T04:23:14.867-07:00Hi Steve, i was nodding until i saw you were using...Hi Steve, i was nodding until i saw you were using AutoMapper.<br /><br />Please try this:<br />CreateMap().ConvertUsing(str => (str ?? string.Empty).Trim());<br /><br />-Daniel P. DoyleAnonymoushttps://www.blogger.com/profile/17507368683384280326noreply@blogger.com