예제 #1
0
        private void RegisterVoidHook(IDbSaveHook hook, IHookedEntity entry, HookStage stage)
        {
            var hookType = hook.GetType();

            // Unregister from request cache (if cached)
            _hooksRequestCache.Remove(new RequestHookKey(entry, stage, false), hook);
            _hooksRequestCache.Remove(new RequestHookKey(entry, stage, true), hook);

            lock (_lock)
            {
                // Add to static void hooks set
                _voidHooks.Add(new HookKey(hookType, entry, stage));
            }
        }
예제 #2
0
        private bool HandledAlready(IHookedEntity entry, HookStage stage)
        {
            var entity = entry.Entity;

            if (entity == null || entity.IsTransientRecord())
            {
                return(false);
            }

            var key = new HookedEntityKey(entry, stage, entity.Id);

            if (_hookedEntities.Contains(key))
            {
                return(true);
            }

            _hookedEntities.Add(key);
            return(false);
        }
        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>());
        }
예제 #4
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);
        }
예제 #5
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);
        }
 private IEnumerable <IDbSaveHook> GetSaveHookInstancesFor(IHookedEntity entry, HookStage stage, bool importantOnly)
 {
     return(GetHookInstancesFor <IDbSaveHook>(
                entry.EntityType,
                entry.InitialState,
                stage,
                importantOnly,
                _saveHooks,
                _importantSaveHookTypes));
 }