Esempio n. 1
0
        public void GetQuery_Context()
        {
            MetaModel m = Utils.GetModel <MyDataContext2> ();

            var req = new FakeHttpWorkerRequest();
            var ctx = new HttpContext(req);

            HttpContext.Current = ctx;

            RouteCollection routes = RouteTable.Routes;

            routes.Clear();
            var route = new MyDynamicDataRoute("{table}/{action}.aspx")
            {
                Constraints  = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model        = m,
                RouteHandler = new MyDynamicDataRouteHandler()
            };

            routes.Add(route);

            MetaTable  t     = m.GetTable("FooTable");;
            IQueryable query = t.GetQuery(null);

            Assert.IsNotNull(query, "#A1");
            Assert.IsTrue(query.GetType() == typeof(Table <Foo>), "#A2");

            var foo = new Foo(true);

            AssertExtensions.Throws(() => t.GetQuery(foo), "#B1");
        }
Esempio n. 2
0
        private MetaTable GetTableFromTableName()
        {
            var tableName       = TableName;
            var contextTypeName = ContextTypeName;

            Debug.Assert(!String.IsNullOrEmpty(tableName));

            if (!String.IsNullOrEmpty(contextTypeName))
            {
                // context type allows to disambiguate table names
                Type      contextType = BuildManager.GetType(contextTypeName, /* throwOnError */ true, /* ignoreCase */ true);
                MetaModel model       = MetaModel.GetModel(contextType);
                MetaTable table       = model.GetTable(tableName, contextType);
                return(table);
            }
            else
            {
                var table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
                if (table == null)
                {
                    return(null);
                }
                return(table.Model.GetTable(tableName));
            }
        }
        // Make sure that if the Table or Action properties were used, they get added to
        // the Defaults dictionary
        private void EnsureRouteInitialize()
        {
            if (!_initialized)
            {
                lock (_initializationLock) {
                    if (!_initialized)
                    {
                        // Give the model to the handler
                        Debug.Assert(Model != null);
                        RouteHandler.Model = Model;

                        // If neither was specified, we don't need to do anything
                        if (Table == null && Action == null)
                        {
                            return;
                        }

                        // If we don't already have a Defaults dictionary, create one
                        if (Defaults == null)
                        {
                            Defaults = new RouteValueDictionary();
                        }

                        if (Table != null)
                        {
                            // Try to get the table just to cause a failure if it doesn't exist
                            var metaTable = Model.GetTable(Table);

                            Defaults[TableToken] = Table;
                        }

                        if (Action != null)
                        {
                            Defaults[ActionToken] = Action;
                        }

                        _initialized = true;
                    }
                }
            }
        }
Esempio n. 4
0
        public void GetTable()
        {
            MetaModel m = Utils.GetModel <MyDataContext2> ();
            MetaTable t;
            string    str  = null;
            Type      type = null;

            AssertExtensions.Throws <ArgumentNullException> (() => t = m.GetTable(str), "#A1");
            AssertExtensions.Throws <ArgumentNullException> (() => t = m.GetTable(type), "#A2");
            AssertExtensions.Throws <ArgumentNullException> (() => t = m.GetTable(null, null), "#A3");
            AssertExtensions.Throws <ArgumentNullException> (() => t = m.GetTable(null, typeof(Foo)), "#A4");
            AssertExtensions.Throws <ArgumentNullException> (() => t = m.GetTable("FooTable", null), "#A5");

            AssertExtensions.Throws <ArgumentException> (() => t = m.GetTable(String.Empty), "#B1");
            AssertExtensions.Throws <ArgumentException> (() => t = m.GetTable("NoSuchName"), "#B2");
            AssertExtensions.Throws <ArgumentException> (() => t = m.GetTable(typeof(object)), "#B3");
            AssertExtensions.Throws <ArgumentException> (() => t = m.GetTable("FooTable", typeof(object)), "#B4");
            AssertExtensions.Throws <ArgumentException> (() => t = m.GetTable("NoSuchTable", typeof(object)), "#B5");

            Assert.IsNotNull(t = m.GetTable("FooTable"), "#C1");
            Assert.AreEqual(typeof(Foo), t.EntityType, "#C2");
            Assert.IsNotNull(t = m.GetTable(typeof(Foo)), "#C3");
            Assert.AreEqual(typeof(Foo), t.EntityType, "#C4");
            Assert.IsNotNull(t = m.GetTable("FooTable", typeof(MyDataContext2)), "#C5");
            Assert.AreEqual(typeof(Foo), t.EntityType, "#C6");
        }
Esempio n. 5
0
        private void EnsureInit()
        {
            if (_column != null)
            {
                return;
            }

            // make sure we have a DataField
            if (String.IsNullOrEmpty(DataField))
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                  DynamicDataResources.FilterUserControlBase_MissingDataField,
                                                                  ID));
            }

            MetaTable table = null;

            if (!String.IsNullOrEmpty(ContextTypeName) || !String.IsNullOrEmpty(TableName))
            {
                // make sure both ContextTypeName and TableName are specified together
                if (String.IsNullOrEmpty(ContextTypeName))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      DynamicDataResources.FilterUserControlBase_MissingContextTypeName,
                                                                      ID));
                }
                if (String.IsNullOrEmpty(TableName))
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      DynamicDataResources.FilterUserControlBase_MissingTableName,
                                                                      ID));
                }

                Type      contextType = GetContextType(ContextTypeName);
                MetaModel model       = null;
                try {
                    model = MetaModel.GetModel(contextType);
                } catch (InvalidOperationException e) {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      DynamicDataResources.FilterUserControlBase_UnknownContextType,
                                                                      ID,
                                                                      contextType.FullName), e);
                }

                string tableName = TableName;
                try {
                    table = model.GetTable(tableName);
                } catch (ArgumentException e) {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      DynamicDataResources.FilterUserControlBase_InvalidTableName,
                                                                      ID,
                                                                      tableName), e);
                }
            }
            else
            {
                // get context information from request context
                table = DynamicDataRouteHandler.GetRequestMetaTable(HttpContext.Current);
                if (table == null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                      DynamicDataResources.FilterUserControlBase_CantInferInformationFromRequestUrl,
                                                                      ID));
                }
            }

            try {
                _column = table.GetColumn(DataField);
            } catch (InvalidOperationException e) {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                  DynamicDataResources.FilterUserControlBase_InvalidDataField,
                                                                  ID,
                                                                  DataField), e);
            }

            // create appropriate filter implementation based on column type
            if (_column is MetaForeignKeyColumn)
            {
                _filterDelegate = new ForeignKeyFilterDelegate(this);
            }
            else if (_column.ColumnType == typeof(bool) && !_column.IsCustomProperty)
            {
                _filterDelegate = new BooleanPropertyFilterDelegate(this);
            }
            else
            {
                _filterDelegate = new DefaultPropertyFilterDelegate(this);
            }
        }