NOP source code analysis of eight --- set of storage

We have basically achieved ISetting all over again, but it seems how its value taken out, I do not know.

In fact, through the festival we know, he was eventually carried out by SettingService.cs class GetAllSettingsCached method. Internal codes are as follows:

var query = from s in _settingRepository.TableNoTracking
                            orderby s.Name, s.StoreId
                            select s;

AsNoTracking is a function of EF, is not tracking state. To improve the efficiency with which the read-only case, Baidu. Ultimately removed from the database by settingRepository value. Setting the table is the table of course, by declaring private readonly IRepository <Setting> _settingRepository; can know.

Well, now all we know the value of all subclasses of ISetting are obtained by Setting the table read from the database.

I did not expect LocalizationSettings come out so much, that we should return to the original position and continue from there. Back WebWorkContext class method WorkingLanguage.

Language detectedLanguage = null;
                if (_localizationSettings.SeoFriendlyUrlsForLanguagesEnabled)
                {
                    //get language from URL
                    detectedLanguage = GetLanguageFromUrl();
                }

From the database SeoFriendlyUrlsForLanguagesEnabled is False, so go down.

if (detectedLanguage == null && _localizationSettings.AutomaticallyDetectLanguage)

AutomaticallyDetectLanguage database This value is also false.

Return false do not perform, we then read on:

was allLanguages = _languageService.GetAllLanguages (storeId: _storeContext.CurrentStore.Id);

This one, meaning look that is returned from all languages.

After New Year's Day, we continue to write notes.

LanguageService find language services under implementation class amespace Nop.Services.Localization: ILanguageService. Methods as below:

/// <summary>
        /// Gets all languages
        /// </summary>
        /// <param name="storeId">Load records allowed only in a specified store; pass 0 to load all records</param>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Language collection</returns>
        public virtual IList<Language> GetAllLanguages(bool showHidden = false, int storeId = 0)
        {
            string key = string.Format(LANGUAGES_ALL_KEY, showHidden);
            var languages = _cacheManager.Get(key, () =>
            {
                var query = _languageRepository.Table;
                if (!showHidden)
                    query = query.Where(l => l.Published);
                query = query.OrderBy(l => l.DisplayOrder);
                return query.ToList();
            });

            //store mapping
            if (storeId > 0)
            {
                languages = languages
                    .Where(l => _storeMappingService.Authorize(l, storeId))
                    .ToList();
            }
            return languages;
        }

Individuals now feel as if storeId is used to manage multi-point bar, or multiple repositories.

The first sentence format KEY.

The second sentence, the expression values ​​obtained from the cache by KEY, cached for 60 minutes.

Expression Functions repository you use the following statement:

private readonly IRepository<Language> _languageRepository;

Visible Language check database table is the table, get all the published final sorted DisplayOrder. Return to the list of languages.

The next sentence:

//find current customer language
                var languageId = this.CurrentCustomer.GetAttribute<int>(SystemCustomerAttributeNames.LanguageId,

currentCustomer code is as follows:

/// <summary>
        /// Gets or sets the current customer
        /// </summary>
        public virtual Customer CurrentCustomer
        {
            get
            {
                if (_cachedCustomer != null)
                    return _cachedCustomer;

                Customer customer = null;
                if (_httpContext == null || _httpContext is FakeHttpContext)
                {
                    //check whether request is made by a background task
                    //in this case return built-in customer record for background task
                    customer = _customerService.GetCustomerBySystemName(SystemCustomerNames.BackgroundTask);
                }

                //check whether request is made by a search engine
                //in this case return built-in customer record for search engines 
                //or comment the following two lines of code in order to disable this functionality
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    if (_userAgentHelper.IsSearchEngine())
                        customer = _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine);
                }

                //registered user
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    customer = _authenticationService.GetAuthenticatedCustomer();
                }

                //impersonate user if required (currently used for 'phone order' support)
                if (customer != null && !customer.Deleted && customer.Active)
                {
                    var impersonatedCustomerId = customer.GetAttribute<int?>(SystemCustomerAttributeNames.ImpersonatedCustomerId);
                    if (impersonatedCustomerId.HasValue && impersonatedCustomerId.Value > 0)
                    {
                        var impersonatedCustomer = _customerService.GetCustomerById(impersonatedCustomerId.Value);
                        if (impersonatedCustomer != null && !impersonatedCustomer.Deleted && impersonatedCustomer.Active)
                        {
                            //set impersonated customer
                            _originalCustomerIfImpersonated = customer;
                            customer = impersonatedCustomer;
                        }
                    }
                }

                //load guest customer
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    var customerCookie = GetCustomerCookie();
                    if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value))
                    {
                        Guid customerGuid;
                        if (Guid.TryParse(customerCookie.Value, out customerGuid))
                        {
                            var customerByCookie = _customerService.GetCustomerByGuid(customerGuid);
                            if (customerByCookie != null &&
                                //this customer (from cookie) should not be registered
                                !customerByCookie.IsRegistered())
                                customer = customerByCookie;
                        }
                    }
                }

                //create guest if not exists
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    customer = _customerService.InsertGuestCustomer();
                }


                //validation
                if (!customer.Deleted && customer.Active)
                {
                    SetCustomerCookie(customer.CustomerGuid);
                    _cachedCustomer = customer;
                }

                return _cachedCustomer;
            }
            set
            {
                SetCustomerCookie(value.CustomerGuid);
                _cachedCustomer = value;
            }
        }

= Let's analyze the Customer _customerService.GetCustomerBySystemName (SystemCustomerNames.BackgroundTask); this sentence.

public virtual Customer GetCustomerBySystemName(string systemName)
        {
            if (string.IsNullOrWhiteSpace(systemName))
                return null;

            var query = from c in _customerRepository.Table
                        orderby c.Id
                        where c.SystemName == systemName
                        select c;
            var customer = query.FirstOrDefault();
            return customer;
        }

It is to get equal systemName customer records from the Customer table. systemName is SystemCustomerNames.BackgroundTask, is the string BackgroundTask.

Then GetAttribute research methods Customer, which is an extension method:

public static TPropType GetAttribute<TPropType>(this BaseEntity entity,
            string key, IGenericAttributeService genericAttributeService, int storeId = 0)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");

            string keyGroup = entity.GetUnproxiedEntityType().Name;

            var props = genericAttributeService.GetAttributesForEntity(entity.Id, keyGroup);
            //little hack here (only for unit testing). we should write ecpect-return rules in unit tests for such cases
            if (props == null)
                return default(TPropType);
            props = props.Where(x => x.StoreId == storeId).ToList();
            if (props.Count == 0)
                return default(TPropType);

            var prop = props.FirstOrDefault(ga =>
                ga.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)); //should be culture invariant

            if (prop == null || string.IsNullOrEmpty(prop.Value))
                return default(TPropType);

            return CommonHelper.To<TPropType>(prop.Value);
        }

string keyGroup = entity.GetUnproxiedEntityType () Name;. type name of the class is obtained.

Find the implementation class GenericAttributeService IGenericAttributeService. method:

/// <summary>
        /// Get attributes
        /// </summary>
        /// <param name="entityId">Entity identifier</param>
        /// <param name="keyGroup">Key group</param>
        /// <returns>Get attributes</returns>
        public virtual IList<GenericAttribute> GetAttributesForEntity(int entityId, string keyGroup)
        {
            string key = string.Format(GENERICATTRIBUTE_KEY, entityId, keyGroup);
            return _cacheManager.Get(key, () =>were
            {
                 query = from ga in _genericAttributeRepository.Table
                            where ga.EntityId == entityId &&
                            ga.KeyGroup == keyGroup
                            select ga;
                var attributes = query.ToList();
                return attributes;
            });
        }

private readonly IRepository <GenericAttribute> _genericAttributeRepository; are declared with the library, the data is obtained from the table in accordance with EntityId GenericAttribute and KeyGroup.

 

Finally var language = allLanguages.FirstOrDefault (x => x.Id == languageId); language records obtained.

Guess you like

Origin www.cnblogs.com/runit/p/4196093.html