ELENESSKI GAMES
  • Home
  • Games
    • Worzzler
    • The Lost Tribes
  • Unity
    • Facade Generator for Archimatix
    • Elenesski Object Database
    • Generic Move Camera
    • Unity C# Library
  • SQLINQ
    • Change Log
    • Concepts >
      • Overview
      • The Patterns
      • Database & Factories
      • Working with Tables
      • Custom Queries
      • Lazy Loading
      • Units of Work
    • Video Tutorials
    • Getting Started
    • Code Examples >
      • Implementing Behavior
      • Finding Objects
      • Custom Searches
      • Converters and Transformers
      • Object Association Management
      • New Objects
      • Creating Database
      • Associating Objects
      • Deleting Objects
      • Validating Objects
      • Saving
    • Sample Model >
      • Tutorial Model
      • Table Classes Code
      • Domain Classes Code
      • Factories Code
    • OSX Developers
    • Bridge Building
    • External Websites
    • Class Documentation
  • Presentations
    • 3D Ship Design
  • Music
    • Jamendo (Main)
    • Blend
    • Sound Cloud
    • Mixcloud (DJ)
    • DJ Demos
  • YouTube

Selecting rows from a table (Using LINQ)

To get instances of data from your table, you implement this in a two stage process.  In your IBaseTable class, you implement one or more methods like the examples shown below.  This pulls the equivalent of rows of data from tables.  Then you use a factory to transform these into instances you use in your unity environment.  At a minimum you must implement a GetByOID method.

        public static ValueTab GetByOID(int aOID) {

            return Repository.GetTemporaryDatabaseConnection().Table<ValueTab>().Where( IT => IT.OID == aOID ).FirstOrDefault();
        }

        public static IEnumerable<ValueTab> GetAllBySubType(string aSubType) {
            return Repository.GetTemporaryDatabaseConnection().Table<ValueTab>().Where( VT => VT.SubType == aSubType );
        }

        public static IEnumerable<ValueTab>  GetAll() {
            return Repository.GetTemporaryDatabaseConnection().Table<ValueTab>();
        }

        public static ValueTab GetByInformation(string aSubType , string aInformation) {
            return Repository.GetTemporaryDatabaseConnection()
                                           .Table<ValueTab>()
                                           .Where( VT => VT.SubType == aSubType && VT.Information == aInformation ).FirstOrDefault();
        }


Then you implement a series of methods in your domain class that references these methods, for your various features.  Sometimes you want to implement the filtering logic at the table or in the domain.  Doesn't matter to the library what the correct approach is, except that filtering at the domain level will guarantee that rows are loaded into memory; which could result in faster subsequent calls because there is no database I/O to contend with.

The CountByType() method below also demonstrates that you don't need to use the factory to get your data.  Since I'm only interested in a count.

        public static Value GetByOid(int aOID) {
            return Repository.GetByOID<ValueTab,Value>( aOID );
        }

        public static IEnumerable<Value> GetAll() {
            return Repository.Factory<ValueTab,Value>(ValueTab.GetAll());
        }

        public static IEnumerable<Value> GetNonTypes() {
            return Repository.Factory<ValueTab,Value>(ValueTab.GetAll().Where( A => A.SubType != TypeValue.SubType ));
        }

        public static IEnumerable<Value> GetByName(string aName) {
            return Repository.Factory<ValueTab,Value>( ValueTab.GetAll().Where( A => A.SubType != TypeValue.SubType ) )
                                          .Where( A=>A.ToString() == aName );
        }

        public static IEnumerable<Value> GetBySubType(string aSubType) {
            return Repository.Factory<ValueTab,Value>( ValueTab.GetAll().Where( A => A.SubType == aSubType ) );
        }

        public static int CountByType(string aSubType) {
            return ValueTab.GetAll().Count( A => A.SubType == aSubType );
        }

INSerting or updating Data to the database

Saving to a table is done in each BaseClass, as it's the only place that knows how to transform attributes in the class to columns in a table.  You don't need to differentiate an insert from an update with Save().  If it's an insert, the instance is created in the database and the OID for the table is automatically updated in your instance.

public class Reference : BaseClass {

        public override bool Save() {
            ReferenceTab TABLE = new ReferenceTab();
            TABLE.TheValueOID = TheValue.OID;
            TABLE.SubType = "REF";
            TABLE.FieldOrder = FieldOrder;
            return Repository.Save( TABLE , this );
        }
}

Deleting DATA from a table

To delete an instance, you simply call "Delete()" on the instance itself.  In some cases, you might have aggregate or composite objects stored in other tables.  If this is the case, you might need to override the Delete() method to also delete all the instances in those tables first, before deleting this instance.  For example:

        public override bool Delete() {

            // Delete associated / composite data here

            return base.Delete();
        }

Deleting a instance doesn't actually delete it from the database until you Save() it.  Don't use Save if you are using Units of Work (see below).