BypassPropertyChecks

BypassPropertyChecks

Old forum URL: forums.lhotka.net/forums/t/10237.aspx


Kevin Fairclough posted on Tuesday, April 05, 2011

Hi,

We're currently using AutoMapper to map our Csla BO to our NHIbernate data model via an object factory.  The AutoMapper automatically maps an entire graph based configuration for each type in the graph.

What I need is to set BypassPropertyChecks on and off for each mapped type when going from data model to Csla bo.  I can use intercepts in Automapper BeforeMap & AfterMap to do this for each type in the graph.

My only solution up to now is to call methods in my BusinessBase override that sets the private field _bypassPropertyChecks like so:

[NonSerializedprivate FieldInfo _field  = typeof(Csla.Core.BusinessBase).GetField("_bypassPropertyChecks"BindingFlags.Instance | BindingFlags.NonPublic |
                                                      BindingFlags.FlattenHierarchy);

        public void BypassPropertyChecksMappingStart()
        {
            _field.SetValue(thistrue);            
        }

        public void BypassPropertyChecksMappingFinish()
        {
            _field.SetValue(thisfalse);            
        }

These methods are called in BeforeMap & AfterMap respectively for each child in the graph when mapping takes place.

Is it safe to do this?  A convention in AutoMapper ensures that they are always run in pairs.

Kevin

 

 

RockfordLhotka replied on Tuesday, April 05, 2011

In CSLA 4 there is an ICheckRules interface in Csla.Core. It exists specifically to allow external code to turn rule check on/off, and to force all rules to run.

We added this to support ASP.NET MVC, but it has other applications as well - such as yours.

There's a section in the Using CSLA 4: Creating Business Objects ebook that discusses all three supported techniques for enabling/disabling rules in an object.

Kevin Fairclough replied on Wednesday, April 06, 2011

Thanks for that I needed to do that also.  It would be nice if I could use similar methods with BypassPropertyChecks

Kevin

RockfordLhotka replied on Wednesday, April 06, 2011

I'm not sure what you mean?

The three rule techniques for suppressing rules all end up using the same low-level scheme. So the ICheckRules interface does the same thing as BypassPropertyChecks, but in a way that provides you with more control.

Kevin Fairclough replied on Wednesday, April 06, 2011

AutoMapper will set the property directly via its setter.  I was under the impression BypassPropertyChecks stopped things like PropertyChanged being fired when loading the object from the database.  If I use ICheckRules to supress rules from firing, do I still need to set BypassPropertyChecks aswell? 

RockfordLhotka replied on Wednesday, April 06, 2011

I am almost 100% sure that ICheckRules does the same thing as BypassPropertyChecks - just through a different style of interface.

Copyright (c) Marimer LLC