2 using System.Collections.Generic;
5 using System.Web.DynamicData;
6 using System.Web.DynamicData.ModelProviders;
8 using MonoTests.DataSource;
10 namespace MonoTests.ModelProviders
12 public class DynamicDataContainerColumnProvider : ColumnProvider
14 DynamicDataColumn column;
15 bool associationResolved;
17 public override AssociationProvider Association {
19 ResolveAssociations ();
20 return base.Association;
24 base.Association = value;
28 public DynamicDataContainerColumnProvider (DynamicDataContainerTableProvider owner, DynamicDataColumn column)
32 throw new ArgumentNullException ("column");
36 Type columnType = column.DataType;
37 if (columnType == null)
38 throw new InvalidOperationException ("column.DataType must not be null for column '" + column.Name + "'");
41 ColumnType = columnType;
42 Nullable = columnType.IsGenericType && typeof (Nullable<>).IsAssignableFrom (columnType.GetGenericTypeDefinition ());
43 IsPrimaryKey = column.PrimaryKey;
46 public void ResolveAssociations ()
48 if (associationResolved)
51 associationResolved = true;
52 string associated = column.AssociatedTo;
53 if (String.IsNullOrEmpty (associated))
56 string[] names = associated.Split (new char[] { '.' });
57 if (names.Length != 2)
58 throw new ApplicationException ("Only associations of type Table.Column are supported");
59 string tableName = names[0];
60 string columnName = names[1];
62 TableProvider tableProvider = null;
64 tableProvider = Table.DataModel.Tables.First<TableProvider> ((TableProvider tp) => {
65 if (tp.Name == tableName)
73 if (tableProvider == null)
76 ColumnProvider toColumn = null;
79 toColumn = tableProvider.Columns.First<ColumnProvider> ((ColumnProvider cp) => {
80 if (cp.Name == columnName)
91 IsForeignKeyComponent = true;
92 Association = new DynamicDataAssociationProvider (column.AssociationDirection, this, toColumn);