Generated Classes


Generated Classes

Summary== SubSonic generates classes based on your table structure - one class per table. These classes will vary depending on which version of SubSonic you use. Avoid using the following names for table columns in your database - "SchemaName" , "ClassName", which will result in compilation errors during class generation. ==Active Record

If you're using ; } public static TableSchema.Table Schema { get { if (BaseSchema

null) SetSQLProps(); return BaseSchema; } } private static void GetTableSchema() { if(!IsSchemaInitialized) { //Schema declaration TableSchema.Table schema = new TableSchema.Table("Region", TableType.Table, DataService.GetInstance("Northwind")); schema.Columns = new TableSchema.TableColumnCollection(); schema.SchemaName = @"dbo"; //columns TableSchema.TableColumn colvarRegionID = new TableSchema.TableColumn(schema); colvarRegionID.ColumnName = "RegionID"; colvarRegionID.DataType = DbType.Int32; colvarRegionID.MaxLength = 0; colvarRegionID.AutoIncrement = true; colvarRegionID.IsNullable = false; colvarRegionID.IsPrimaryKey = true; colvarRegionID.IsForeignKey = false; colvarRegionID.IsReadOnly = false; colvarRegionID.DefaultSetting = @""; colvarRegionID.ForeignKeyTableName = ""; schema.Columns.Add(colvarRegionID); TableSchema.TableColumn colvarRegionDescription = new TableSchema.TableColumn(schema); colvarRegionDescription.ColumnName = "RegionDescription"; colvarRegionDescription.DataType = DbType.String; colvarRegionDescription.MaxLength = 50; colvarRegionDescription.AutoIncrement = false; colvarRegionDescription.IsNullable = false; colvarRegionDescription.IsPrimaryKey = false; colvarRegionDescription.IsForeignKey = false; colvarRegionDescription.IsReadOnly = false; colvarRegionDescription.DefaultSetting = @""; colvarRegionDescription.ForeignKeyTableName = ""; schema.Columns.Add(colvarRegionDescription); BaseSchema = schema; //add this schema to the provider //so we can query it later DataService.Providers["Northwind"].AddSchema("Region",schema); } } #endregion #region Props [XmlAttribute("RegionID")] [Bindable(true)] public int RegionID { get { return GetColumnValue(Columns.RegionID); } set { SetColumnValue(Columns.RegionID, value); } } [XmlAttribute("RegionDescription")] [Bindable(true)] public string RegionDescription { get { return GetColumnValue(Columns.RegionDescription); } set { SetColumnValue(Columns.RegionDescription, value); } } #endregion #region PrimaryKey Methods protected override void SetPrimaryKey(object oValue) { base.SetPrimaryKey(oValue); SetPKValues(); } public Northwind.TerritoryCollection Territories() { return new Northwind.TerritoryCollection() .Where(Territory.Columns.RegionID, RegionID).Load(); } #endregion //no foreign key tables defined (0) //no ManyToMany tables defined (0) #region ObjectDataSource support /// /// Inserts a record, can be used with the Object Data Source /// public static void Insert(string varRegionDescription) { Region item = new Region(); item.RegionDescription = varRegionDescription; if (System.Web.HttpContext.Current != null) item.Save(System.Web.HttpContext.Current.User.Identity.Name); else item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name); } /// /// Updates a record, can be used with the Object Data Source /// public static void Update(int varRegionID,string varRegionDescription) { Region item = new Region(); item.RegionID = varRegionID; item.RegionDescription = varRegionDescription; item.IsNew = false; if (System.Web.HttpContext.Current != null) item.Save(System.Web.HttpContext.Current.User.Identity.Name); else item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name); } #endregion #region Typed Columns public static TableSchema.TableColumn RegionIDColumn { get { return Schema.Columns[0]; } } public static TableSchema.TableColumn RegionDescriptionColumn { get { return Schema.Columns[1]; } } #endregion #region Columns Struct public struct Columns { public static string RegionID = @"RegionID"; public static string RegionDescription = @"RegionDescription"; } #endregion #region Update PK Collections public void SetPKValues() { } #endregion #region Deep Save public void DeepSave() { Save(); } #endregion } } ==Active Record Foreign Keys== All foreign key tables are attached to these classes as child collections which you can query and access as needed. ==Customizing Active Record

You can change the code generated here by tweaking the code templates used to produce the classes. This code is found in your SubSonic install folder, inside the "Templates" directory. By default you can just place this file into your project in the "CodeTemplates" directory and SubSonic will find it while generating your code. You can override this setting by telling SubSonic (2.x only) where the templates are:

POCOs With SubSonic 3

As of 3.0, our preference is to get away from inheritance constraints which decrease SubSonic's flexibility and to go with "Plain Old CLR Objects" - or "POCOs" as they are called. This is to facilitate testability, which is core to ASP.NET MVC. The Northwind Regions table, therefore, would generate this code (a sample template - yours may be different): ///

/// A class which represents the Region table in the Northwind Database. /// This class is queryable through Northwind.DB.Region /// public partial class Region: INotifyPropertyChanging, INotifyPropertyChanged { public Region(){ OnCreated(); } public string KeyName (){ return "RegionID"; } public int KeyValue (){ return RegionID; } int _RegionID; public int RegionID { get{ return _RegionID; } set{ this.OnRegionIDChanging(value); this.SendPropertyChanging(); this.RegionID = value; this.SendPropertyChanged("RegionID"); this.OnRegionIDChanged(); } } public string DescriptorValue(){ return this.RegionDescription; } public string DescriptorColumn() { return "RegionDescription"; } public override string ToString(){ return this.RegionDescription; } public static string GetDescriptorColumn() { return "RegionDescription"; } string RegionDescription; public string RegionDescription { get{ return _RegionDescription; } set{ this.OnRegionDescriptionChanging(value); this.SendPropertyChanging(); this.RegionDescription = value; this.SendPropertyChanged("RegionDescription"); this.OnRegionDescriptionChanged(); } } #region Extensibility Hooks partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnRegionIDChanging(int value); partial void OnRegionIDChanged(); partial void OnRegionDescriptionChanging(string value); partial void OnRegionDescriptionChanged(); #endregion #region Foriegn Keys public IQueryable Territories { get{ SubSonic.Web.Models.DB _db =DB.CreateDB(); return from items in _db.Territories where items.RegionID == _RegionID select items; } } #endregion private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); public event PropertyChangingEventHandler PropertyChanging; public event PropertyChangedEventHandler PropertyChanged; protected virtual void SendPropertyChanging() { if ((this.PropertyChanging != null)) { this.PropertyChanging(this, emptyChangingEventArgs); } } protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } As you can see, this class is a little less verbose than the class.

Customizing 3.0 POCOs

All classes are created using our file, which is a Visual Studio code-generation file. You can change this code to reflect whatever it is you need to do. And if you do something brilliant you can !