#region Copyright © 2003-2008 Richard Beauchamp /* * DomainObjects for .NET * Copyright © 2003-2008 Richard Beauchamp * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #endregion using System; using DomainObjects.Facade.Domain; namespace DomainObjects.Tutorial { /// /// This persistable object class maps to the PLANET database table. /// [Serializable] public class Planet : EditableObject { // MAPPED FIELDS - each of these fields map to a column in the PLANET database table. // maps to the foreign key column PLANET.STAR_ID private int _starId; // maps to the column PLANET.NAME // maps to the column PLANET.ROTATIONAL_PERIOD // RELATIONSHIP FIELDS // Holds the instance of Star referenced by the '_starId' foreign key field // // DomainObjects automatically populates this type of "reference" field via reflection: // it selects the row in the STAR table with the primary key of '_starId', // reconstructs the Star persistable object from the data reader, then sets the // value of this field to the reconstructed Star. private Reference _star; // Holds a strongly-typed collection of PlanetarySatellites based on the // PLANETARY_SATELLITE.PLANET_ID inverse foreign key. // // DomainObjects automatically populates this collection via reflection: it selects // the set of rows in the PLANETARY_SATELLITE table where PLANET_ID equals // this primary key, reconstructs the PlanetarySatellite persistable objects // from the result set, then sets the value of this field to the PlanetarySatellite // collection. private Collection _mySatellites; // CONSTRUCTORS /// /// The protected constructor that DomainObjects uses to reconstruct an existing instance /// from the database. This constructor must declare all of the fields that /// are mapped to the database. You'll find one parameter for each column in /// the PLANET table. /// /// /// The database primary key for this instance. The base class, DomainObjects.Facade.Domain.EditableObject, /// holds the primary key value, so the primary key must not be declared as /// a member field of this persistable object sub-class; rather, pass the primaryKey /// value to the base class constructor as shown below. /// /// The foreign key value of the related star. /// The name of the planet /// The rotational period of the planet. protected Planet(int primaryKey, int starId, string name, decimal rotationalPeriod) : base(primaryKey) { // For each mapped member field, set its value _starId = starId; Name = name; RotationalPeriod = rotationalPeriod; // Call this method as the last statement in your reconstructor // to ensure that your reference and collection fields are initialized // to non-null values. InitializeRelationships(); } /// /// The 'factory' constructor that your application will use to construct a /// new instance of Planet. If this constructor is called in the context of /// a transaction, then DomainObjects will automatically insert it into the database. /// /// Generally, as a best practice, this constructor should declare all fields /// that are required, i.e., not null, in the database. That way, (1) the client /// calling this constructor knows which fields are required, and (2) you can /// be more assured that your persistable object instance is constructed such /// that it meets the minimum database constraints. /// /// DomainObjects automatically generates and then sets the primary key via reflection; /// therefore, there's no need to pass a primary key parameter. /// /// /// The star around which this planet is orbiting /// /// /// The name of this planet /// /// /// The amount of time required for the planet to perform one complete rotation /// about its own axis relative to the earth /// public Planet(Star star, string name, decimal rotationalPeriod) { // Call this method as the first statement in your factory constructor // to ensure that your reference and collection fields are initialized // to non-null values. InitializeRelationships(); // For each parameter, set the value of the corresponding member field // Based on the instance of Star set here, DomainObjects automatically sets the // value of the '_starId' foreign key field _star.ReferencedObject = star; Name = name; RotationalPeriod = rotationalPeriod; } // PROPERTIES /// /// Gets the star around which this planet is orbiting. /// public Star Star { get { return _star.ReferencedObject; } } /// /// You must apply the MutatorAttribute to this property to tell DomainObjects that /// this method modifies a field that has been mapped to a database column. /// When the transaction commits, DomainObjects will automatically update this persistent /// object to the PLANET database table. /// [Mutator] public string Name { get; set; } /// /// You must apply the MutatorAttribute to this property to tell DomainObjects that /// this method modifies a field that has been mapped to a database column. /// When the transaction commits, DomainObjects will automatically update this persistent /// object to the PLANET database table. /// [Mutator] public decimal RotationalPeriod { get; set; } /// /// Gets the satellites associated with this planet. /// public Collection Satellites { get { return _mySatellites; } } /// /// Initializes reference fields to an instance of /// and collection fields to an instance of . /// /// Call this method as the first statement in your constructor to ensure that your reference and collection fields are initialized to non-null values. /// private void InitializeRelationships() { _mySatellites = new Collection(this, "_mySatellites"); _star = new Reference(this, "_star"); } } }