Ejemplo n.º 1
0
        public static T GetStateObject <T>(BotStateContext context, Func <T> fnDefault) where T : class
        {
            T   result        = null;
            var procedureName = "GetStateObject";
            var dbOperation   = new Action <SqlCommand>(command =>
            {
                // Do Something
                AddStateParameters(command, context);
                var r = command.ExecuteReader();

                var builders = GetJSONResultsFromReader(r);
                string data  = builders[0].ToString();
                if (data != "[]")
                {
                    result = JsonConvert.DeserializeObject <T>(
                        data);
                }

                if (result == null)
                {
                    result = fnDefault();
                }
            });

            ExecuteSQLStoredProc(procedureName, dbOperation, ConnectionContext.State);
            return(result);
        }
Ejemplo n.º 2
0
        public static async Task PersistInStateAsync <T, U>(IServiceProvider serviceProvider, ITurnContext context, U value, bool useDbHelper = false)
            where T : BotState
            where U : class
        {
            var target = typeof(U);

            if (!useDbHelper)
            {
                var sp       = serviceProvider.GetService <T>();
                var accessor = sp.CreateProperty <U>(target.Name);
                await accessor.SetAsync(context, value);

                await sp.SaveChangesAsync(context, true);
            }
            else
            {
                bool isUserState = (typeof(T) == typeof(UserState));
                var  bsc         = new BotStateContext
                {
                    Category = isUserState
                        ? BotStorageCategory.User : BotStorageCategory.Conversation,
                    ContextId = isUserState
                        ? context.Activity.From.Id : context.Activity.Conversation.Id,
                    PropertyName = target.Name
                };
                DBHelper.SaveStateObject(bsc, value);
            }
        }
Ejemplo n.º 3
0
        public static async Task <U> RetrieveFromStateAsync <T, U>(IServiceProvider serviceProvider, ITurnContext context, bool useDbHelper = false)
            where T : BotState
            where U : class, new()
        {
            U        value     = default(U);
            var      target    = typeof(U);
            Func <U> fnDefault = () =>
            {
                System.Diagnostics.Debug.WriteLine(context.Activity.From.Id);
                return(new U());
            };

            if (!useDbHelper)
            {
                var sp = serviceProvider.GetService <T>();

                var accessor = sp.CreateProperty <U>(target.Name);
                value = await accessor.GetAsync(context, fnDefault);
            }
            else
            {
                bool isUserState = (typeof(T) == typeof(UserState));
                var  bsc         = new BotStateContext
                {
                    Category = isUserState
                        ? BotStorageCategory.User : BotStorageCategory.Conversation,
                    ContextId = isUserState
                        ? context.Activity.From.Id : context.Activity.Conversation.Id,
                    PropertyName = target.Name
                };
                value = DBHelper.GetStateObject(bsc, fnDefault);
            }
            return(value);
        }
Ejemplo n.º 4
0
        public static void RemoveStateObject <T>(BotStateContext context) where T : class
        {
            var procedureName = "DeleteStateObject";
            var dbOperation   = new Action <SqlCommand>(command =>
            {
                // Do Something
                AddStateParameters(command, context);
                command.ExecuteNonQuery();
            });

            ExecuteSQLStoredProc(procedureName, dbOperation, ConnectionContext.State);
        }
Ejemplo n.º 5
0
        public static void SaveStateObject <T>(BotStateContext context, T value) where T : class
        {
            var procedureName = "PersistStateObject";
            var dbOperation   = new Action <SqlCommand>(command =>
            {
                // Do Something
                AddStateParameters(command, context);

                command.Parameters.Add("@state", SqlDbType.NVarChar);
                command.Parameters["@state"].Value = JsonConvert.SerializeObject(value);

                command.ExecuteNonQuery();
            });

            ExecuteSQLStoredProc(procedureName, dbOperation, ConnectionContext.State);
        }
Ejemplo n.º 6
0
 private static void AddStateParameters(SqlCommand command, BotStateContext context)
 {
     AddStateParameters(command, context.ContextId, context.Category, context.PropertyName);
 }