Example #1
0
 internal static InstanceEntityHookFactory From <TRequest, TEntity>(
     IEntityHook <TRequest, TEntity> hook)
     where TEntity : class
 {
     return(new InstanceEntityHookFactory(
                hook,
                new FunctionEntityHook((request, entity, ct)
                                       => hook.Run((TRequest)request, (TEntity)entity, ct))));
 }
        /// <summary>
        /// Adds an entity hook instance.
        /// </summary>
        public TBuilder AddEntityHook(IEntityHook hook)
        {
            var hookType = hook.GetType();

            var baseHookType = hookType
                               .GetBaseTypes()
                               .SingleOrDefault(x =>
                                                x.IsGenericType && x.GetGenericTypeDefinition() == typeof(EntityHook <,>));

            if (baseHookType == null)
            {
                throw new ArgumentException($"Unable to add '{hookType}' as an entity hook for '{typeof(TRequest)}'.\r\n" +
                                            $"Entity hooks must inherit EntityHook<TRequest, TEntity>.");
            }

            var requestType = baseHookType.GenericTypeArguments[0];

            if (!requestType.IsAssignableFrom(typeof(TRequest)))
            {
                throw new ContravarianceException(nameof(AddEntityHook), requestType, typeof(TRequest));
            }

            var entityType = baseHookType.GenericTypeArguments[1];

            if (!entityType.IsAssignableFrom(typeof(TEntity)))
            {
                throw new ContravarianceException(nameof(AddEntityHook), entityType, typeof(TEntity));
            }

            var factoryMethod = typeof(InstanceEntityHookFactory)
                                .GetMethod(nameof(InstanceEntityHookFactory.From), BindingFlags.NonPublic | BindingFlags.Static)
                                .MakeGenericMethod(requestType, entityType);

            try
            {
                return(AddEntityHook((IEntityHookFactory)factoryMethod.Invoke(null, new object[] { hook })));
            }
            catch (TargetInvocationException e)
            {
                if (e.InnerException != null)
                {
                    throw e.InnerException;
                }

                throw e;
            }
        }
        public TBuilder AddEntityHook <TBaseRequest, TBaseEntity>(IEntityHook <TBaseRequest, TBaseEntity> hook)
            where TBaseEntity : class
        {
            if (!typeof(TBaseRequest).IsAssignableFrom(typeof(TRequest)))
            {
                throw new ContravarianceException(nameof(FilterWith), typeof(TBaseRequest), typeof(TRequest));
            }

            if (!typeof(TBaseEntity).IsAssignableFrom(typeof(TEntity)))
            {
                throw new ContravarianceException(nameof(FilterWith), typeof(TBaseEntity), typeof(TEntity));
            }

            EntityHooks.Add(InstanceEntityHookFactory.From(hook));

            return((TBuilder)this);
        }