CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

SharedValidationRules and locking in CSLA 3.8.x

rated by 0 users
Answered (Verified) This post has 1 verified answer | 1 Reply | 2 Followers

Top 500 Contributor
31 Posts
Matthew Copeland posted on Wed, Jan 9 2013 1:40 PM

Hi,

Inspecting the code in SharedValidationRules class I noticed that the synchronization object used in the lock is _managers, the private dictionary which contains the ValidationRulesManager for each Type.

 

    internal static ValidationRulesManager GetManager(Type objectType, bool create)

    {

      ValidationRulesManager result = null;

      if (!_managers.TryGetValue(objectType, out result) && create)

      {

        lock (_managers)

        {

          if (!_managers.TryGetValue(objectType, out result))

          {

            result = new ValidationRulesManager();

            _managers.Add(objectType, result);

          }

        }

      }

      return result;

    }

 I was taught that the synchronization object itself is never blocked, and to always use a private variable of type object declared for the sole purpose of being the synch object.

Seeing the above code made me wonder if I had been taught wrong.

For example, does using _managers as the synch object result in blocking calls to RuleExistsFor?

    public static bool RulesExistFor(Type objectType)

    {

      return _managers.ContainsKey(objectType);

    }

Thanks,

Matthew

Answered (Verified) Verified Answer

Top 10 Contributor
9,270 Posts

It is recommended to use a private instance value as the target of the lock statement. Whether that's a real object or a 'new Object()' can vary.

The target object of a lock statement merely defines the scope of the lock. The basic recommendations are there to help people avoid deadlock or race conditions. But if you intentionally want to lock against something in multiple parts of your code then you need to make sure to use a common scope for your lock.

Rocky

Page 1 of 1 (2 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems