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:
[NonSerialized] private FieldInfo _field = typeof(Csla.Core.BusinessBase).GetField("_bypassPropertyChecks", BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy);
public void BypassPropertyChecksMappingStart()
{
_field.SetValue(this, true);
}
public void BypassPropertyChecksMappingFinish()
{
_field.SetValue(this, false);
}
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
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.
Thanks for that I needed to do that also. It would be nice if I could use similar methods with BypassPropertyChecks
Kevin
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.
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?
I am almost 100% sure that ICheckRules does the same thing as BypassPropertyChecks - just through a different style of interface.
Copyright (c) Marimer LLC