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):

  Repository object & lt; - & gt; Unit & lt; - & gt;   

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 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;

Thanks

Thank you.

Update

I have created a test code here:

Replaced here for reference:

ProfileClasses.cs

  namespace nullcollectionIssue {system.collections.Generic; Public category source profiles are a {public identification & lt; String & gt; Stuff {get; Set; }} Public Square DestProfileOne {Public ICollection & lt; String & gt; Stuff {get; Set; }} Public class SourceProfileTwo {public ICollection & lt; String & gt; Stuff {get; Set; }} Public Square DestProfileTwo {public ICollection & lt; String & gt; Stuff {get; Set; }}}   

AutoMapperConfigurator.cs

  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; ();}); }}}   

Mapping Tests CS

  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); }}}   

test AutoMapper_Configuration_IsValid and AutoMapper_ProfileTwo_DoesntAllowNullCollections , but test AutoMapper_ProfileOne_AllowsNullCollections fails because < Code> dest.Stuff is not null.

"itemprop =" text ">

You are getting this behavior because you are calling static Mapper.Map in your configured method:

  protected override zero configured () {Mapper.CreateMap & LT; SourceProfileTwo, DestProfileTwo & gt; (); }   

should be instead

  protected override zero configured () {CreateMap & LT; SourceProfileTwo, DestProfileTwo & gt; (); // Major Mapper Removed}    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -