Esempio n. 1
0
        public static void InitializeConstraints(IConstraintFactory constraintFactory, PluginFactory pluginFactory, DbContext entityContext)
        {
            var constraintTypes = entityContext.Set <ConstraintDefinition>();
            Dictionary <string, object> emptyDict = new Dictionary <string, object>
            {
                { "Ask", Ask.Value },
                { "AskType", typeof(Ask) }
            };

            foreach (var constraintType in constraintTypes)
            {
                Type targetType           = (Type)ExpressionParser.Parse(constraintType.ConstraintType, emptyDict, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); });
                ConstraintConstructor cst = new ConstraintConstructor(targetType,
                                                                      constraintType.ConstraintIdentifier);
                constraintType.Parameters.OrderBy(n => n.ParameterOrder)
                .ForEach(
                    n =>
                    cst.Parameters.Add(
                        new ConstructorParameter(ExpressionParser.Parse(n.ParameterValue, emptyDict, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }),
                                                 (Type)ExpressionParser.Parse(n.ParameterType, emptyDict, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }))));
                constraintFactory.RegisterType(cst);
            }

            var dc = new DeciderContext {
                PluginFactory = pluginFactory, ConstraintFactory = constraintFactory, DbContext = entityContext
            };

            factory2Decisions.TryAdd(constraintFactory, dc);
            context2Decisions.TryAdd(entityContext, dc);
            constraintFactory.Disposed += UnRegisterFactory;
        }
        /// <summary>
        /// Gets the custom value for a Property that is decorated with a derived attribute
        /// </summary>
        /// <param name="originalObject">the original object that is applied to a viewmodel</param>
        /// <param name="requestInstance"></param>
        /// <returns>the value to assign to the target-property on a viewmodel</returns>
        protected override object GetCustomValueFor(object originalObject, Func <Type, object> requestInstance)
        {
            var dbContext = ContextHelper.GetDbContextFromEntity(originalObject);

            if (dbContext == null)
            {
                dbContext = (DbContext)requestInstance(typeof(DbContext));
            }

            if (dbContext != null)
            {
                var src = originalObject;
                if (DeciderMode != DeciderMode.This)
                {
                    src = ExpressionParser.Parse(PropertyName, originalObject);
                }

                var    deciderName = DeciderName;
                int    deciderId = -1;
                Type   entityType = typeof(object);
                string table = null, schema = null;
                if (string.IsNullOrEmpty(deciderName) && src != null)
                {
                    table = dbContext.GetTableName(src.GetType(), out entityType, out schema);
                    string fqName = $"[{schema}].[{table}]";
                    var    td     = dbContext.Set <TableDecider>().FirstOrDefault(n => n.TableName == fqName);
                    deciderId = td?.DeciderId ?? -1;
                }
                else
                {
                    deciderId = dbContext.Set <Decider>().FirstOrDefault(n => n.DisplayName == deciderName)?.DeciderId ?? -1;
                }

                if (deciderId != -1)
                {
                    IConstraintFactory factory  = RepoConstraintsInitializer.GetFactory(dbContext);
                    var           deciderRecord = dbContext.Set <Decider>().First(n => n.DeciderId == deciderId);
                    var           decider       = GetSimpleDecider(deciderRecord, entityType, factory, table, schema, (s, t) => { return(null); });
                    DeciderResult retVal        = new DeciderResult();
                    retVal.Result   = decider.Decide(src, DecisionMethod.Full, out var msg);
                    retVal.Messages = msg;
                    return(retVal);
                }

                if (src == null)
                {
                    return(new DeciderResult {
                        Messages = EntityNullMessage, Result = EntityNullResult
                    });
                }

                LogEnvironment.LogDebugEvent(null, "No capable decider was found.", (int)LogSeverity.Warning, null);
                return(null);
            }

            LogEnvironment.LogDebugEvent(null, "Unable to resolve the DbContext for provided model.", (int)LogSeverity.Warning, null);
            return(null);
        }
Esempio n. 3
0
        private static void UnRegisterFactory(object sender, EventArgs e)
        {
            IConstraintFactory factory = sender as IConstraintFactory;

            if (factory2Decisions.TryRemove(factory, out var context))
            {
                context2Decisions.TryRemove(context.DbContext, out _);
            }

            factory.Disposed -= UnRegisterFactory;
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a new <see cref="DispatcherValueConstraintBuilder"/> instance.
        /// </summary>
        /// <param name="constraintFactory">The <see cref="IConstraintFactory"/>.</param>
        /// <param name="rawText">The display name (for use in error messages).</param>
        public DispatcherValueConstraintBuilder(
            IConstraintFactory constraintFactory,
            string rawText)
        {
            if (constraintFactory == null)
            {
                throw new ArgumentNullException(nameof(constraintFactory));
            }

            if (rawText == null)
            {
                throw new ArgumentNullException(nameof(rawText));
            }

            _constraintFactory = constraintFactory;
            _rawText           = rawText;

            _constraints        = new Dictionary <string, List <IDispatcherValueConstraint> >(StringComparer.OrdinalIgnoreCase);
            _optionalParameters = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
        }
        private IDecider GetSimpleDecider(Decider decider, Type entityType, IConstraintFactory factory, string tableName, string schema, Func <string, Type, object> unknownParameters)
        {
            IDecider deciderObject = factory.CreateDecider(entityType, decider.ContextDriven);

            decider.Constraints.ForEach(n => deciderObject.AddConstraint(factory.GetConstraint(entityType, n.Constraint.ConstraintIdentifier,
                                                                                               (u, t) =>
            {
                if (u.Equals("tablename", StringComparison.OrdinalIgnoreCase) && t == typeof(string))
                {
                    return(tableName);
                }

                if (u.Equals("schema", StringComparison.OrdinalIgnoreCase) && t == typeof(string))
                {
                    return(schema);
                }

                return(unknownParameters(u, t));
            })));

            return(deciderObject);
        }
Esempio n. 6
0
 public RookValidatorStrategy(IConstraintFactory constraintFactory) : base(constraintFactory)
 {
 }
Esempio n. 7
0
        /// <summary>
        ///     Adds a table constraint.
        /// </summary>
        /// <param name="constraintFactory">
        ///     The constraint factory that instantiates a dedicated constraint instance for
        ///     this table.
        ///     </param>
        public void Add(IConstraintFactory <T> constraintFactory)
        {
            IConstraint <T> constraint = constraintFactory.Create();

            this.Add(constraint);
        }
Esempio n. 8
0
 protected override void InitializeServices(IServiceProvider services)
 {
     _constraintFactory = services.GetRequiredService <IConstraintFactory>();
 }
 public QueenValidatorStrategy(IConstraintFactory constraintFactory) : base(constraintFactory)
 {
 }
 public KnightValidatorStrategy(IConstraintFactory constraintFactory) : base(constraintFactory)
 {
 }
Esempio n. 11
0
 public CheesboardValidatorManager(IConstraintFactory constraintFactory)
 {
     _constraintFactory = constraintFactory;
 }
Esempio n. 12
0
 public BishopValidatorStrategy(IConstraintFactory constraintFactory) : base(constraintFactory)
 {
 }
Esempio n. 13
0
 protected ChessPawnValidatorStrategyBase(IConstraintFactory constraintFactory)
 {
     ConstraintFactory = constraintFactory;
 }