示例#1
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);
        }