示例#1
0
        public MixInValidation(DynamicModel mixWith)
        {
            rules = new List <dynamic>();

            errors = new List <dynamic>();

            @this = mixWith;

            if (HasValidationCapabilities(mixWith))
            {
                mixWith.SetUnTrackedMember("Errors", new DynamicFunction(Errors));

                mixWith.SetUnTrackedMember("IsValid", new DynamicFunctionWithParam(IsValid));

                mixWith.SetUnTrackedMember("FirstError", new DynamicFunction(FirstError));

                IEnumerable <dynamic> validationRules = @this.Validates();

                foreach (var validationRule in validationRules)
                {
                    validationRule.Init(mixWith);

                    AddRule(validationRule);
                }
            }
        }
示例#2
0
        private dynamic EntityFor(DynamicModel model, dynamic attributes)
        {
            var entity = new Gemini(attributes);

            entity.SetMember(ForeignKeyFor(model), model.GetMember(Id()));

            return(Repository.Projection(entity));
        }
示例#3
0
        public void Init(DynamicModel model)
        {
            string foreignKeyName = string.IsNullOrEmpty(ForeignKey) ? ForeignKeyFor(model) : ForeignKey;

            model.SetUnTrackedMember(
                Singular(Repository),
                new DynamicFunction(() => Repository.SingleWhere(foreignKeyName + " = @0", model.GetMember(Id()))));
        }
示例#4
0
        public void Init(DynamicModel model)
        {
            ForeignKey = ForeignKeyFor(model);

            var toTable = Repository.GetType().Name;

            AddAssociationMethods(model, ForeignKey, toTable);
        }
示例#5
0
        public void Init(DynamicModel model)
        {
            string foreignKeyName = string.IsNullOrEmpty(ForeignKey) ? ForeignKeyFor(model) : ForeignKey;

            model.SetUnTrackedMember(
                Singular(Repository),
                Query(foreignKeyName, Repository.GetType().Name, through.GetType().Name, ForeignKeyFor(Repository), model));
        }
示例#6
0
        private DynamicFunction QueryIds(DynamicModel model)
        {
            return(() =>
            {
                IEnumerable <dynamic> models = (Query(model) as DynamicFunctionWithParam).Invoke(null);

                return models.Select(s => s.Id).ToList();
            });
        }
示例#7
0
 private void AddNewAssociationMethod(DynamicModels collection, DynamicModel model)
 {
     collection.SetMember(
         "New",
         new DynamicFunctionWithParam(attributes =>
     {
         return(EntityFor(attributes));
     }));
 }
示例#8
0
        private void AddAssociationMethod(DynamicModel model)
        {
            model.SetUnTrackedMember(
                Named,
                Query(model));

            model.SetUnTrackedMember(
                Singular(Named) + "Ids",
                QueryIds(model));
        }
示例#9
0
        public void Init(DynamicModel model)
        {
            fromColumn = ForeignKeyFor(model);

            toTable = Repository.GetType().Name;

            resolvedForeignKey = ForeignKey ?? ForeignKeyFor(Repository);

            AddAssociationMethod(model);
        }
示例#10
0
文件: Changes.cs 项目: kujotx/Oak
        public MixInChanges(dynamic dynamicModel)
        {
            @this = dynamicModel;

            originalValues = new Dictionary<string, object>(dynamicModel.TrackedHash());

            dynamicModel.SetUnTrackedMember("HasChanged", new DynamicFunctionWithParam(HasChanged));

            dynamicModel.SetUnTrackedMember("Original", new DynamicFunctionWithParam(Original));

            dynamicModel.SetUnTrackedMember("Changes", new DynamicFunctionWithParam(Changes));
        }
示例#11
0
        public MixInChanges(dynamic dynamicModel)
        {
            @this = dynamicModel;

            originalValues = new Dictionary <string, object>(dynamicModel.TrackedHash());

            dynamicModel.SetUnTrackedMember("HasChanged", new DynamicFunctionWithParam(HasChanged));

            dynamicModel.SetUnTrackedMember("Original", new DynamicFunctionWithParam(Original));

            dynamicModel.SetUnTrackedMember("Changes", new DynamicFunctionWithParam(Changes));
        }
示例#12
0
        public void Init(DynamicModel model)
        {
            string foreignKeyName = string.IsNullOrEmpty(ForeignKey) ? ForeignKeyFor(Repository) : ForeignKey;

            string primaryKeyName = string.IsNullOrEmpty(PrimaryKey) ? "Id" : PrimaryKey;

            string whereClause = string.Format("{0} = @0", primaryKeyName);

            model.SetUnTrackedMember(
                Named,
                new DynamicFunction(() => Repository.SingleWhere(whereClause, model.GetMember(foreignKeyName))));
        }
示例#13
0
        public MixInAssociation(DynamicModel mixWith)
        {
            if (!SupportsAssociations(mixWith))
            {
                return;
            }

            IEnumerable <dynamic> associations = (mixWith as dynamic).Associates();

            foreach (dynamic association in associations)
            {
                referencedAssociations.Add(association);
                association.Init(mixWith);
            }

            mixWith.SetUnTrackedMember("AssociationNamed", new DynamicFunctionWithParam(AssociationNamed));
        }
示例#14
0
        private DynamicFunctionWithParam Query(DynamicModel model)
        {
            return((options) =>
            {
                if (DiscardCache(options))
                {
                    cachedCollection = null;
                }

                if (cachedCollection != null)
                {
                    return cachedCollection;
                }

                cachedCollection = new DynamicModels(Repository.Query(SelectClause(model)));

                AddNewAssociationMethod(cachedCollection, model);

                return cachedCollection;
            });
        }
示例#15
0
        public bool Validate(DynamicModel entity)
        {
            dynamic value = entity.GetMember(Property);

            if (value == null && IgnoreNull == true)
            {
                return(true);
            }

            if (value == null)
            {
                return(false);
            }

            int length = value.Length;

            if (Minimum != null && length < Minimum)
            {
                return(false);
            }

            if (Maximum != null && length > Maximum)
            {
                return(false);
            }

            if (In != null && !In.Contains(length))
            {
                return(false);
            }

            if (Is != null && length != Is)
            {
                return(false);
            }

            return(true);
        }
示例#16
0
 private DynamicFunction Query(string fromColumn, string toTable, string throughTable, string @using, DynamicModel model)
 {
     return(() => Repository.Query(
                @"
         select {toTable}.*
         from {throughTable}
         inner join {toTable}
         on {throughTable}.{using} = {toTable}.Id
         where {fromColumn} = @0"
                .Replace("{toTable}", toTable)
                .Replace("{throughTable}", throughTable)
                .Replace("{using}", @using)
                .Replace("{fromColumn}", fromColumn), model.Expando.Id as object)
            .FirstOrDefault());
 }
示例#17
0
        private void AddAssociationMethods(DynamicModel model, string fromColumn, string toTable)
        {
            model.SetUnTrackedMember(Named, Query(fromColumn, model));

            model.SetUnTrackedMember(Singular(Named) + "Ids", QueryIds(fromColumn, model));
        }
示例#18
0
 public bool HasValidationCapabilities(DynamicModel mixWith)
 {
     return(mixWith.GetType().GetMethod("Validates") != null);
 }
示例#19
0
 public bool SupportsAssociations(DynamicModel mixWith)
 {
     return(mixWith.GetType().GetMethod("Associates") != null);
 }