#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 DomainObjects.Facade.Command; using DomainObjects.Facade.Transaction; namespace DomainObjects.Tutorial { /// /// This class provides a transaction facade to the tutorial functionality. /// Because the 'Transaction' attribute has been applied to this class, /// all methods are executed in the context of a transaction. /// /// This class defines the units of work supported by your application; it can /// be thought of as the 'service layer' of your application. /// /// Any persistable objects created or modified during the execution of these /// methods will be automatically inserted or updated to the database by DomainObjects. /// [Transaction(TransactionOption.Required)] public sealed class TransactionFacade : Service { /// /// Queries the database for an instance of Planet with the name of 'Earth'. /// /// /// The instance of planet if found, else null. /// public Planet HelloWorld() { // Create a new instance of Criteria which will hold the query criteria Criteria criteria = new Criteria(); // The line below tell DomainObjects: "Query for an instance of type Planet // where the field '_name' has a value of 'Earth'" criteria.AddEqualTo(Types.Planet.Field._name, "Earth"); // the name of the planet is unique so at most one planet will be selected // from the database. DomainObjects returns a 'managed instance' of Planet, if found. // A 'managed instance' of an object is one where DomainObjects can track modifications // to the object. return QueryFacade.FindObject(criteria); } /// /// Creates a new instance of sun with the given sunName and an associated /// new planet with the given planetName and rotationalPeriod. /// Both new objects are persisted it to the database in the context of a /// single transaction. /// /// As an experiment, try entering a planetName that is over 20 characters; /// this will cause a database constraint to be thrown upon the insert. /// You'll find that, because this method is transactional, neither the star /// nor the planet is persisted to the database. /// public Planet CreateNewStarAndPlanet(string starName, string planetName, decimal rotationalPeriod) { // A call to the method 'Manage()' must be called immediately after // constructing a persistable object so that DomainObjects can track // modifications to the instance; your code must not hold a direct reference // to an unmanaged persistable object. Star newStar = new Star(starName).Manage(); return new Planet(newStar, planetName, rotationalPeriod).Manage(); } } }