public DependencyState GetState(DependencyStateContext context)
        {
            if (!(context is CheckoutShippingSelectionDependencyStateContext))
            {
                return(null);
            }

            // Proceed through a chain of calls to get the customer's selected shipping method,
            // shipping address, and billing address.
            var checkoutStateContext = (CheckoutShippingSelectionDependencyStateContext)context;

            var customer = new Customer(checkoutStateContext.CustomerId, true);

            var persistedCheckoutContext = PersistedCheckoutContextProvider.LoadCheckoutContext(customer);

            var selectedPaymentMethod = PaymentMethodInfoProvider.GetPaymentMethodInfo(
                paymentMethod: customer.RequestedPaymentMethod,
                gateway: AppLogic.ActivePaymentGatewayCleaned());

            var checkoutState = CheckoutSelectionProvider.GetCheckoutSelection(
                customer: customer,
                selectedPaymentMethod: selectedPaymentMethod,
                persistedCheckoutContext: persistedCheckoutContext);

            // Return a state that is the hash of the shipping method ID, billing address, and shipping address.
            return(new DependencyState(
                       context: context,
                       state: HashProvider.Hash(
                           new object[] { checkoutState.SelectedShippingMethodId }
                           .Concat(GetAddressState(checkoutState.SelectedBillingAddress))
                           .Concat(GetAddressState(checkoutState.SelectedShippingAddress, persistedCheckoutContext.ShippingEstimate)))));
        }
Ejemplo n.º 2
0
        public DependencyState GetState(DependencyStateContext context)
        {
            if (!(context is CacheEntryDependencyStateContext))
            {
                return(null);
            }

            // Attempt to get the requested entry from the cache
            var cacheContext = (CacheEntryDependencyStateContext)context;
            var entry        = CacheProvider.Check(cacheContext.Key);

            // If the entry doesn't exist, return a "null" state.
            if (entry == null)
            {
                return(new DependencyState(
                           context: context,
                           state: 0));
            }

            // Create a state that is the hash of the matching keys' states.
            var hash = entry
                       .DependencyStates
                       .Where(state => !(state.Context is CacheEntryDependencyStateContext))
                       .Aggregate(0L, (agg, state) => agg ^ state.State);

            return(new DependencyState(
                       context: context,
                       state: hash));
        }
Ejemplo n.º 3
0
 public DependencyState GetState(DependencyStateContext context)
 {
     // Delegate down to all managers, returning the first non-null result
     return(Managers
            .Select(manager => manager.GetState(context))
            .Where(state => state != null)
            .FirstOrDefault());
 }
Ejemplo n.º 4
0
        public DependencyState GetState(DependencyStateContext context)
        {
            if (!(context is QueryDependencyStateContext))
            {
                return(null);
            }

            var queryContext    = (QueryDependencyStateContext)context;
            var updatedOnValues = new List <object>();

            // Connect to the database and run the query
            using (var connection = new SqlConnection(DB.GetDBConn()))
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = queryContext.Query;

                    var sqlParameters = queryContext
                                        .Parameters
                                        .Select(kvp => new SqlParameter(kvp.Key, kvp.Value))
                                        .ToArray();

                    command.Parameters.AddRange(sqlParameters);

                    connection.Open();
                    using (var reader = command.ExecuteReader())
                        while (reader.Read())
                        {
                            // Read out all results as nullable DateTime's and store them in a list
                            var updatedOn = reader.GetFieldValue <DateTime?>(0);
                            if (updatedOn != null)
                            {
                                updatedOnValues.Add(updatedOn.Value);
                            }
                        }
                }

            // Return a state that is the result of hashing all of the returned DateTimes together.
            return(new DependencyState(
                       context: context,
                       state: HashProvider.Hash(updatedOnValues)));
        }
Ejemplo n.º 5
0
        public DependencyState GetState(DependencyStateContext context)
        {
            if (!(context is AppConfigValueDependencyStateContext))
            {
                return(null);
            }

            // Get the requested AppConfig
            var appConfigValueContext = (AppConfigValueDependencyStateContext)context;
            var appConfig             = AppConfigProvider.GetAppConfig(appConfigValueContext.Name, appConfigValueContext.StoreId, cascadeToDefault: true);

            // If the AppConfig doesn't exist or it doesn't match predicate, return a
            // state that will be different every time. The required behavior is that any AppConfig
            // that doesn't match the predicate should cause the dependecy to fail.
            if (appConfig == null || !appConfigValueContext.Predicate(appConfig.ConfigValue))
            {
                // Generate a random long
                var rng       = new Random();
                var rngBuffer = new byte[8];
                rng.NextBytes(rngBuffer);
                var failedHash = BitConverter.ToInt64(rngBuffer, 0);

                // Return that as the state
                return(new DependencyState(
                           context: context,
                           state: failedHash));
            }

            // If the AppConfig exists and matches the predicate, return a state that tracks any changes in the AppConfig.
            return(new DependencyState(
                       context: context,
                       state: HashProvider.Hash(new object[]
            {
                appConfigValueContext.Name,
                appConfigValueContext.StoreId,
                appConfig.StoreId,
                appConfig.UpdatedOn.Ticks,
            })));
        }
Ejemplo n.º 6
0
        public DependencyState GetState(DependencyStateContext context)
        {
            if (!(context is AppConfigDependencyStateContext))
            {
                return(null);
            }

            // Get the requested AppConfig
            var appConfigContext = (AppConfigDependencyStateContext)context;
            var appConfig        = AppConfigProvider.GetAppConfigContextless(appConfigContext.Name, appConfigContext.StoreId, cascadeToDefault: true);

            // There are many cases to consider: creation, deletion, and modification; and default
            // store fallbacks in all three cases.

            // If the requested AppConfig doesn't exist, create a state that doesn't include the value
            if (appConfig == null)
            {
                return(new DependencyState(
                           context: context,
                           state: HashProvider.Hash(new object[]
                {
                    appConfigContext.Name,
                    appConfigContext.StoreId,
                })));
            }

            // Create a state that includes both the requested and actual values.
            return(new DependencyState(
                       context: context,
                       state: HashProvider.Hash(new object[]
            {
                appConfigContext.Name,                                          // The name of the AppConfig
                appConfigContext.StoreId,                                       // The requested store ID
                appConfig.StoreId,                                              // The actual store ID, in case the requested
                // store ID used the default store's value
                appConfig.UpdatedOn,                                            // The timestamp of the last change
            })));
        }
Ejemplo n.º 7
0
 public DependencyState(DependencyStateContext context, long state)
 {
     Context = context;
     State   = state;
 }