コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Refreshes the constraints that are registered in this Factory
        /// </summary>
        private void RefreshConstraints()
        {
            DbContext repo;

            using (contextProvider.AcquireContext(out repo))
            {
                RepoConstraintsInitializer.InitializeConstraints(this, this.Factory, repo);
            }
        }