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)
        {
            _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);
        }
Ejemplo n.º 3
0
        public void BeforeInvoke(InvocationContext invocationContext)
        {
            this._validateAttribute = invocationContext.GetAttributeFromMethod <ValidateAttribute>();

            if (typeof(IValidation).IsAssignableFrom(_validateAttribute._validatorType) == false)
            {
                throw new Exception("Wrong validation manager");
            }

            var validator = (IValidation)ActivatorUtilities.CreateInstance(_serviceProvider, _validateAttribute._validatorType);

            int entityPosition = invocationContext.GetParameterPosition(_validateAttribute._entityType);
            var entity         = invocationContext.GetParameterValue(entityPosition);

            if (entity != null)
            {
                validator.Validator(entity);
            }
        }