IgnoreMembers vs IgnoreFromMembers + IgnoreToMembers

Jun 12, 2012 at 4:37 PM

It would be nice if there was an option to ignore certain source members or target members only. Currently the IgnoreMembers names list does not differentiate between source or target.

Karl

Developer
Jun 30, 2012 at 10:36 AM

Could you provide small code snippet where you need such feature?

Jul 5, 2012 at 3:49 PM
qwer_ty wrote:

Could you provide small code snippet where you need such feature?

Yes, here it is (I have  created a custom version of EmitMapper that does that):

            VisitToEntry = new DefaultMapConfig()
                .ConvertUsing<string, string>(src => MapHelper.StringToDbFormat(src))
                .ConvertUsing<DateTime?, DateTime>(src => src.ToDbFormat())
                .ConvertUsing<decimal, decimal>(src => src.Normalize())
                .ConvertUsing<decimal?, decimal>(src => src.HasValue ? src.Value.Normalize() : 0)
                .MatchMembers((src, tgt) => src == tgt || (tgt == "Wp" + src) || (tgt == "WpViewDt" && src == "LastViewDt"))
                .IgnoreToMembers<decimal, decimal>(new[] { "SchedUnits", "TravelKm", "TravelTime", "ErrandKm", "BusFare",
                    "TaxiFare", "ParkingFees", "MealFee" });

The problem here is I have two sets of fields in entity1, both (almost) identical named except the second one has prefix "Wp".

Then, entity2 has the same fields, but only the names without prefix. Initially, the fields in entity2 are defaulted from the same named fields in entity1, but then, the fields in entity2 get updated and once their values are confirmed, get saved back to the fields in entity1 with Wp prefix, so that at the end, entity1 has the initial and final ("Wp") values for these fields. When saving back from entity2 to entity1 (the code above), I do not want to update the fields in entity1 without prefix (even thought they have the same names) because they contain the default values which we don't want to modify, but at the same time I cannot ignore these fields on the source entity (entity2), because they need to be mapped to the "Wp" fields in entity1.

 

Karl