Пример #1
0
 public DataTable(IDatabaseAnalyzer databaseAnalyzer,
                  Action <T, IDatabaseAnalyzer> onPerformLinking,
                  Action <T, IDataTableAnalyzer> onCommitChanges
                  )
 {
     OnCommitChanges  = onCommitChanges;
     OnPerformLinking = onPerformLinking;
     DatabaseAnalyzer = databaseAnalyzer;
 }
Пример #2
0
        private IDataTable BuildTableFromType(
            Type baseType,
            IDatabaseAnalyzer databaseAnalyzer,
            object onCommitChanges,
            object onPerformLinking
            )
        {
            var constructorParams = new List <object>()
            {
                databaseAnalyzer
            };
            var constructorTypes = new List <Type>()
            {
                typeof(IDatabaseAnalyzer)
            };

            if (onPerformLinking != null)
            {
                var actionBase = typeof(Action <,>);
                var actionType = actionBase.MakeGenericType(baseType, typeof(IDatabaseAnalyzer));
                constructorTypes.Add(actionType);
                constructorParams.Add(onPerformLinking);
            }
            if (onCommitChanges != null)
            {
                var actionBase = typeof(Action <,>);
                var actionType = actionBase.MakeGenericType(baseType, typeof(IDataTableAnalyzer));
                constructorTypes.Add(actionType);
                constructorParams.Add(onCommitChanges);
            }

            var tableBase = typeof(DataTable <>);
            var tableType = tableBase.MakeGenericType(baseType);

            return((IDataTable)tableType.GetConstructor(constructorTypes.ToArray()).Invoke(constructorParams.ToArray()));
        }
Пример #3
0
 public DataTable(IDatabaseAnalyzer databaseAnalyzer)
 {
     OnCommitChanges  = DefaultOnCommitChanges;
     DatabaseAnalyzer = databaseAnalyzer;
 }
Пример #4
0
 public DataTable(IDatabaseAnalyzer databaseAnalyzer, Action <T, IDataTableAnalyzer> onCommitChanges)
 {
     OnCommitChanges  = onCommitChanges;
     DatabaseAnalyzer = databaseAnalyzer;
 }
Пример #5
0
        /// <summary>
        /// Process the given type and construct the corresponding ObjectMap instance.
        /// </summary>
        /// <param name="broker">The PersistenceBroker to use for obtaining metadata on the type. If
        /// null is passed the DefaultProvider will be used.</param>
        /// <param name="type">The type to process.</param>
        /// <returns>An ObjectMap instance describing the type</returns>
        protected static ObjectMap ConstructMap(PersistenceBroker broker, Type type)
        {
            Check.Verify(IsTypeSupported(type), Error.UnsupportedType, type);
            ObjectMap map = new ObjectMap(broker, type);
            // get persistence attributes
            IList memberAttributeInfos = Reflector.FindMembers(Reflector.InstanceCriteria,
                                                               type, false, typeof(TableColumnAttribute),
                                                               typeof(PrimaryKeyAttribute), typeof(ForeignKeyAttribute),
                                                               typeof(ConcurrencyAttribute), typeof(SoftDeleteAttribute),
                                                               typeof(SequenceNameAttribute), typeof(InheritanceAttribute));

            foreach (MemberAttributeInfo mai in memberAttributeInfos)
            {
                map.Fields.Add(new FieldMap(map, mai));
            }
            // get custom view(s)
            memberAttributeInfos = Reflector.FindMembers(Reflector.InstanceCriteria, type, true, typeof(CustomViewAttribute));
            foreach (MemberAttributeInfo mai in memberAttributeInfos)
            {
                if (mai.MemberInfo is PropertyInfo)
                {
                    foreach (CustomViewAttribute cv in mai.Attributes)
                    {
                        PropertyInfo pi = mai.MemberInfo as PropertyInfo;
                        // create a viewmap for this attribute
                        ViewMap vm = new ViewMap(map, pi.Name, pi.PropertyType, cv);
                        // add it to the specified view
                        map.AddViewColumn(cv.ViewName, vm);
                    }
                }
            }
            // get validation(s)
            memberAttributeInfos = Reflector.FindMembers(Reflector.InstanceCriteria, type, false,
                                                         typeof(ValidatorBaseAttribute));
            foreach (MemberAttributeInfo mai in memberAttributeInfos)
            {
                foreach (ValidatorBaseAttribute vb in mai.Attributes)
                {
                    PropertyInfo pi = mai.MemberInfo as PropertyInfo;
                    // create a validation map for this attribute
                    ValidationMap va = new ValidationMap(map, pi.Name, pi.PropertyType, vb);
                    // add it to the specified view
                    map.Validations.Add(va);
                }
            }

            // analyze the actual database schema and update our internal maps accordingly
            if (GentleSettings.AnalyzerLevel != AnalyzerLevel.None)
            {
                if (broker == null)
                {
                    broker = new PersistenceBroker(type);
                }
                IDatabaseAnalyzer da = broker.Provider.GetAnalyzer();
                if (da != null)                  // not all persistence engines may support this
                {
                    da.UpdateObjectMap(map);
                }
            }
            // return the generated object <-> database map
            return(map);
        }