Can AutoMappers AllowNullCollections setting be constrained to a profile? -
Note: I On the question have asked my MVC There are basically two levels of mapping in the application (Simplified for this question): We have created two profiles, that are going to handle the configuration for each appropriate mapping level. "Repository Objects" is finally sorted on XML and used in REST web services. . The problem we encountered was that an empty collection in the storage object would be sorted for an empty element in XML, and would cause a problem because the web service is neither an element nor data containing an element. We were able to resolve this by using the AllowNullCollections configuration setting. This (of course) will create an empty collection instead of a blank collection, which will then be exactly serial. Although I'm not entirely enjoyable as a global setting because it is not really the best practice. Send me this Thanks Thank you. Update I have created a test code here: Replaced here for reference: ProfileClasses.cs AutoMapperConfigurator.cs Mapping Tests CS test You are getting this behavior because you are calling static should be instead
Repository object & lt; - & gt; Unit & lt; - & gt;
repository object & lt; - & gt; Unit mapping because the repository objects are automatically generated (hence are ugly anyway) and its very low levels in the application But I "corrupt" to the
unit & lt; - & gt;
using namespace NullCollectionIssue {AutoMapper By; Public Category ProfileOne: Profile {Public Override String ProfileName {get {return "ProfileOne"; }} Secure Override Zero () {AllowNullCollections = true; Mapper Createmap & lt; SourceProfileOne, DestProfileOne & gt; (); }} Public Square ProfileTwo: Profile {Public Override String ProfileName {get {return "profileTwo"; }} Secure Override Zero Configure () {Mapper. Creatives & lt; SourceProfile, Two, Destprofile Two & gt; (); }} Public Static Class AutoMapperConfigurator {Public Fixed Zero Configuration () {Mapper. Initial (x = & gt; {x.AddProfile & lt; ProfileOne & gt; (); x.AddProfile & lt; Profile Two & gt; ();}); }}}
using namespace NullCollectionIssue {AutoMapper By; Using NUnit.Fraamework; [Testfaiakshns] public class mapping tests {[Test] public void Otmaprpr Configreshn_aisai Wide () {Otomaprpr Configutr. ConfiForce (); Mapper.AssertConfigurationIsValid (); } [Test] Public Zero AutomaprofileOn_AllowClocolations () {Configurator on AutoMex ConfiForce (); Mapper.AssertConfigurationIsValid (); Var Source = New SourceProfileOne {Stuff = null}; Var dev = Mapper.Map & lt; SourceProfileOne, DestProfileOne & gt; (Source); Decide. That (dest, is.Not.Null); Responsibility That (dest.Stuff, Is.Null); } [Test] Public Zero AutomagraphyTw_DoesntAllowNullCollections () {AutoMapperConfigurator.Configure (); Mapper.AssertConfigurationIsValid (); Var source = new source profile {{stuff = null}; Var dev = Mapper.Map & lt; SourceProfileTwo, DestProfileTwo & gt; (Source); Decide. That (dest, is.Not.Null); Responsibility That (dest.Stuff, Is.Not.Null); Responsibility That (dest.Stuff, Is.Empty); }}}
AutoMapper_Configuration_IsValid and
AutoMapper_ProfileOne_AllowsNullCollections fails because < Code> dest.Stuff is not null.
Mapper.Map in your configured method:
protected override zero configured () {Mapper.CreateMap & LT; SourceProfileTwo, DestProfileTwo & gt; (); }
protected override zero configured () {CreateMap & LT; SourceProfileTwo, DestProfileTwo & gt; (); // Major Mapper Removed}
Comments
Post a Comment