CSLA 4 - Csla.Core.BusinessBase.AddBusinessRules() gives exception "Sequence contains no elements"

CSLA 4 - Csla.Core.BusinessBase.AddBusinessRules() gives exception "Sequence contains no elements"

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

Jaans posted on Tuesday, February 01, 2011

I have a non-CSLA Property with a [Required] data annotation that is causing CLSA to freak out.

        [DataObjectFieldtruefalse )]
        public string DateString
            get { return InternalDate.Text; }
            set { InternalDate = new SmartDatevalue ); }

It's a runtime issue, that happens in my business object when it calls "base.AddBusinessRules" which calls to Core.BusinessBase.AddBusinessRules()

		protected override void AddBusinessRules()



Here is the exception:
System.InvalidOperationException {"Sequence contains no elements"}
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Csla.Rules.BusinessRules.AddDataAnnotations()
   at Csla.Core.BusinessBase.AddBusinessRules()
   at XXXXXXXXXXX.BusinessRules.AAAAA.BBBBBB.AddBusinessRules() in __________
   at Csla.Core.BusinessBase.InitializeBusinessRules()
   at Csla.Core.BusinessBase..ctor()
   at Csla.BusinessBase`1..ctor()
   at XXXXXXXXXXX.BusinessRules.AAAAA.BBBBBB..ctor() in __________
   at lambda_method(Closure )
   at Csla.Reflection.MethodCaller.CreateInstance(Type objectType)
   at Csla.Reflection.LateBoundObject..ctor(Type objectType)
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters)

If I add the following it works (but the point is I don't need this):

public static readonly PropertyInfo<stringDateStringProperty = RegisterProperty<string>( c => c.DateString );

I'm expecting that I should be able to use .NET data annotations on "custom" properties that do not need to be CSLA PropertyInfo properties?

ajj3085 replied on Tuesday, February 01, 2011

I'm not sure Csla supports properties which it doesn't know about.  And I'm not really sure why you'd not want Csla to know about this property; if it did work, what you'd be doing is have a validation rule that is ONLY enforced by the client.  And depending on client (web, for example) it's trivially easy to get around client side validation.

Of course if you want to go down that road, don't call the base.AddBusinessRules.  Then Csla won't pick up ANY validation attributes on your properties though.

Jaans replied on Tuesday, February 01, 2011

Thanks guys.

My motivations for such properties are precisely that - it's specific for the the consumer of the business object and mostly on the UI, and almost always wraps over an existing property. It's purposefully not something I would like to subscribe into CSLA's field manager, nor it's business rules. Plainly, it's a convenience property and I do not want to incur additional cost for it's convenience by increasing the FieldManager payload, etc.

Fair enought, it isn't the major use case and it's mostly for edge-cases, but something that I've been able to use with substantial value since CSLA 2.

The code in Csla.Core.BusinessBase.AddBusinessRules() is using a linq statement that assumes all properties decorated with DataAnnotations attributes will be managed properties. All I'm suggesting is that for some scenarios (that I have at least) this isn't always so, and I'm just asking the question whether it is reasonable for a code tweak to check if there is no corresponding managed property to simply skip over it, instead of throwing the exception in question.

I have a simple enough work around, but it does use yet another slot in the field manager.

Thanks again for your input

JonnyBee replied on Tuesday, February 01, 2011


DataAnnotation rules is wrapped into an ordinary Csla BusinessRule and all field rules require a PropertyInfo object to identify the property the rule is linked to. This is by design in the rule engine.

Copyright (c) Marimer LLC