Broken Rules - Client / Server

Broken Rules - Client / Server

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


Patrick posted on Friday, December 21, 2007

Hi,

is it possible to define rules which only run on the server side?
E.g. sometimes a rule might need to "talk" to the database and do quite a few queries to verify that it (the rule) is valid. Naturally I would only want to run this rule just before persisting the entity and not on every IsValid request on the client.

Thanks,
Patrick

JoeFallon1 replied on Friday, December 21, 2007

Yes. I do this all the time. The rule uses a BO (like a command object) to return a result.When the rule is added I set the priority to 1 - the default priority is zero for all "simple rules". This means that the rule that hits the DB does not run unless all the simple rules are passed first.

But that may not be what you are asking...

Hmm. I would consider adding a flag to the BO which tells it if it is just a "standard" IsValid call or the "special" call that precedes a Save. By setting the flag to false initially, the rule can check it and skip over the DB rule and always return True. Then when you are ready to do a real save, your Save button code sets the flag to true and now when you check IsValid the DB rule will actually run for the first time. (And only time if everything is really valid.) You might also consider skipping over the framework call to IsValid so it doesn't run a 2nd time during the .Save method. I forget if there is an overload in the framework that does this or whether I added it to my base class.

Joe

 

Patrick replied on Friday, December 21, 2007

Thank you. This gave me an idea of what would even be nicer? A bit flag...

If there was a bit value for when the rule should run you could do all kinds of scenarios:
[Flags]enum Location {None, Server, Client, All}
[Flags]enum Situation{ Load = 0x01, Update = 0x02, Delete = 0x04, All = 0x08}

The business object knows whether it is on the server or on the client and the IsValid could have an overload to let it know from where it was called e.g. IsValid(Situation.Delete) or IsVaild(Situation.All)

So when you add a business rule you could say:
ValidationRules.AddRule(CommonRules.StringRequired, "GroupName");
or
ValidationRules.AddRule(ProductPriceRangeComparison, "ProductPrice", Location.Server, Situation.Update);
or
ValidationRules.AddRule(ProductNameSoundComparions, "ProductName", Location.Server | Location.Client, Situation.Update | Situation.Delete);

Thanks,
Patrick

Patrick replied on Tuesday, October 07, 2008

Patrick:

is it possible to define rules which only run on the server side?
E.g. sometimes a rule might need to "talk" to the database and do quite a few queries to verify that it (the rule) is valid. Naturally I would only want to run this rule just before persisting the entity and not on every IsValid request on the client.


Just a follow-up to ask if this is possible with CSLA.NET 3.6 at the moment.

Thanks,
Patrick

sergeyb replied on Tuesday, October 07, 2008

I do not think this is possible in 3.6.  I could offer a hack thought :- )

You can use ApplicationContext.LocalContext.Contans(SomeSpecialKey) and set the key say as part of user login on the client (or some other place during startup).  The same value will never be set on the server, so you have a stable check for execution location.  Not as nice of a solution as you propose, but is should work.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: Patrick [mailto:cslanet@lhotka.net]
Sent: Tuesday, October 07, 2008 5:14 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Broken Rules - Client / Server

 

Patrick:


is it possible to define rules which only run on the server side?
E.g. sometimes a rule might need to "talk" to the database and do quite a few queries to verify that it (the rule) is valid. Naturally I would only want to run this rule just before persisting the entity and not on every IsValid request on the client.



Just a follow-up to ask if this is possible with CSLA.NET 3.6 at the moment.

Thanks,
Patrick


ajj3085 replied on Wednesday, October 08, 2008

I'd like to add a feature request.  Currently, Csla.ApplicationContext.ExecutionLocation returns where the code is executing... but it seems to be the physical execution location... so if you're using the local data portal, the value will never be Server.

Perhaps a LogicalExecutionLocation can be added... which "Server" meaning the code is running in the dataportal, regardless of whether or not the code is actually running on the client or the server.

Seems like this would address many of these issues more cleanly than the hack posted.  Smile [:)]

RockfordLhotka replied on Wednesday, October 08, 2008

There's already an item in the wish list for a "logical execution location" property.

ajj3085 replied on Wednesday, October 08, 2008

Oh, guess I should have checked it first. Smile [:)]

Copyright (c) Marimer LLC