2 // System.Web.Compilation.AggregateCacheDependency
5 // Marek Habersack (grendello@gmail.com)
7 // (C) 2006 Marek Habersack
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
35 namespace System.Web.Caching
37 public sealed class AggregateCacheDependency : CacheDependency
39 private object dependenciesLock = new object();
40 private List <CacheDependency> dependencies;
42 public AggregateCacheDependency ()
47 public void Add (params CacheDependency [] dependencies)
49 if (dependencies == null)
50 throw new ArgumentNullException ("dependencies");
51 if (dependencies.Length == 0)
54 bool somethingChanged = false;
55 foreach (CacheDependency dep in dependencies)
56 if (dep == null || dep.IsUsed)
57 throw new InvalidOperationException ("Cache dependency already in use");
58 else if (!somethingChanged && dep != null && dep.HasChanged)
59 somethingChanged = true;
61 lock (dependenciesLock) {
62 if (this.dependencies == null)
63 this.dependencies = new List <CacheDependency> (dependencies.Length);
64 foreach (CacheDependency dep in dependencies)
66 dep.DependencyChanged += new EventHandler (OnAnyChanged);
68 this.dependencies.AddRange (dependencies);
69 base.Start = DateTime.UtcNow;
72 base.NotifyDependencyChanged (this, null);
75 public override string GetUniqueID ()
77 if (dependencies == null || dependencies.Count == 0)
80 StringBuilder sb = new StringBuilder ();
81 lock (dependenciesLock) {
83 foreach (CacheDependency dep in dependencies) {
84 depid = dep.GetUniqueID ();
85 if (String.IsNullOrEmpty (depid))
91 return sb.ToString ();
94 internal override void DependencyDisposeInternal ()
96 if (dependencies != null && dependencies.Count > 0)
97 foreach (CacheDependency dep in dependencies)
98 dep.DependencyChanged -= new EventHandler (OnAnyChanged);
101 void OnAnyChanged (object sender, EventArgs args)
103 base.NotifyDependencyChanged (sender, args);