2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / ModelProviders / DynamicDataContainerModelProvider.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Linq;
5 using System.Web;
6 using System.Web.DynamicData;
7 using System.Web.DynamicData.ModelProviders;
8
9 using MonoTests.DataSource;
10
11 namespace MonoTests.ModelProviders
12 {
13         public class DynamicDataContainerModelProvider : DataModelProvider
14         {
15                 IDynamicDataContainer container;
16                 Type containerType;
17                 ReadOnlyCollection<TableProvider> tables;
18
19                 IDynamicDataContainer Container
20                 {
21                         get
22                         {
23                                 if (container != null)
24                                         return container;
25
26                                 container = Activator.CreateInstance (containerType) as IDynamicDataContainer;
27                                 if (container == null)
28                                         throw new InvalidOperationException ("Failed to create an instance of container type '" + ContextType + "'.");
29
30                                 return container;
31                         }
32                 }
33
34                 public override Type ContextType
35                 {
36                         get
37                         {
38                                 return Container.ContainedType;
39                         }
40                         protected set
41                         {
42                                 throw new InvalidOperationException ("Setting the context type on this provider is not supported.");
43                         }
44                 }
45
46                 public DynamicDataContainerModelProvider (Type containerType)
47                 {
48                         if (containerType == null)
49                                 throw new ArgumentNullException ("contextType");
50
51                         if (!typeof (IDynamicDataContainer).IsAssignableFrom (containerType))
52                                 throw new ArgumentException ("Container type must implement the IDynamicDataContainer interface.", "contextType");
53
54                         this.containerType = containerType;
55                 }
56
57                 public DynamicDataContainerModelProvider (IDynamicDataContainer container)
58                 {
59                         if (container == null)
60                                 throw new ArgumentNullException ("container");
61
62                         this.container = container;
63                 }
64
65                 public override object CreateContext ()
66                 {
67                         return Activator.CreateInstance (ContextType);
68                 }
69
70                 public override ReadOnlyCollection<TableProvider> Tables
71                 {
72                         get
73                         {
74                                 if (tables != null)
75                                         return tables;
76                                 tables = LoadTables ();
77
78                                 return tables;
79                         }
80                 }
81
82                 void ResolveAssociations ()
83                 {
84                         foreach (var t in Tables) {
85                                 var table = t as DynamicDataContainerTableProvider;
86                                 if (t == null)
87                                         continue;
88                                 table.ResolveAssociations ();
89                         }
90                 }
91
92                 ReadOnlyCollection<TableProvider> LoadTables ()
93                 {
94                         List<DynamicDataTable> containerTables = Container.GetTables ();
95
96                         if (containerTables == null || containerTables.Count == 0)
97                                 return new ReadOnlyCollection<TableProvider> (new List<TableProvider> ());
98
99                         var tables = new List<TableProvider> ();
100                         foreach (var table in containerTables)
101                                 tables.Add (new DynamicDataContainerTableProvider (this, table));
102
103                         return new ReadOnlyCollection<TableProvider> (tables);
104                 }
105         }
106 }