Here ya go.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mindscape.LightSpeed;
using Mindscape.LightSpeed.Validation;
using System.Text.RegularExpressions;
namespace LightSpeedCombinedSample
{
//The table attribute attaches a class to a table in the case where you are not using stored procedures
//The IdColumn is used to define the column name in the table that is used as the primary key id value
[Table("PENGUINS", IdColumnName = "ID")]
//The CrudProcedure attribute defines the stored procedure used to populate the entity
//Currently, you need to assign all of the procedure names. In the future they may be inferred by LightSpeed.
[CrudProcedure(
Select = "SelectPenguins",
SelectById = "SelectPenguin",
Insert = "CreatePenguin",
Update = "UpdatePenguin",
Delete = "DeletePenguin")]
public class Penguin : Entity<int> //All LightSpeed entities must inherit from Entity<int>
{
//Define all columns in the table or stored procedure, except for the ID column, which is
//a property of the base Entity<int> class
#region Private Members
[Column("SPECIES")] //Mapping of the private member to a column name in a table
private string _species;
[Column("NAME")] //Mapping of the private member to a column name in a table
[ValidatePresence] //Required field validation
[ValidateUnique] //Ensures the value is unique in the table
private string _name;
[Column("AGE")] //Mapping of the private member to a column name in a table
[ValidateFormat(@"^\d+$")] //Validates the format of a field based on a regular expression
private int _age;
[Column("PARENT")] //Mapping of the private member to a column name in a table
private int? _parentPenguinId;
//If a table contains a CreatedOn field, it will automatically be populated with the Create Date by LightSpeed
[Column("CREATEDON")] //Mapping of the private member to a column name in a table
private DateTime _createdOn;
//If a table contains a UpdatedOn field, it will automatically be populated with the Create Date by LightSpeed
[Column("UPDATEDON")] //Mapping of the private member to a column name in a table
private DateTime _updatedOn;
#endregion
//Relationships define the associations to other tables
#region Relationships
[ReverseAssociation("ChildPenguins")] //A reverse association helps LightSpeed know the property that the association is linked to
[CrudProcedure(Select = "SelectChildPenguins")] //When using stored procedures, the CrudProcedure attribute specifies the procuedure used to load the association
private readonly EntityCollection<Penguin> _childPenguins = new EntityCollection<Penguin>(); //This EntityCollection holds an association, in this case it is the same table since the table has a self referencing association
private readonly EntityHolder<Penguin> _parentPenguin = new EntityHolder<Penguin>(); //This EntityHolder is a back link to an associated table, in this case it is the same table since the table has a self referencing association
[ReverseAssociation("Seals")] //A reverse association helps LightSpeed know the property that the association is linked to
private readonly EntityCollection<Seal> _seals = new EntityCollection<Seal>();
private readonly EntityCollection<PenguinLion> _penguinLions = new EntityCollection<PenguinLion>(); //This Entity Collection holds the many-to-many table association
private ThroughAssociation<PenguinLion, Lion> _lions; //To bypass the many-to-many table, this ThroughAssociation will create a link directly
#endregion
#region Delegates
private EventHandler<EntityEventArgs<Seal>> handler; //This delegate is used to attach events to an entity, for example when an entity is modified
#endregion
#region Constructors
public Penguin()
{
//When tracking entity changes (Added, Removed) you can attach to an event and handle special logic
handler += new EventHandler<EntityEventArgs<Seal>>(_seals_EntityAdded);
_seals.EntityAdded += handler;
//When using a many-to-many relationship, you need to setup the ThroughAssociation when creating the instance so it can properly map across the many-to-many without using the middle class
_lions = new ThroughAssociation<PenguinLion, Lion>(_penguinLions);
}
#endregion
#region Destructors
~Penguin()
{
//When attaching to entity events, you need to clean up the event handlers in the destructor to make sure they go away gracefully
if (handler != null)
_seals.EntityAdded -= handler;
}
#endregion
#region Event Handlers
//Here you can perform special logic, like setting association Ids.
void _seals_EntityAdded(object sender, EntityEventArgs<Seal> e)
{
// e is the Entity being added and this is the Entity that is getting the new Entity
e.Entity.Name = String.Format("{0} - {1}", e.Entity.Name, this.Id);
}
#endregion
//Each private member should have an associated property that is accessed to get the data
//Always call the Get and Set methods in the property get/set so that LightSpeed can track
//state and notifications
#region Properties
public int? ParentPenguinId
{
get { return Get(ref _parentPenguinId); }
set { Set(ref _parentPenguinId, value, "ParentPenguinId"); }
}
public string Name
{
get { return Get(ref _name); }
set { Set(ref _name, value, "Name"); }
}
public string Species
{
get { return Get(ref _species); }
set { Set(ref _species, value, "Species"); }
}
public int Age
{
get { return Get(ref _age); }
set { Set(ref _age, value, "Age"); }
}
public DateTime CreatedOn
{
get { return Get(ref _createdOn); }
set { Set(ref _createdOn, value, "CreatedOn"); }
}
public DateTime UpdatedOn
{
get { return Get(ref _updatedOn); }
set { Set(ref _updatedOn, value, "UpdatedOn"); }
}
public EntityCollection<Penguin> ChildPenguins
{
get { return Get(_childPenguins); }
}
public Penguin ParentPenguin
{
get { return Get(_parentPenguin); }
set { Set(_parentPenguin, value); }
}
public EntityCollection<Seal> Seals
{
get { return Get(_seals); }
}
public EntityCollection<PenguinLion> PenguinLions //This EntityCollection is the middle table in a many-to-many association
{
get { return Get(_penguinLions); }
}
public ThroughAssociation<PenguinLion, Lion> Lions //This ThroughAssociation is what bypasses the middle table in a many-to-many association
{
get { return _lions; }
}
#endregion
#region Methods
//Using the Find method, it is possible to pass a predicate to simplfy the searching for records in a collection
public static Predicate<Penguin> ByPenguinName(string penguinName)
{
return delegate(Penguin penguins)
{
return (penguins.Name == penguinName);
};
}
#endregion
#region Custom Validation
//You can perform custom validations in the OnValidate event of the Entity if the Validation attributes do not cover what is needed
protected override void OnValidate()
{
if ((Name == "P0") && (Species == "Species0"))
{
Errors.AddError("This is an invalid combination of fields.");
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mindscape.LightSpeed;
using Mindscape.LightSpeed.Validation;
namespace LightSpeedCombinedSample
{
//The table attribute attaches a class to a table in the case where you are not using stored procedures
//The IdColumn is used to define the column name in the table that is used as the primary key id value
[Table("LIONS", IdColumnName = "ID")]
public partial class Lion : Entity<int> //All LightSpeed entities must inherit from Entity<int>
{
//Define all columns in the table or stored procedure, except for the ID column, which is
//a property of the base Entity<int> class
#region Private Members
[ValidatePresence] //Required field validation
[ValidateUnique] //Ensures the value is unique in the table
[Column("NAME")] //Mapping of the private member to a column name in a table
private string _name;
#endregion
//Relationships define the associations to other tables
#region Relationships
//EagerLoad when specified with dynamic SQL will automatically load the associations
//Otherwise, the associations are not loaded until referenced
[EagerLoad(AggregateName = "LoadAll")]
private readonly EntityCollection<PenguinLion> _penguinLions = new EntityCollection<PenguinLion>(); //This EntityCollection holds an association
//EagerLoad when specified with dynamic SQL will automatically load the associations
//Otherwise, the associations are not loaded until referenced
[EagerLoad]
private ThroughAssociation<PenguinLion, Penguin> _penguins; //To bypass the many-to-many table, this ThroughAssociation will create a link directly
#endregion
#region Constructors
public Lion()
{
//When using a many-to-many relationship, you need to setup the ThroughAssociation when creating the instance so it can properly map across the many-to-many without using the middle class
_penguins = new ThroughAssociation<PenguinLion, Penguin>(_penguinLions);
}
#endregion
//Each private member should have an associated property that is accessed to get the data
//Always call the Get and Set methods in the property get/set so that LightSpeed can track
//state and notifications
#region Properties
public string Name
{
get { return Get(ref _name); }
set { Set(ref _name, value, "Name"); }
}
public EntityCollection<PenguinLion> PenguinLions //This EntityCollection is the middle table in a many-to-many association
{
get { return Get(_penguinLions); }
}
public ThroughAssociation<PenguinLion, Penguin> Penguins //This ThroughAssociation is what bypasses the middle table in a many-to-many association
{
get { return _penguins; }
}
#endregion
}
}