Exemple #1
0
        public async Task <Notification[]> GetByIdsAsync(string[] ids, string responseGroup = null)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(GetByIdsAsync), string.Join("-", ids), responseGroup);
            var result   = await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                //It is so important to generate change tokens for all ids even for not existing objects to prevent an issue
                //with caching of empty results for non - existing objects that have the infinitive lifetime in the cache
                //and future unavailability to create objects with these ids.
                cacheEntry.AddExpirationToken(NotificationCacheRegion.CreateChangeToken(ids));

                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    var entities      = await repository.GetByIdsAsync(ids, responseGroup);
                    var notifications = entities.Select(n => n.ToModel(CreateNotification(n.Type, new UnregisteredNotification()))).ToArray();
                    //Load predefined notifications templates
                    if (EnumUtility.SafeParseFlags(responseGroup, NotificationResponseGroup.Full).HasFlag(NotificationResponseGroup.WithTemplates))
                    {
                        foreach (var notification in notifications)
                        {
                            var predefinedTemplates = _templateLoader.LoadTemplates(notification).ToList();
                            if (notification.Templates != null)
                            {
                                notification.Templates.AddRange(predefinedTemplates);
                            }
                            else
                            {
                                notification.Templates = predefinedTemplates;
                            }
                        }
                    }
                    return(notifications);
                }
            });

            return(result.Select(x => x.Clone() as Notification).ToArray());
        }