CSLA.NET 6.0.0
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#if NET5_0_OR_GREATER
12using System.Runtime.Loader;
13
14using Csla.Runtime;
15#endif
16
17namespace Csla.Rules
18{
23 {
24#if NET5_0_OR_GREATER
25 private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, Tuple<string, BusinessRuleManager>>> _perTypeRules =
26 new Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, Tuple<string, BusinessRuleManager>>>();
27#else
28 private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, BusinessRuleManager>> _perTypeRules =
29 new Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, BusinessRuleManager>>();
30#endif
31
32 internal static BusinessRuleManager GetRulesForType(Type type, string ruleSet)
33 {
35 ruleSet = null;
36
37 var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
38
39#if NET5_0_OR_GREATER
40 var rulesInfo = _perTypeRules.Value
41 .GetOrAdd(
42 key,
43 (t) => AssemblyLoadContextManager.CreateCacheInstance(type, new BusinessRuleManager(), OnAssemblyLoadContextUnload)
44 );
45
46 return rulesInfo.Item2;
47#else
48 return _perTypeRules.Value.GetOrAdd(key, (t) => { return new BusinessRuleManager(); });
49#endif
50 }
51
56 internal static void CleanupRulesForType(Type type)
57 {
58 lock (_perTypeRules)
59 {
60 // 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.
61 var typeRules = _perTypeRules.Value.Where(value => value.Key.Type == type);
62 foreach (var key in typeRules)
63 _perTypeRules.Value.TryRemove(key.Key, out _);
64 }
65 }
66
67 internal static BusinessRuleManager GetRulesForType(Type type)
68 {
69 return GetRulesForType(type, null);
70 }
71
72 private class RuleSetKey
73 {
74 public Type Type { get; set; }
75 public string RuleSet { get; set; }
76
77 public override bool Equals(object obj)
78 {
79 if (!(obj is RuleSetKey other))
80 return false;
81 else
82 return this.Type.Equals(other.Type) && RuleSet == other.RuleSet;
83 }
84
85 public override int GetHashCode()
86 {
87 return (this.Type.FullName + RuleSet).GetHashCode();
88 }
89 }
90
94 public List<IBusinessRuleBase> Rules { get; private set; }
95
100 public bool Initialized { get; set; }
101
102 private BusinessRuleManager()
103 {
104 Rules = new List<IBusinessRuleBase>();
105 }
106
107#if NET5_0_OR_GREATER
108 private static void OnAssemblyLoadContextUnload(AssemblyLoadContext context)
109 {
110 lock (_perTypeRules)
111 AssemblyLoadContextManager.RemoveFromCache(_perTypeRules.Value, context, true);
112 }
113#endif
114 }
115}
Provides consistent context information between the client and server DataPortal objects.
const string DefaultRuleSet
The default RuleSet name
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.