2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
6 using System.Web.DynamicData;
7 using System.Web.DynamicData.ModelProviders;
9 using MonoTests.DataSource;
11 namespace MonoTests.ModelProviders
13 public class DynamicDataContainerModelProvider : DataModelProvider
15 IDynamicDataContainer container;
17 ReadOnlyCollection<TableProvider> tables;
19 IDynamicDataContainer Container
23 if (container != null)
26 container = Activator.CreateInstance (containerType) as IDynamicDataContainer;
27 if (container == null)
28 throw new InvalidOperationException ("Failed to create an instance of container type '" + ContextType + "'.");
34 public override Type ContextType
38 return Container.ContainedType;
42 throw new InvalidOperationException ("Setting the context type on this provider is not supported.");
46 public DynamicDataContainerModelProvider (Type containerType)
48 if (containerType == null)
49 throw new ArgumentNullException ("contextType");
51 if (!typeof (IDynamicDataContainer).IsAssignableFrom (containerType))
52 throw new ArgumentException ("Container type must implement the IDynamicDataContainer interface.", "contextType");
54 this.containerType = containerType;
57 public DynamicDataContainerModelProvider (IDynamicDataContainer container)
59 if (container == null)
60 throw new ArgumentNullException ("container");
62 this.container = container;
65 public override object CreateContext ()
67 return Activator.CreateInstance (ContextType);
70 public override ReadOnlyCollection<TableProvider> Tables
76 tables = LoadTables ();
82 void ResolveAssociations ()
84 foreach (var t in Tables) {
85 var table = t as DynamicDataContainerTableProvider;
88 table.ResolveAssociations ();
92 ReadOnlyCollection<TableProvider> LoadTables ()
94 List<DynamicDataTable> containerTables = Container.GetTables ();
96 if (containerTables == null || containerTables.Count == 0)
97 return new ReadOnlyCollection<TableProvider> (new List<TableProvider> ());
99 var tables = new List<TableProvider> ();
100 foreach (var table in containerTables)
101 tables.Add (new DynamicDataContainerTableProvider (this, table));
103 return new ReadOnlyCollection<TableProvider> (tables);