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: forums.lhotka.net/forums/t/9099.aspx


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

Name

{

 

 

 

get { return GetProperty(NameProperty); }

 

 

 

set { SetProperty(NameProperty, value); }

}

 

 

protected

 

 

 

override void

AddBusinessRules()

{

BusinessRules.AddRule(

 

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

Hi,


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


http://forums.lhotka.net/forums/p/8282/39434.aspx


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