/// <summary> /// Create a new instance of the factory. /// </summary> public Factory() { if (!_dataObjectInformationMap.ContainsKey(_objectType)) { throw new Exception("No mapping information for " + _objectType.FullName); } _dataObjectInformation = _dataObjectInformationMap[_objectType]; if (_dataObjectInformation.PrimaryKey.PropertyType != typeof(TPrimaryKey)) { throw new Exception("Data object primary key type does not match factory primary key type"); } if (_caches.ContainsKey(_objectType)) { _cache = (DatabaseCache <TDbObject, TPrimaryKey>)_caches[_objectType]; } else { lock (_caches) { _cache = new DatabaseCache <TDbObject, TPrimaryKey>(_dataObjectInformation); _caches.Add(_objectType, _cache); } } _type = GetType().Name; }
/// <summary> /// Create a new instance of this class. /// </summary> /// <param name="information"> /// The metadata object for this class. /// </param> internal DatabaseCache(DataObjectInformation information) { if (information == null) { throw new ArgumentNullException("information"); } _objectInformation = information; _type = typeof(TDbObject).Name; }
/// <summary> /// Static constructor for database objects which enumerates all loaded assemblies to setup the data environment. /// </summary> static Factory() { // Inspect the type of DB object and construct a column list, a column mapping for // all DbObject types that are loaded. Type type = typeof(DbObject); List <Type> types = new List <Type>(); foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) { try { Type[] assemblyTypes = ass.GetTypes(); foreach (Type assemblyType in assemblyTypes) { try { if ((type.IsAssignableFrom(assemblyType)) && (!assemblyType.IsAbstract)) { types.Add(assemblyType); } } catch { } } } catch { } } foreach (Type t in types) { object[] classAttributes = t.GetCustomAttributes(typeof(DataObjectAttribute), true); if ((classAttributes == null) || (classAttributes.Length < 1)) { continue; } DataObjectAttribute dataObjectAttribute = (DataObjectAttribute)classAttributes[0]; DataObjectInformation objectInformation = new DataObjectInformation() { TableName = dataObjectAttribute.TableName, DefaultOrderBy = dataObjectAttribute.DefaultOrderBy }; PropertyInfo[] properties = t.GetProperties(); if (properties == null) { continue; } foreach (PropertyInfo property in properties) { object[] attributes = property.GetCustomAttributes(typeof(DataPropertyMappingAttribute), true); if ((attributes == null) || (attributes.Length < 1)) { continue; } DataPropertyMappingAttribute propertyMapping = (DataPropertyMappingAttribute)attributes[0]; if (string.IsNullOrEmpty(propertyMapping.DatabaseColumn)) { propertyMapping.DatabaseColumn = property.Name; } if (property.Name == dataObjectAttribute.PrimaryKey) { objectInformation.PrimaryKey = property; } objectInformation.DatabaseColumnMap.Add(property, propertyMapping.DatabaseColumn); } if ((objectInformation.Properties.Count() < 0) || (string.IsNullOrEmpty(objectInformation.TableName)) || (objectInformation.PrimaryKey == null)) { continue; } _dataObjectInformationMap.Add(t, objectInformation); } }