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;
10 using MonoTests.Common;
12 namespace MonoTests.ModelProviders
14 public class DynamicDataContainerModelProvider <TContext> : DataModelProvider
16 IDynamicDataContainer <TContext> container;
18 ReadOnlyCollection<TableProvider> tables;
20 public IDynamicDataContainer <TContext> Container
24 if (container != null)
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 + "'.");
35 public override Type ContextType
39 return typeof (TContext);
43 throw new InvalidOperationException ("Setting the context type on this provider is not supported.");
47 public DynamicDataContainerModelProvider ()
49 Type genType = typeof (TestDataContainer<>).GetGenericTypeDefinition ();
50 this.containerType = genType.MakeGenericType (new Type[] { ContextType });
53 public DynamicDataContainerModelProvider (IDynamicDataContainer <TContext> container)
55 if (container == null)
56 throw new ArgumentNullException ("container");
58 this.container = container;
61 public override object CreateContext ()
63 return Activator.CreateInstance (ContextType);
66 public override ReadOnlyCollection<TableProvider> Tables
72 tables = LoadTables ();
78 void ResolveAssociations ()
80 foreach (var t in Tables) {
81 var table = t as DynamicDataContainerTableProvider <TContext>;
84 table.ResolveAssociations ();
88 ReadOnlyCollection<TableProvider> LoadTables ()
90 List<DynamicDataTable> containerTables = Container.GetTables ();
92 if (containerTables == null || containerTables.Count == 0)
93 return new ReadOnlyCollection<TableProvider> (new List<TableProvider> ());
95 var tables = new List<TableProvider> ();
96 foreach (var table in containerTables)
97 tables.Add (new DynamicDataContainerTableProvider <TContext>(this, table));
99 return new ReadOnlyCollection<TableProvider> (tables);