Skip to content
chaowlert edited this page Apr 10, 2020 · 5 revisions

Computed value

You can use Map method to specify logic to compute value. For example, compute full name from first name and last name.

TypeAdapterConfig<Poco, Dto>.NewConfig()
                            .Map(dest => dest.FullName, src => src.FirstName + " " + src.LastName);

Transform value

While Map method specify logic for single property, AddDestinationTransform allows transforms for all items of a type, such as trimming all strings. But really any operation can be performed on the destination value before assignment.

Trim string

TypeAdapterConfig<TSource, TDestination>.NewConfig()
        .AddDestinationTransform((string x) => x.Trim());

Null replacement

TypeAdapterConfig<TSource, TDestination>.NewConfig()
        .AddDestinationTransform((string x) => x ?? "");

Return empty collection if null

config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

Passing run-time value

In some cases, you might would like to pass runtime values (ie, current user). On configuration, we can receive run-time value by MapContext.Current.Parameters.

TypeAdapterConfig<Poco, Dto>.NewConfig()
                            .Map(dest => dest.CreatedBy,
                                 src => MapContext.Current.Parameters["user"]);

To pass run-time value, we need to use BuildAdapter method, and call AddParameters method to add each parameter.

var dto = poco.BuildAdapter()
              .AddParameters("user", this.User.Identity.Name)
              .AdaptToType<Dto>();
Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy