Ejemplo n.º 1
0
        /// <inheritdoc />
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this.unitOfWorkAttribute = invocationContext.GetOwningAttribute() as UnitOfWorkAttribute;
            var dbContextType       = this.unitOfWorkAttribute?.DbContextType;
            var uowPropertyPosition = invocationContext.GetParameterPosition <IUnitOfWork>();
            var uow = invocationContext.GetParameterValue <IUnitOfWork>(uowPropertyPosition);

            // If the Unit of Work is already set - skip the interception
            if (uow != null)
            {
                return;
            }

            // Get the Generic Type Definition
            var methodInfo = this.unitOfWorkFactory.GetType().GetMethods().FirstOrDefault(p => p.IsGenericMethod && p.Name == nameof(this.unitOfWorkFactory.Create));

            // Build a method with the DB Context Type
            var method = methodInfo?.MakeGenericMethod(dbContextType);

            // Create the new UnitOfWork
            this.unitOfWork = (IUnitOfWork)method?.Invoke(this.unitOfWorkFactory, null);

            // Replace the UOW on the method with the new one
            invocationContext.SetParameterValue(uowPropertyPosition, this.unitOfWork);
        }
Ejemplo n.º 2
0
        protected override void BeforeInvoke(InvocationContext invocationContext)
        {
            _logger       = _loggerFactory.CreateLogger(invocationContext.GetOwningType());
            _logAttribute = invocationContext.GetOwningAttribute() as LogAttribute;

            var level = _logAttribute.LogLevel;

            _logger.Log(level, "{owningType}: Method executing: {methodName}", invocationContext.GetOwningType(), invocationContext.GetExecutingMethodName());
        }
Ejemplo n.º 3
0
        protected override void BeforeInvoke(InvocationContext invocationContext)
        {
            _attribute = invocationContext.GetOwningAttribute() as UnitOfWorkAttribute;

            var uowParamPosition = invocationContext.GetParameterPosition <IUnitOfWork>();
            var uow = invocationContext.GetParameterValue <IUnitOfWork>(uowParamPosition);

            if (uow != null)
            {
                return;
            }

            invocationContext.SetParameterValue(uowParamPosition, _unitOfWork.Value);
        }
        protected override void AfterInvoke(InvocationContext invocationContext, object returnValue)
        {
            var expirationTime = (invocationContext.GetOwningAttribute() as CacheAttribute)
                                 .ExpireIn;

            if (expirationTime > 0)
            {
                _memoryCache.Set(invocationContext.GetExecutingMethodName(), returnValue, TimeSpan.FromMilliseconds(expirationTime));
            }
            else
            {
                _memoryCache.Set(invocationContext.GetExecutingMethodName(), returnValue);
            }
        }