Adding a Business Rule to make sure a property is unique...

Adding a Business Rule to make sure a property is unique...

Old forum URL:

chris.stafford posted on Thursday, June 17, 2010

A quick question for all the CSLA.Net users out there with more experience then me (which should be pretty much all of you...)


I am constructing a business object for our middle tier, and I need to ensure that the Name property is unique in order ot fulfill database constraints.  The relevant structure of the object is below:




public partial class Object: BusinessBase<Object>







public static PropertyInfo<string> NameProperty = RegisterProperty<string> (c => c.Name);




public string






get { return GetProperty(NameProperty); }




set { SetProperty(NameProperty, value); }








override void





new Csla.Rules.CommonRules.MaxLength(NameProperty, 100));



The Object is contained in another CSLA Object, ObjectList.

I want to handle the unique Name constraint with a BusinessRule; in looking through the CSLA.Rules.CommonRules library, I noticed that there is a Lambda rule in there.  Any suggestions on how I can add a BusinessRule that will make sure all the objects have a unique Name?  Or is that better handled on an Insert/Update?


rxelizondo replied on Thursday, June 17, 2010


You may want to take a look at the following post:

Is not exactly what you are looking for but it talks about how to validate a collection.

ajj3085 replied on Sunday, June 20, 2010

I'd make a rule (async, if you expect the check to take any significant amount of time) which checks the data currently in the database.

Note though that you should still catch and retrown a DuplicateName exception in your DP_I or DP_U, as someone may have saved after your rule check fired during the properly changed.

Copyright (c) Marimer LLC