コード例 #1
0
        private IEnumerable <IDbSaveHook> GetSaveHookInstancesFor(IHookedEntity entry, HookStage stage, bool importantOnly)
        {
            if (entry.EntityType == null)
            {
                return(Enumerable.Empty <IDbSaveHook>());
            }

            IEnumerable <IDbSaveHook> hooks;

            // For request cache lookup
            var requestKey = new RequestHookKey(entry, stage, importantOnly);

            if (_hooksRequestCache.ContainsKey(requestKey))
            {
                hooks = _hooksRequestCache[requestKey];
            }
            else
            {
                hooks = _saveHooks
                        // Reduce by data context types
                        .Where(x => x.Metadata.DbContextType.IsAssignableFrom(entry.ContextType))
                        // Reduce by entity types which can be processed by this hook
                        .Where(x => x.Metadata.HookedType.IsAssignableFrom(entry.EntityType))
                        // When importantOnly, only include hook types with [ImportantAttribute]
                        .Where(x => !importantOnly || _importantSaveHookTypes.Contains(x.Metadata.ImplType))
                        // Exclude void hooks (hooks known to be useless for the current EntityType/State/Stage combination)
                        .Where(x => !_voidHooks.Contains(new HookKey(x.Metadata.ImplType, entry, stage)))
                        .Select(x => x.Value)
                        .ToArray();

                _hooksRequestCache.AddRange(requestKey, hooks);
            }

            return(hooks);
        }
コード例 #2
0
        private IEnumerable <IDbSaveHook> GetSaveHookInstancesFor(IHookedEntity entry, HookStage stage, HookImportance minHookImportance)
        {
            if (entry.EntityType == null)
            {
                return(Enumerable.Empty <IDbSaveHook>());
            }

            IEnumerable <IDbSaveHook> hooks;

            // For request cache lookup
            var requestKey = new RequestHookKey(entry, stage, minHookImportance);

            if (_hooksRequestCache.ContainsKey(requestKey))
            {
                hooks = _hooksRequestCache[requestKey];
            }
            else
            {
                hooks = _saveHooks
                        // Reduce by data context types
                        .Where(x => x.Metadata.DbContextType.IsAssignableFrom(entry.DbContext.GetType()))
                        // Reduce by entity types which can be processed by this hook
                        .Where(x => x.Metadata.HookedType.IsAssignableFrom(entry.EntityType))
                        // Only include hook types with Importance >= minHookImportance
                        .Where(x => x.Metadata.Importance >= minHookImportance)
                        // Exclude void hooks (hooks known to be useless for the current EntityType/State/Stage combination)
                        .Where(x => !_voidHooks.Contains(new HookKey(x.Metadata.ImplType, entry, stage)))
                        // Apply sort
                        .OrderBy(x => x.Metadata.Order)
                        // Get the hook instance
                        .Select(x => x.Value)
                        // Make array
                        .ToArray();

                _hooksRequestCache.AddRange(requestKey, hooks);
            }

            return(hooks);
        }
コード例 #3
0
        private IEnumerable <THook> GetHookInstancesFor <THook>(
            Type entityType,
            EntityState entityState,
            HookStage stage,
            bool importantOnly,
            IList <Lazy <IDbHook, HookMetadata> > hookList,
            HashSet <Type> importantHookTypes) where THook : IDbHook
        {
            IEnumerable <IDbHook> hooks;

            if (entityType == null)
            {
                return(Enumerable.Empty <THook>());
            }

            // For request cache lookup
            var requestKey = new RequestHookKey(entityType, entityState, stage, importantOnly);

            if (_hooksRequestCache.ContainsKey(requestKey))
            {
                hooks = _hooksRequestCache[requestKey];
            }
            else
            {
                hooks = hookList
                        // Reduce by entity types which can be processed by this hook
                        .Where(x => x.Metadata.HookedType.IsAssignableFrom(entityType))
                        // When importantOnly, only include hook types with [ImportantAttribute]
                        .Where(x => !importantOnly || importantHookTypes.Contains(x.Metadata.ImplType))
                        // Exclude void hooks (hooks known to be useless for the current EntityType/State/Stage combination)
                        .Where(x => !_voidHooks.Contains(new HookKey(x.Metadata.ImplType, entityType, entityState, stage)))
                        .Select(x => x.Value)
                        .ToArray();

                _hooksRequestCache.AddRange(requestKey, hooks);
            }

            return(hooks.Cast <THook>());
        }