Exemplo n.º 1
0
        private IDictionary FindResourcesFor(CultureInfo culture)
        {
            var results = new Dictionary <string, object>();

            var cacheKey = string.Concat(this.GetType().FullName, "|", this.resourceGroupKey, "|", (culture != null && culture != CultureInfo.InvariantCulture ? culture.Name : string.Empty));

            if (cache.Contains(cacheKey))
            {
                results = cache.Get(cacheKey) as Dictionary <string, object>;
            }
            else
            {
                using (var connection = this.dbProvider.CreateConnection())
                {
                    connection.ConnectionString = this.connectionString;
                    connection.Open();

                    var command = connection.CreateCommand();
                    command.CommandType = CommandType.Text;

                    var sql = new StringBuilder();

                    sql.AppendFormat("SELECT [ResourceKey], [ResourceValue], [CultureCode] FROM [{0}] ", this.tableName);

                    sql.AppendFormat("WHERE ([ResourceGroup] = {0})", dbProvider.GetParameterPlaceholderFor("resourceGroup"));
                    command.AddParameter(p => { p.ParameterName = dbProvider.GetParameterName("resourceGroup"); p.Value = this.resourceGroupKey; });

                    sql.Append(" AND (LEN([CultureCode]) = 0");
                    if (culture != null && culture != CultureInfo.InvariantCulture)
                    {
                        sql.AppendFormat(" OR [CultureCode] = {0}", dbProvider.GetParameterPlaceholderFor("language"));
                        command.AddParameter(p => { p.ParameterName = dbProvider.GetParameterName("language"); p.Value = culture.TwoLetterISOLanguageName; });

                        if (culture.Name != culture.TwoLetterISOLanguageName)
                        {
                            sql.AppendFormat(" OR [CultureCode] = {0}", dbProvider.GetParameterPlaceholderFor("culture"));
                            command.AddParameter(p => { p.ParameterName = dbProvider.GetParameterName("culture"); p.Value = culture.Name; });
                        }
                    }
                    sql.Append(")");

                    // reverse ordering ensures correct fallback precedence
                    // {lang}-{region} => {lang} => NULL
                    sql.Append(" ORDER BY [CultureCode] DESC");

                    command.CommandText = sql.ToString();

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var resourceKey = reader.GetString(0);
                            if (!results.ContainsKey(resourceKey))
                            {
                                results.Add(resourceKey, reader.GetString(1));
                            }
                        }
                    }
                }

                cache.Add(cacheKey, results, new CacheItemPolicy());
            }

            return(results);
        }