Herman Code 🚀

Ignore mapping one property with Automapper

February 20, 2025

Ignore mapping one property with Automapper

Streamlining information mapping is important for immoderate exertion dealing with analyzable entity transformations. AutoMapper, a fashionable entity-to-entity mapping room, simplifies this procedure importantly. However what occurs once you demand much granular power, particularly once you privation to exclude definite properties from the mapping procedure? This station delves into the creation of ignoring place mappings with AutoMapper, offering you with the instruments and strategies to maestro this indispensable facet of information manipulation.

Knowing the Fundamentals of AutoMapper

AutoMapper eliminates tedious handbook mapping codification by mechanically mapping properties betwixt origin and vacation spot objects. This normal-primarily based attack saves builders clip and reduces boilerplate. Nevertheless, location are situations wherever you mightiness not privation each properties to beryllium mapped. For case, you mightiness person delicate information successful your origin entity that shouldn’t beryllium transferred to the vacation spot, oregon you mightiness person calculated properties that demand to beryllium dealt with individually.

This is wherever the powerfulness of ignoring properties comes into drama. By explicitly telling AutoMapper which properties to disregard, you addition good-grained power complete the mapping procedure, guaranteeing information integrity and safety.

Ideate mapping person information from a database to a DTO. You wouldn’t privation to exposure delicate accusation similar passwords oregon inner IDs. Ignoring these properties throughout the mapping procedure is a captious safety measurement.

Ignoring Properties: The ForMember Methodology

AutoMapper’s ForMember methodology is your capital implement for controlling idiosyncratic place mappings. It permits you to specify precisely however a vacation spot place ought to beryllium populated, together with ignoring the origin place altogether. This versatile methodology affords respective choices for ignoring properties.

1 attack is utilizing Disregard(). This tells AutoMapper to wholly skip the mapping for the specified vacation spot place, leaving its worth arsenic the default for its kind.

Different method includes utilizing MapFrom(src => null). This explicitly units the vacation spot place to null, overriding immoderate possible default values. This tin beryllium utile once you privation to guarantee a place is explicitly nullified successful the vacation spot entity.

Illustration: Ignoring a Password Tract

See a script wherever you’re mapping a Person entity to a UserDTO. You privation to exclude the PasswordHash place for safety causes. Present’s however you tin accomplish this utilizing ForMember and Disregard():

CreateMap<Person, UserDTO>() .ForMember(dest => dest.PasswordHash, choose => choose.Disregard()); 

Conditional Mapping: Ignoring Primarily based connected Discourse

Typically, you mightiness demand to disregard a place primarily based connected definite situations. AutoMapper permits you to instrumentality conditional mapping logic inside the ForMember technique. This offers you the flexibility to dynamically power the mapping procedure primarily based connected the government of your objects oregon another contextual components.

For case, you might disregard a place if its worth is null successful the origin entity oregon primarily based connected the execution situation (e.g., improvement vs. exhibition).

This dynamic attack to ignoring properties enhances the flexibility and powerfulness of AutoMapper, permitting for much blase mapping eventualities.

Illustration: Ignoring a Place Based mostly connected a Information

CreateMap<Origin, Vacation spot>() .ForMember(dest => dest.PropertyToIgnore, choose => decide.Information(src => src.SomeCondition == actual)); 

Precocious Methods: Utilizing Worth Resolvers and Converters

For much analyzable eventualities, AutoMapper affords Worth Resolvers and Kind Converters. These supply larger power complete the mapping procedure, permitting you to instrumentality customized logic for dealing with circumstantial properties. Piece not strictly for ignoring properties, they tin beryllium utilized to accomplish akin outcomes by conditionally returning null oregon default values.

Worth Resolvers are utile once the logic for figuring out the vacation spot place’s worth is analyzable and includes outer dependencies. Kind Converters are appropriate once you demand to change full information varieties throughout the mapping procedure.

These precocious strategies supply a almighty manner to grip intricate mapping necessities, providing a advanced grade of power and flexibility.

Champion Practices and Issues

  • Prioritize safety: Ever disregard delicate properties similar passwords oregon API keys throughout mapping.
  • Keep consistency: Found broad conventions for ignoring properties to guarantee codification readability and maintainability.

Selecting the correct attack for ignoring properties relies upon connected your circumstantial wants. Disregard() is appropriate for elemental eventualities, piece conditional mapping and precocious strategies message larger flexibility for analyzable logic.

  1. Place the properties to disregard.
  2. Take the due method (Disregard(), MapFrom(src => null), conditional mapping, and many others.).
  3. Instrumentality the logic inside the ForMember technique.
  4. Trial totally to guarantee the desired behaviour.

AutoMapper’s ForMember technique, coupled with strategies similar Disregard() and conditional mapping, offers a sturdy resolution for excluding circumstantial properties throughout the mapping procedure. This granular power ensures information integrity, enhances safety, and simplifies analyzable entity transformations.

Larn much astir precocious AutoMapper configurations.- AutoMapper Documentation

[Infographic Placeholder: Illustrating antithetic strategies for ignoring properties with AutoMapper]

FAQ

Q: What’s the quality betwixt Disregard() and MapFrom(src => null)?

A: Disregard() leaves the vacation spot place with its default worth, piece MapFrom(src => null) explicitly units it to null.

By mastering the creation of ignoring properties with AutoMapper, you tin importantly better the ratio and safety of your information mapping processes. Research the assorted methods mentioned successful this station and take the attack that champion fits your wants. Retrieve to prioritize safety and keep consistency for cleanable and maintainable codification. Commencement optimizing your mapping scheme present and unlock the afloat possible of AutoMapper.

Question & Answer :
I’m utilizing Automapper and I person the pursuing script: People OrderModel has a place known as ‘ProductName’ that isn’t successful the database. Truthful once I attempt to bash the mapping with:

Mapper.CreateMap<OrderModel, Orders>(); 

It generates an objection :

“The pursuing 1 properties connected Task.ViewModels.OrderModel are not mapped: ‘ProductName’

I’ve publication astatine AutoMapper’s Wiki for Projections the other lawsuit (the other property is connected the vacation spot, not successful the origin which is really my lawsuit )

However tin I debar automapper to brand the mapping of this place?

From Jimmy Bogard: CreateMap<Foo, Barroom>().ForMember(x => x.Blarg, decide => choose.Disregard());

It’s successful 1 of the feedback astatine his weblog.

Replace(from Jamie’s remark Jan four ‘19 astatine eleven:eleven:)

Disregard has been changed with DoNotValidate successful ForSourceMember: https://github.com/AutoMapper/AutoMapper/blob/maestro/docs/eight.zero-Improve-Usher.md