2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 using MonoTests.Common;
11
12 namespace MonoTests.ModelProviders
13 {
14         public class DynamicDataContainerModelProvider <TContext> : DataModelProvider
15         {
16                 IDynamicDataContainer <TContext> container;
17                 Type containerType;
18                 ReadOnlyCollection<TableProvider> tables;
19
20                 public IDynamicDataContainer <TContext> Container
21                 {
22                         get
23                         {
24                                 if (container != null)
25                                         return container;
26
27                                 container = Activator.CreateInstance (containerType) as IDynamicDataContainer <TContext>;
28                                 if (container == null)
29                                         throw new InvalidOperationException ("Failed to create an instance of container type '" + ContextType + "'.");
30
31                                 return container;
32                         }
33                 }
34
35                 public override Type ContextType
36                 {
37                         get
38                         {
39                                 return typeof (TContext);
40                         }
41                         protected set
42                         {
43                                 throw new InvalidOperationException ("Setting the context type on this provider is not supported.");
44                         }
45                 }
46
47                 public DynamicDataContainerModelProvider ()
48                 {
49                         Type genType = typeof (TestDataContainer<>).GetGenericTypeDefinition ();
50                         this.containerType = genType.MakeGenericType (new Type[] { ContextType });
51                 }
52
53                 public DynamicDataContainerModelProvider (IDynamicDataContainer <TContext> container)
54                 {
55                         if (container == null)
56                                 throw new ArgumentNullException ("container");
57
58                         this.container = container;
59                 }
60
61                 public override object CreateContext ()
62                 {
63                         return Activator.CreateInstance (ContextType);
64                 }
65
66                 public override ReadOnlyCollection<TableProvider> Tables
67                 {
68                         get
69                         {
70                                 if (tables != null)
71                                         return tables;
72                                 tables = LoadTables ();
73
74                                 return tables;
75                         }
76                 }
77
78                 void ResolveAssociations ()
79                 {
80                         foreach (var t in Tables) {
81                                 var table = t as DynamicDataContainerTableProvider <TContext>;
82                                 if (t == null)
83                                         continue;
84                                 table.ResolveAssociations ();
85                         }
86                 }
87
88                 ReadOnlyCollection<TableProvider> LoadTables ()
89                 {
90                         List<DynamicDataTable> containerTables = Container.GetTables ();
91
92                         if (containerTables == null || containerTables.Count == 0)
93                                 return new ReadOnlyCollection<TableProvider> (new List<TableProvider> ());
94
95                         var tables = new List<TableProvider> ();
96                         foreach (var table in containerTables)
97                                 tables.Add (new DynamicDataContainerTableProvider <TContext>(this, table));
98
99                         return new ReadOnlyCollection<TableProvider> (tables);
100                 }
101         }
102 }