/// <summary>
        /// Осуществляет регистрацию пользовательских OData-функций.
        /// </summary>
        /// <param name="container">Container of user functions.</param>
        /// <param name="dataService">Сервис данных.</param>
        public void RegisterODataUserFunctions(IFunctionContainer container, IDataService dataService)
        {
            Dictionary <string, Type> parametersTypes = new Dictionary <string, Type>();

            if (!container.IsRegistered("FunctionWithLcs1"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "entitySet", typeof(string) }
                };
                container.Register(new Function(
                                       "FunctionWithLcs1",
                                       (queryParameters, parameters) =>
                {
                    var type  = queryParameters.GetDataObjectType(parameters["entitySet"] as string);
                    var lcs   = queryParameters.CreateLcs(type);
                    var dobjs = dataService.LoadObjects(lcs);
                    return(dobjs.AsEnumerable());
                },
                                       typeof(IEnumerable <DataObject>),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionWithLcs2"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "entitySet", typeof(string) }, { "query", typeof(string) }
                };
                container.Register(new Function(
                                       "FunctionWithLcs2",
                                       (queryParameters, parameters) =>
                {
                    var type  = queryParameters.GetDataObjectType(parameters["entitySet"] as string);
                    var uri   = $"http://a/b/c?{parameters["query"]}";
                    var lcs   = queryParameters.CreateLcs(type, uri);
                    var dobjs = dataService.LoadObjects(lcs);
                    return(dobjs.Length);
                },
                                       typeof(int),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionString"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "stringParam", typeof(string) }
                };
                container.Register(new Function(
                                       "FunctionString",
                                       (queryParameters, parameters) => parameters["stringParam"],
                                       typeof(string),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionInt"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "intParam", typeof(int) }
                };
                container.Register(new Function(
                                       "FunctionInt",
                                       (queryParameters, parameters) => parameters["intParam"],
                                       typeof(int),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionEntity"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "intParam", typeof(int) }
                };
                container.Register(new Function(
                                       "FunctionEntity",
                                       (queryParameters, parameters) =>
                {
                    var result = (dataService as SQLDataService).Query <Страна>(Страна.Views.СтранаE).ToArray();
                    return(result[(int)parameters["intParam"]]);
                },
                                       typeof(Страна),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionEntitiesCollection"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "intParam", typeof(int) }
                };
                container.Register(new Function(
                                       "FunctionEntitiesCollection",
                                       (queryParameters, parameters) =>
                {
                    var top               = (int)parameters["intParam"];
                    var result            = (dataService as SQLDataService).Query <Страна>(Страна.Views.СтранаE).Take(top).ToArray();
                    queryParameters.Count = result.Length;
                    return(result);
                },
                                       typeof(IEnumerable <Страна>),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionSelectExpandEntity"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "intParam", typeof(int) }
                };
                container.Register(new Function(
                                       "FunctionSelectExpandEntity",
                                       (queryParameters, parameters) =>
                {
                    var result = (dataService as SQLDataService).Query <Медведь>(Медведь.Views.МедведьE).ToArray();
                    return(result[(int)parameters["intParam"]]);
                },
                                       typeof(Медведь),
                                       parametersTypes));
            }

            if (!container.IsRegistered("FunctionEnum"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "пол", typeof(tПол) }
                };
                container.Register(new Function(
                                       "FunctionEnum",
                                       (queryParameters, parameters) =>
                {
                    return((tПол)parameters["пол"]);
                },
                                       typeof(tПол),
                                       parametersTypes));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Осуществляет регистрацию пользовательских OData-actions.
        /// </summary>
        /// <param name="container">Container of user functions.</param>
        /// <param name="dataService">Сервис данных.</param>
        public void RegisterODataActions(IFunctionContainer container, IDataService dataService)
        {
            Dictionary <string, Type> parametersTypes = new Dictionary <string, Type>();

            if (!container.IsRegistered("ActionWithLcs"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "entitySet", typeof(string) }, { "query", typeof(string) }
                };
                container.Register(new Action(
                                       "ActionWithLcs",
                                       (queryParameters, parameters) =>
                {
                    var type  = queryParameters.GetDataObjectType(parameters["entitySet"] as string);
                    var uri   = $"http://a/b/c?{parameters["query"]}";
                    var lcs   = queryParameters.CreateLcs(type, uri);
                    var dobjs = dataService.LoadObjects(lcs);
                    return(dobjs.AsEnumerable());
                },
                                       typeof(IEnumerable <DataObject>),
                                       parametersTypes));
            }

            if (!container.IsRegistered("ActionVoid"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "entitySet", typeof(string) }, { "query", typeof(string) }
                };
                container.Register(new Action(
                                       "ActionVoid",
                                       (queryParameters, parameters) =>
                {
                    var type  = queryParameters.GetDataObjectType(parameters["entitySet"] as string);
                    var uri   = $"http://a/b/c?{parameters["query"]}";
                    var lcs   = queryParameters.CreateLcs(type, uri);
                    var dobjs = dataService.LoadObjects(lcs);
                    return(null);
                },
                                       typeof(void),
                                       parametersTypes));
            }

            if (!container.IsRegistered("ActionEnum"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "пол", typeof(tПол) }
                };
                container.Register(new Action(
                                       "ActionEnum",
                                       (queryParameters, parameters) =>
                {
                    return((tПол)parameters["пол"]);
                },
                                       typeof(tПол),
                                       parametersTypes));
            }

            if (!container.IsRegistered("ActionEntity"))
            {
                parametersTypes = new Dictionary <string, Type> {
                    { "entity", typeof(КлассСМножествомТипов) }, { "collection", typeof(IEnumerable <КлассСМножествомТипов>) }
                };
                container.Register(new Action(
                                       "ActionEntity",
                                       (queryParameters, parameters) =>
                {
                    IEnumerable <КлассСМножествомТипов> collection = parameters["collection"] as IEnumerable <КлассСМножествомТипов>;
                    var item = collection.ToArray()[0];
                    return(collection);
                },
                                       typeof(IEnumerable <КлассСМножествомТипов>),
                                       parametersTypes));
            }
        }