Skip to content

Fluent Interface Type Configuration

webJose edited this page Mar 3, 2019 · 4 revisions

Fluent Interface Type Configuration webJose's ObjectComparer

(Esta página en Español)

One of the available ways to customize per-property object comparison is using a type configuration object. This object is designed with fluent interface in mind in order to simplify its use.

IMPORTANT: This type of configuration is global. This means that configuration made via a type configuration object is stored globally inside the scanner's data cache. Object comparer objects created after type configuration will use the values configured via this method. The configuration made via this method is overridable locally using fluent interface comparer configuration.

It may be possible that a particular data type's source code is not available for modification, or it is not desirable to modify it in order to use attributed configuration. For these cases there is the possibility to perform configuration using a type configuration object.

Creating a Type Configuration Object

The Scanner.ConfigureType() method creates this object. The object allows for fluent interface, so method calls can be chained.

//Configure the PurchaseOrder data type.  Changes will be global.
var configType = Scanner.ConfigureType<PurchaseOrder>();

Configuration Methods

The type configuration object has the IgnoreProperty() method that allows you to ignore a property during per-property comparison. Because type configuration object is not tied at this point with any particular target type, this method allows you to ignore a property for comparison against objects of the same type, objects of other types, or all objects.

All other configurations, such as property mapping, require a specific target type. In order to perform these other configuration tasks, call the ForType() method, which creates a dependent target type configuration object.

Configuration Methods In a Target Type Configuration Object

The MapProperty() method creates a property map between a property in the source type and a property in the target type. It also allows setting up string coercion.

The IgnoreProperty() method ignores the specified property for per-property comparison purposes when objects of the specified source type are compared against objects of the specified target type.

Finally, the ForType() method in this class is the exact same one found in the type configuration object. It is here for simplicity and to facilitate method call chaining.

Full Example

In the following example, configuration is provided for the type PurchaseOrder, and then it is further configured for PurchaseOrderVM and PurchaseOrderDto.

var poConfig = Scanner.ConfigureType<PurchaseOrder>();
//Ignore The Id property when comparing against any other data type.
poConfig.IgnoreProperty(src => src.Id, IgnorePropertyOptions.IgnoreForOthers)
    //Switch to the specific PurchaseOrder--PurchaseOrderVM combination.
    .ForType<PurchaseOrderVM>()
    //PurchaseOrder.CreatedOn => PurchaseOrderVM.SubmissionDate
    .MapProperty(src => src.CreatedOn, dst => dst.SubmissionDate)
    //Etc.  You get it, right?
    .MapProperty(src => src.ModifiedOn, dst => dst.ModifiedDate)
    .IgnoreProperty(src => src.CreatedBy)
    //Switch to the specific PurchaseOrder--PurchaseOrderDto.
    .ForType<PurchaseOrderDto>()
    .MapProperty(src => src.CreatedOn, dst => dst.RecordCreated);

For more information about the comparison routine and comparison results, see Property Comparison Result Interpretation and Property Value Comparison Customization.

Available Languages

Clone this wiki locally