CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BusinessRuleManager.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BusinessRuleManager.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Manages the list of rules for a business type.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11
12namespace Csla.Rules
13{
18 {
19 private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, BusinessRuleManager>> _perTypeRules =
20 new Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, BusinessRuleManager>>();
21
22 internal static BusinessRuleManager GetRulesForType(Type type, string ruleSet)
23 {
24 if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;
25
26 var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
27 return _perTypeRules.Value.GetOrAdd(key, (t) => { return new BusinessRuleManager(); });
28 }
29
34 internal static void CleanupRulesForType(Type type)
35 {
36 lock (_perTypeRules)
37 {
38 // the first RuleSet is already added to list when this check is executed so so if count > 1 then we have already initialized type rules.
39 var typeRules = _perTypeRules.Value.Where(value => value.Key.Type == type);
40 foreach (var key in typeRules)
41 _perTypeRules.Value.TryRemove(key.Key, out BusinessRuleManager manager);
42 }
43 }
44
45 internal static BusinessRuleManager GetRulesForType(Type type)
46 {
47 return GetRulesForType(type, null);
48 }
49
50 private class RuleSetKey
51 {
52 public Type Type { get; set; }
53 public string RuleSet { get; set; }
54
55 public override bool Equals(object obj)
56 {
57 if (!(obj is RuleSetKey other))
58 return false;
59 else
60 return this.Type.Equals(other.Type) && RuleSet == other.RuleSet;
61 }
62
63 public override int GetHashCode()
64 {
65 return (this.Type.FullName + RuleSet).GetHashCode();
66 }
67 }
68
72 public List<IBusinessRuleBase> Rules { get; private set; }
73
78 public bool Initialized { get; set; }
79
80 private BusinessRuleManager()
81 {
82 Rules = new List<IBusinessRuleBase>();
83 }
84 }
85}
Manages the list of rules for a business type.
bool Initialized
Gets or sets a value indicating whether the rules have been initialized.
List< IBusinessRuleBase > Rules
Gets the list of rule objects for the business type.