/// <summary>
 /// Retrieve a list of objects of the given type constrained by the properties of
 /// this object instance.
 /// </summary>
 /// <param name="type">The type of objects being retrieved</param>
 /// <param name="propertyNames">A list of property names on the current object</param>
 /// <returns>A collection of objects of the given type</returns>
 protected virtual IList RetrieveList(Type type, params string[] propertyNames)
 {
     if (GetType().Equals(type))
     {
         // if the list type we're selecting is same type as self no foreign key
         // translation is required - simply use the name given
         return(broker.RetrieveList(type, Key.GetKey(broker, true, this, propertyNames)));
     }
     else
     {
         // get the key with values from current object
         Key key = Key.GetKey(broker, true, this, propertyNames);
         return(broker.RetrieveList(type, key));
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Retrieve all objects of a certain type (plain select of all rows in a the
 /// table, i.e. a statement without any where-clause).
 /// </summary>
 /// <param name="broker">The PersistenceBroker and associated provider used fetch the data</param>
 /// <param name="type">The type of the Persistent descendant to retrieve</param>
 /// <returns>A collection of objects of the given type</returns>
 public static IList RetrieveList(PersistenceBroker broker, Type type)
 {
     if (broker == null)
     {
         // use default broker instance by use of the Broker class
         return(Broker.RetrieveList(type));
     }
     else             // use supplied broker instance
     {
         return(broker.RetrieveList(type));
     }
 }
Ejemplo n.º 3
0
 protected virtual void InitList(Type viaType, params Type[] relationTypes)
 {
     if (listType == GentleListType.StandAlone)
     {
         broker.RetrieveList(containedMap.Type, this);
     }
     else if (listType == GentleListType.OneToMany)
     {
         // no relation objects for 1:n relationships
         viaInstances = null;
         mappings     = containedMap.GetForeignKeyMappings(parentMap, true);
         if (mappings.Count == 0)
         {
             mappings = viaMap.GetForeignKeyMappings(parentMap, false);
         }
         Check.Verify(mappings.Count > 0, Error.DeveloperError,
                      "The type {0} does not contain a foreign key reference to type {1}.",
                      parentMap.Type, containedMap.Type);
         Check.Verify(mappings.Count == 1, Error.NotImplemented,
                      "GentleList for 1:n relations can not be used with composite keys.");
         // populate self with any existing entries matching the current parent
         Key key = new Key(parentMap.Type, true);
         IDictionaryEnumerator iterator = mappings.GetEnumerator();
         while (iterator.MoveNext())
         {
             // construct a key to read the data; first obtain the referenced value from
             // the parent object (this is the constraint value used in the select)
             FieldMap fm = iterator.Value as FieldMap;
             object   referencedValue = fm.GetValue(parent);
             // if class references self make sure to pick the outgoing column
             if (containedMap.Type == parentMap.Type && !fm.IsForeignKey)
             {
                 fm = iterator.Key as FieldMap;
             }
             key[fm.MemberName] = referencedValue;
         }
         broker.RetrieveList(containedMap.Type, key, this);
     }
     else if (listType == GentleListType.ManyToMany)
     {
         // create relation for n:m management
         Type[] relatedTypes = Merge(containedMap.Type, relationTypes);
         viaInstances = new GentleRelation(broker, viaType, parent, relatedTypes);
         // populate the list with any existing entries matching the current relation entries
         ObjectMap  viaMap = ObjectFactory.GetMap(broker, viaType);
         SqlBuilder sb     = new SqlBuilder(broker, StatementType.Select, containedMap.Type);
         // assume the relation object is the child, i.e. refers to the contained type
         mappings = viaMap.GetForeignKeyMappings(containedMap, true);
         if (mappings.Count == 0)
         {
             mappings = viaMap.GetForeignKeyMappings(containedMap, false);
         }
         Check.Verify(mappings.Count > 0, Error.DeveloperError,
                      "The type {0} does not contain a foreign key reference to type {1}.",
                      viaMap.Type, containedMap.Type);
         Check.Verify(mappings.Count == 1, Error.NotImplemented,
                      "GentleList for n:m relations can not be used with composite keys.");
         // verify that references point to unique instance
         //Check.Verify( mappings.Count == parentMap.PrimaryKeyCount, Error.DeveloperError,
         //	"The number of fields ({0}) referencing {1} from {2} must equal the primary key count ({3}).",
         //	mappings.Count, parentMap.Type, containedMap.Type, parentMap.PrimaryKeyCount );
         if (viaInstances.Count > 0)
         {
             foreach (FieldMap remote in mappings.Keys)
             {
                 FieldMap local = (FieldMap)mappings[remote];
                 // viaMap.GetForeignKeyFieldMap( containedMap.Type, local.PropertyName );
                 sb.AddConstraint(Operator.In, local.MemberName, viaInstances, remote.MemberName);
             }
             ObjectFactory.GetCollection(containedMap.Type, sb.GetStatement(true).Execute(), this);
         }
     }
 }