DataPortal non CSLA stereotypes

DataPortal non CSLA stereotypes

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


aramka posted on Wednesday, November 14, 2012

Is there anything wrong with using the DataPortal like this:

 

 public class FieldMustBeUnique : Csla.Rules.BusinessRule

    {

        FieldIsUniqueCommand _cmd = null;

 

        public FieldMustBeUnique(IPropertyInfo primaryProperty, FieldIsUniqueCommand cmd)

            : base(primaryProperty)

        {

            if (InputProperties == null) InputProperties = new System.Collections.Generic.List<IPropertyInfo> { primaryProperty };

            _cmd = cmd;

        }

 

        protected override void Execute(RuleContext context)

        {

            string field = context.InputPropertyValues[PrimaryProperty].ToString();

 

            _cmd.Field = field;

            _cmd = DataPortal.Update(_cmd);

 

            if (!_cmd.IsUnique)

            {

                context.AddErrorResult(Localization.Localization.GetMessage(Localization.Messages.ObjectAlreadyExists));

 

            }

        }

    }

 

    [Serializable]

    public abstract class FieldIsUniqueCommand

    {

        public bool IsUnique

        {

            get;

            protected set;

        }

 

        public string Field

        {

            get;

            set;

        }

 

        protected abstract void DataPortal_Update();

 

    }

RockfordLhotka replied on Wednesday, November 14, 2012

You can invoke the data portal from inside a business rule, yes. That is supported.

My one comment, is that by implementing this as a synchronous rule you'll block the user from interacting with the UI until that rule completes. You might choose to make the rule async (as described in the 'Using CSLA 4' books) so the UI doesn't freeze.

RockfordLhotka replied on Wednesday, November 14, 2012

Oh, I see - your question was about the Command object not inheriting from CommandBase?

The answer to that is a little more complex.

In pure .NET code the data portal will do what you describe, because it only cares that the type is Serializable and that you implement the appropriate DataPortal_XYZ methods.

But in Silverlight, Windows Phone, and WinRT code this won't work because your type also needs to implement a serialization interface so the MobileFormatter can work. That interface is tricky to implement, and you are better off subclassing one of the CSLA base classes (at a minimum the Csla.Core.MobileObject base class).

JonnyBee replied on Wednesday, November 14, 2012

Just a couple of suggestions:

 

  1. Make the command object inherit from CommandBase (will make life easier for you)
  2. Inherit your rule from PropertyRule. This will give you the option of setting CanRunInCheckRules/CanRunInServer/CanRunAsAffected property so the rule may be denied to run in any combination of these contexts. Typically - these rules should only run when the user edits a filed in the UI (on a rich client) or in the postback on an web application. 
  3. Make sure to use managed properties on your command object. This will make your rule/command more veratile and work in all supported platforms.
  4. I suspect you should try to use the generic version of the DataPortal call but haven´t tried to compile/run your code. 

 

Otherwise the code is good. 

 

aramka replied on Wednesday, November 14, 2012

I have taken your suggestion into account. Thanks for your help.

Copyright (c) Marimer LLC