From 3f91bc38ac8d7f51450fb0d8ef98b355ef9d9e18 Mon Sep 17 00:00:00 2001 From: robconery Date: Thu, 22 Jul 2010 20:22:36 -1000 Subject: [PATCH] Fixed borked solution file --- SubSonic.Linq.sln | 11 - .../BugReports/Generated/ActiveRecord.cs | 7802 +++++++++++++++++ 2 files changed, 7802 insertions(+), 11 deletions(-) diff --git a/SubSonic.Linq.sln b/SubSonic.Linq.sln index 840ce70..dae5306 100644 --- a/SubSonic.Linq.sln +++ b/SubSonic.Linq.sln @@ -5,11 +5,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubSonic.Core", "SubSonic.C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubSonic.Tests", "SubSonic.Tests\SubSonic.Tests.csproj", "{6B23B65F-2E09-4531-BDA1-A6EA08CBA843}" EndProject -<<<<<<< HEAD -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerfRunner", "PerfRunner\PerfRunner.csproj", "{45BA3F78-05F5-4344-AD0D-309F2AED9A0A}" -======= Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubSonic.Tests.Unit", "SubSonic.Tests.Unit\SubSonic.Tests.Unit.csproj", "{29F51F2F-115C-4AFE-85CB-1F34498C6AD3}" ->>>>>>> 081569933c8ed7d3dbd442d1798f26b9375b5abc EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -25,17 +21,10 @@ Global {6B23B65F-2E09-4531-BDA1-A6EA08CBA843}.Debug|Any CPU.Build.0 = Debug|Any CPU {6B23B65F-2E09-4531-BDA1-A6EA08CBA843}.Release|Any CPU.ActiveCfg = Release|Any CPU {6B23B65F-2E09-4531-BDA1-A6EA08CBA843}.Release|Any CPU.Build.0 = Release|Any CPU -<<<<<<< HEAD - {45BA3F78-05F5-4344-AD0D-309F2AED9A0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {45BA3F78-05F5-4344-AD0D-309F2AED9A0A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {45BA3F78-05F5-4344-AD0D-309F2AED9A0A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {45BA3F78-05F5-4344-AD0D-309F2AED9A0A}.Release|Any CPU.Build.0 = Release|Any CPU -======= {29F51F2F-115C-4AFE-85CB-1F34498C6AD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {29F51F2F-115C-4AFE-85CB-1F34498C6AD3}.Debug|Any CPU.Build.0 = Debug|Any CPU {29F51F2F-115C-4AFE-85CB-1F34498C6AD3}.Release|Any CPU.ActiveCfg = Release|Any CPU {29F51F2F-115C-4AFE-85CB-1F34498C6AD3}.Release|Any CPU.Build.0 = Release|Any CPU ->>>>>>> 081569933c8ed7d3dbd442d1798f26b9375b5abc EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SubSonic.Tests/BugReports/Generated/ActiveRecord.cs b/SubSonic.Tests/BugReports/Generated/ActiveRecord.cs index 840276d..a28aa48 100644 --- a/SubSonic.Tests/BugReports/Generated/ActiveRecord.cs +++ b/SubSonic.Tests/BugReports/Generated/ActiveRecord.cs @@ -18,3 +18,7805 @@ namespace SouthWind { + + + /// + /// A class which represents the Customers table in the Northwind Database. + /// + public partial class Customer: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Customer item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Customer(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Customer.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Customer(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Customer(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Customer.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Customer SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Customer single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Customer SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Customer single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "CustomerID"; + } + + public object KeyValue() + { + return this.CustomerID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CustomerID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Customer)){ + Customer compare=(Customer)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + public string DescriptorValue() + { + return this.CustomerID.ToString(); + } + + public string DescriptorColumn() { + return "CustomerID"; + } + public static string GetKeyColumn() + { + return "CustomerID"; + } + public static string GetDescriptorColumn() + { + return "CustomerID"; + } + + #region ' Foreign Keys ' + public IQueryable CustomerCustomerDemos + { + get + { + + var repo=SouthWind.CustomerCustomerDemo.GetRepo(); + return from items in repo.GetAll() + where items.CustomerID == _CustomerID + select items; + } + } + + public IQueryable Orders + { + get + { + + var repo=SouthWind.Order.GetRepo(); + return from items in repo.GetAll() + where items.CustomerID == _CustomerID + select items; + } + } + + #endregion + + + string _CustomerID; + public string CustomerID + { + get { return _CustomerID; } + set + { + if(_CustomerID!=value){ + _CustomerID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CustomerID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CompanyName; + public string CompanyName + { + get { return _CompanyName; } + set + { + if(_CompanyName!=value){ + _CompanyName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CompanyName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ContactName; + public string ContactName + { + get { return _ContactName; } + set + { + if(_ContactName!=value){ + _ContactName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ContactName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ContactTitle; + public string ContactTitle + { + get { return _ContactTitle; } + set + { + if(_ContactTitle!=value){ + _ContactTitle=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ContactTitle"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Address; + public string Address + { + get { return _Address; } + set + { + if(_Address!=value){ + _Address=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Address"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _City; + public string City + { + get { return _City; } + set + { + if(_City!=value){ + _City=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="City"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Region; + public string Region + { + get { return _Region; } + set + { + if(_Region!=value){ + _Region=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Region"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _PostalCode; + public string PostalCode + { + get { return _PostalCode; } + set + { + if(_PostalCode!=value){ + _PostalCode=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PostalCode"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Country; + public string Country + { + get { return _Country; } + set + { + if(_Country!=value){ + _Country=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Country"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Phone; + public string Phone + { + get { return _Phone; } + set + { + if(_Phone!=value){ + _Phone=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Phone"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Fax; + public string Fax + { + get { return _Fax; } + set + { + if(_Fax!=value){ + _Fax=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Fax"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Shippers table in the Northwind Database. + /// + public partial class Shipper: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Shipper item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Shipper(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Shipper.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Shipper(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Shipper(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Shipper.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Shipper SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Shipper single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Shipper SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Shipper single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "ShipperID"; + } + + public object KeyValue() + { + return this.ShipperID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CompanyName.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Shipper)){ + Shipper compare=(Shipper)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.ShipperID; + } + + public string DescriptorValue() + { + return this.CompanyName.ToString(); + } + + public string DescriptorColumn() { + return "CompanyName"; + } + public static string GetKeyColumn() + { + return "ShipperID"; + } + public static string GetDescriptorColumn() + { + return "CompanyName"; + } + + #region ' Foreign Keys ' + public IQueryable Orders + { + get + { + + var repo=SouthWind.Order.GetRepo(); + return from items in repo.GetAll() + where items.ShipVia == _ShipperID + select items; + } + } + + #endregion + + + int _ShipperID; + public int ShipperID + { + get { return _ShipperID; } + set + { + if(_ShipperID!=value){ + _ShipperID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipperID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CompanyName; + public string CompanyName + { + get { return _CompanyName; } + set + { + if(_CompanyName!=value){ + _CompanyName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CompanyName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Phone; + public string Phone + { + get { return _Phone; } + set + { + if(_Phone!=value){ + _Phone=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Phone"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Suppliers table in the Northwind Database. + /// + public partial class Supplier: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Supplier item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Supplier(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Supplier.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Supplier(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Supplier(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Supplier.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Supplier SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Supplier single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Supplier SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Supplier single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "SupplierID"; + } + + public object KeyValue() + { + return this.SupplierID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CompanyName.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Supplier)){ + Supplier compare=(Supplier)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.SupplierID; + } + + public string DescriptorValue() + { + return this.CompanyName.ToString(); + } + + public string DescriptorColumn() { + return "CompanyName"; + } + public static string GetKeyColumn() + { + return "SupplierID"; + } + public static string GetDescriptorColumn() + { + return "CompanyName"; + } + + #region ' Foreign Keys ' + public IQueryable Products + { + get + { + + var repo=SouthWind.Product.GetRepo(); + return from items in repo.GetAll() + where items.SupplierID == _SupplierID + select items; + } + } + + #endregion + + + int _SupplierID; + public int SupplierID + { + get { return _SupplierID; } + set + { + if(_SupplierID!=value){ + _SupplierID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SupplierID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CompanyName; + public string CompanyName + { + get { return _CompanyName; } + set + { + if(_CompanyName!=value){ + _CompanyName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CompanyName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ContactName; + public string ContactName + { + get { return _ContactName; } + set + { + if(_ContactName!=value){ + _ContactName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ContactName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ContactTitle; + public string ContactTitle + { + get { return _ContactTitle; } + set + { + if(_ContactTitle!=value){ + _ContactTitle=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ContactTitle"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Address; + public string Address + { + get { return _Address; } + set + { + if(_Address!=value){ + _Address=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Address"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _City; + public string City + { + get { return _City; } + set + { + if(_City!=value){ + _City=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="City"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Region; + public string Region + { + get { return _Region; } + set + { + if(_Region!=value){ + _Region=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Region"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _PostalCode; + public string PostalCode + { + get { return _PostalCode; } + set + { + if(_PostalCode!=value){ + _PostalCode=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PostalCode"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Country; + public string Country + { + get { return _Country; } + set + { + if(_Country!=value){ + _Country=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Country"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Phone; + public string Phone + { + get { return _Phone; } + set + { + if(_Phone!=value){ + _Phone=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Phone"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Fax; + public string Fax + { + get { return _Fax; } + set + { + if(_Fax!=value){ + _Fax=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Fax"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _HomePage; + public string HomePage + { + get { return _HomePage; } + set + { + if(_HomePage!=value){ + _HomePage=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="HomePage"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Order Details table in the Northwind Database. + /// + public partial class OrderDetail: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(OrderDetail item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public OrderDetail(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + OrderDetail.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public OrderDetail(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public OrderDetail(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + OrderDetail.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static OrderDetail SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + OrderDetail single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static OrderDetail SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + OrderDetail single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "OrderID"; + } + + public object KeyValue() + { + return this.OrderID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.ProductID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(OrderDetail)){ + OrderDetail compare=(OrderDetail)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.OrderID; + } + + public string DescriptorValue() + { + return this.ProductID.ToString(); + } + + public string DescriptorColumn() { + return "ProductID"; + } + public static string GetKeyColumn() + { + return "OrderID"; + } + public static string GetDescriptorColumn() + { + return "ProductID"; + } + + #region ' Foreign Keys ' + public IQueryable Orders + { + get + { + + var repo=SouthWind.Order.GetRepo(); + return from items in repo.GetAll() + where items.OrderID == _OrderID + select items; + } + } + + public IQueryable Products + { + get + { + + var repo=SouthWind.Product.GetRepo(); + return from items in repo.GetAll() + where items.ProductID == _ProductID + select items; + } + } + + #endregion + + + int _OrderID; + public int OrderID + { + get { return _OrderID; } + set + { + if(_OrderID!=value){ + _OrderID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int _ProductID; + public int ProductID + { + get { return _ProductID; } + set + { + if(_ProductID!=value){ + _ProductID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ProductID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal _UnitPrice; + public decimal UnitPrice + { + get { return _UnitPrice; } + set + { + if(_UnitPrice!=value){ + _UnitPrice=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UnitPrice"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + short _Quantity; + public short Quantity + { + get { return _Quantity; } + set + { + if(_Quantity!=value){ + _Quantity=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Quantity"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal _Discount; + public decimal Discount + { + get { return _Discount; } + set + { + if(_Discount!=value){ + _Discount=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Discount"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the CustomerCustomerDemo table in the Northwind Database. + /// + public partial class CustomerCustomerDemo: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(CustomerCustomerDemo item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public CustomerCustomerDemo(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + CustomerCustomerDemo.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public CustomerCustomerDemo(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public CustomerCustomerDemo(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + CustomerCustomerDemo.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static CustomerCustomerDemo SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + CustomerCustomerDemo single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static CustomerCustomerDemo SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + CustomerCustomerDemo single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "CustomerID"; + } + + public object KeyValue() + { + return this.CustomerID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CustomerID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(CustomerCustomerDemo)){ + CustomerCustomerDemo compare=(CustomerCustomerDemo)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + public string DescriptorValue() + { + return this.CustomerID.ToString(); + } + + public string DescriptorColumn() { + return "CustomerID"; + } + public static string GetKeyColumn() + { + return "CustomerID"; + } + public static string GetDescriptorColumn() + { + return "CustomerID"; + } + + #region ' Foreign Keys ' + public IQueryable CustomerDemographics + { + get + { + + var repo=SouthWind.CustomerDemographic.GetRepo(); + return from items in repo.GetAll() + where items.CustomerTypeID == _CustomerTypeID + select items; + } + } + + public IQueryable Customers + { + get + { + + var repo=SouthWind.Customer.GetRepo(); + return from items in repo.GetAll() + where items.CustomerID == _CustomerID + select items; + } + } + + #endregion + + + string _CustomerID; + public string CustomerID + { + get { return _CustomerID; } + set + { + if(_CustomerID!=value){ + _CustomerID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CustomerID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CustomerTypeID; + public string CustomerTypeID + { + get { return _CustomerTypeID; } + set + { + if(_CustomerTypeID!=value){ + _CustomerTypeID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CustomerTypeID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the CustomerDemographics table in the Northwind Database. + /// + public partial class CustomerDemographic: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(CustomerDemographic item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public CustomerDemographic(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + CustomerDemographic.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public CustomerDemographic(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public CustomerDemographic(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + CustomerDemographic.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static CustomerDemographic SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + CustomerDemographic single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static CustomerDemographic SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + CustomerDemographic single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "CustomerTypeID"; + } + + public object KeyValue() + { + return this.CustomerTypeID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CustomerTypeID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(CustomerDemographic)){ + CustomerDemographic compare=(CustomerDemographic)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + public string DescriptorValue() + { + return this.CustomerTypeID.ToString(); + } + + public string DescriptorColumn() { + return "CustomerTypeID"; + } + public static string GetKeyColumn() + { + return "CustomerTypeID"; + } + public static string GetDescriptorColumn() + { + return "CustomerTypeID"; + } + + #region ' Foreign Keys ' + public IQueryable CustomerCustomerDemos + { + get + { + + var repo=SouthWind.CustomerCustomerDemo.GetRepo(); + return from items in repo.GetAll() + where items.CustomerTypeID == _CustomerTypeID + select items; + } + } + + #endregion + + + string _CustomerTypeID; + public string CustomerTypeID + { + get { return _CustomerTypeID; } + set + { + if(_CustomerTypeID!=value){ + _CustomerTypeID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CustomerTypeID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CustomerDesc; + public string CustomerDesc + { + get { return _CustomerDesc; } + set + { + if(_CustomerDesc!=value){ + _CustomerDesc=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CustomerDesc"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Region table in the Northwind Database. + /// + public partial class Region: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Region item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Region(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Region.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Region(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Region(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Region.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Region SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Region single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Region SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Region single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "RegionID"; + } + + public object KeyValue() + { + return this.RegionID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.RegionDescription.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Region)){ + Region compare=(Region)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.RegionID; + } + + public string DescriptorValue() + { + return this.RegionDescription.ToString(); + } + + public string DescriptorColumn() { + return "RegionDescription"; + } + public static string GetKeyColumn() + { + return "RegionID"; + } + public static string GetDescriptorColumn() + { + return "RegionDescription"; + } + + #region ' Foreign Keys ' + public IQueryable Territories + { + get + { + + var repo=SouthWind.Territory.GetRepo(); + return from items in repo.GetAll() + where items.RegionID == _RegionID + select items; + } + } + + #endregion + + + int _RegionID; + public int RegionID + { + get { return _RegionID; } + set + { + if(_RegionID!=value){ + _RegionID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RegionID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _RegionDescription; + public string RegionDescription + { + get { return _RegionDescription; } + set + { + if(_RegionDescription!=value){ + _RegionDescription=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RegionDescription"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Territories table in the Northwind Database. + /// + public partial class Territory: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Territory item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Territory(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Territory.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Territory(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Territory(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Territory.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Territory SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Territory single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Territory SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Territory single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "TerritoryID"; + } + + public object KeyValue() + { + return this.TerritoryID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.TerritoryID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Territory)){ + Territory compare=(Territory)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + public string DescriptorValue() + { + return this.TerritoryID.ToString(); + } + + public string DescriptorColumn() { + return "TerritoryID"; + } + public static string GetKeyColumn() + { + return "TerritoryID"; + } + public static string GetDescriptorColumn() + { + return "TerritoryID"; + } + + #region ' Foreign Keys ' + public IQueryable EmployeeTerritories + { + get + { + + var repo=SouthWind.EmployeeTerritory.GetRepo(); + return from items in repo.GetAll() + where items.TerritoryID == _TerritoryID + select items; + } + } + + public IQueryable Regions + { + get + { + + var repo=SouthWind.Region.GetRepo(); + return from items in repo.GetAll() + where items.RegionID == _RegionID + select items; + } + } + + #endregion + + + string _TerritoryID; + public string TerritoryID + { + get { return _TerritoryID; } + set + { + if(_TerritoryID!=value){ + _TerritoryID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TerritoryID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _TerritoryDescription; + public string TerritoryDescription + { + get { return _TerritoryDescription; } + set + { + if(_TerritoryDescription!=value){ + _TerritoryDescription=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TerritoryDescription"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int _RegionID; + public int RegionID + { + get { return _RegionID; } + set + { + if(_RegionID!=value){ + _RegionID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RegionID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the EmployeeTerritories table in the Northwind Database. + /// + public partial class EmployeeTerritory: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(EmployeeTerritory item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public EmployeeTerritory(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + EmployeeTerritory.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public EmployeeTerritory(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public EmployeeTerritory(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + EmployeeTerritory.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static EmployeeTerritory SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + EmployeeTerritory single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static EmployeeTerritory SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + EmployeeTerritory single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "EmployeeID"; + } + + public object KeyValue() + { + return this.EmployeeID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.TerritoryID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(EmployeeTerritory)){ + EmployeeTerritory compare=(EmployeeTerritory)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.EmployeeID; + } + + public string DescriptorValue() + { + return this.TerritoryID.ToString(); + } + + public string DescriptorColumn() { + return "TerritoryID"; + } + public static string GetKeyColumn() + { + return "EmployeeID"; + } + public static string GetDescriptorColumn() + { + return "TerritoryID"; + } + + #region ' Foreign Keys ' + public IQueryable Employees + { + get + { + + var repo=SouthWind.Employee.GetRepo(); + return from items in repo.GetAll() + where items.EmployeeID == _EmployeeID + select items; + } + } + + public IQueryable Territories + { + get + { + + var repo=SouthWind.Territory.GetRepo(); + return from items in repo.GetAll() + where items.TerritoryID == _TerritoryID + select items; + } + } + + #endregion + + + int _EmployeeID; + public int EmployeeID + { + get { return _EmployeeID; } + set + { + if(_EmployeeID!=value){ + _EmployeeID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="EmployeeID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _TerritoryID; + public string TerritoryID + { + get { return _TerritoryID; } + set + { + if(_TerritoryID!=value){ + _TerritoryID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TerritoryID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Orders table in the Northwind Database. + /// + public partial class Order: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Order item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Order(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Order.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Order(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Order(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Order.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Order SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Order single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Order SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Order single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "OrderID"; + } + + public object KeyValue() + { + return this.OrderID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CustomerID.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Order)){ + Order compare=(Order)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.OrderID; + } + + public string DescriptorValue() + { + return this.CustomerID.ToString(); + } + + public string DescriptorColumn() { + return "CustomerID"; + } + public static string GetKeyColumn() + { + return "OrderID"; + } + public static string GetDescriptorColumn() + { + return "CustomerID"; + } + + #region ' Foreign Keys ' + public IQueryable OrderDetails + { + get + { + + var repo=SouthWind.OrderDetail.GetRepo(); + return from items in repo.GetAll() + where items.OrderID == _OrderID + select items; + } + } + + public IQueryable Customers + { + get + { + + var repo=SouthWind.Customer.GetRepo(); + return from items in repo.GetAll() + where items.CustomerID == _CustomerID + select items; + } + } + + public IQueryable Employees + { + get + { + + var repo=SouthWind.Employee.GetRepo(); + return from items in repo.GetAll() + where items.EmployeeID == _EmployeeID + select items; + } + } + + public IQueryable Shippers + { + get + { + + var repo=SouthWind.Shipper.GetRepo(); + return from items in repo.GetAll() + where items.ShipperID == _ShipVia + select items; + } + } + + #endregion + + + int _OrderID; + public int OrderID + { + get { return _OrderID; } + set + { + if(_OrderID!=value){ + _OrderID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CustomerID; + public string CustomerID + { + get { return _CustomerID; } + set + { + if(_CustomerID!=value){ + _CustomerID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CustomerID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int _EmployeeID; + public int EmployeeID + { + get { return _EmployeeID; } + set + { + if(_EmployeeID!=value){ + _EmployeeID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="EmployeeID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + DateTime _OrderDate; + public DateTime OrderDate + { + get { return _OrderDate; } + set + { + if(_OrderDate!=value){ + _OrderDate=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="OrderDate"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + DateTime? _RequiredDate; + public DateTime? RequiredDate + { + get { return _RequiredDate; } + set + { + if(_RequiredDate!=value){ + _RequiredDate=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="RequiredDate"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + DateTime? _ShippedDate; + public DateTime? ShippedDate + { + get { return _ShippedDate; } + set + { + if(_ShippedDate!=value){ + _ShippedDate=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShippedDate"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int? _ShipVia; + public int? ShipVia + { + get { return _ShipVia; } + set + { + if(_ShipVia!=value){ + _ShipVia=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipVia"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal? _Freight; + public decimal? Freight + { + get { return _Freight; } + set + { + if(_Freight!=value){ + _Freight=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Freight"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ShipName; + public string ShipName + { + get { return _ShipName; } + set + { + if(_ShipName!=value){ + _ShipName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ShipAddress; + public string ShipAddress + { + get { return _ShipAddress; } + set + { + if(_ShipAddress!=value){ + _ShipAddress=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipAddress"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ShipCity; + public string ShipCity + { + get { return _ShipCity; } + set + { + if(_ShipCity!=value){ + _ShipCity=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipCity"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ShipRegion; + public string ShipRegion + { + get { return _ShipRegion; } + set + { + if(_ShipRegion!=value){ + _ShipRegion=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipRegion"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ShipPostalCode; + public string ShipPostalCode + { + get { return _ShipPostalCode; } + set + { + if(_ShipPostalCode!=value){ + _ShipPostalCode=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipPostalCode"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ShipCountry; + public string ShipCountry + { + get { return _ShipCountry; } + set + { + if(_ShipCountry!=value){ + _ShipCountry=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ShipCountry"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the SubSonicTests table in the Northwind Database. + /// + public partial class SubSonicTest: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(SubSonicTest item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public SubSonicTest(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + SubSonicTest.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public SubSonicTest(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public SubSonicTest(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + SubSonicTest.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static SubSonicTest SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + SubSonicTest single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static SubSonicTest SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + SubSonicTest single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "SubSonicTestID"; + } + + public object KeyValue() + { + return this.SubSonicTestID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.Name.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(SubSonicTest)){ + SubSonicTest compare=(SubSonicTest)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.SubSonicTestID; + } + + public string DescriptorValue() + { + return this.Name.ToString(); + } + + public string DescriptorColumn() { + return "Name"; + } + public static string GetKeyColumn() + { + return "SubSonicTestID"; + } + public static string GetDescriptorColumn() + { + return "Name"; + } + + #region ' Foreign Keys ' + #endregion + + + int _SubSonicTestID; + public int SubSonicTestID + { + get { return _SubSonicTestID; } + set + { + if(_SubSonicTestID!=value){ + _SubSonicTestID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SubSonicTestID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int _Thinger; + public int Thinger + { + get { return _Thinger; } + set + { + if(_Thinger!=value){ + _Thinger=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Thinger"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Name; + public string Name + { + get { return _Name; } + set + { + if(_Name!=value){ + _Name=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Name"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _UserName; + public string UserName + { + get { return _UserName; } + set + { + if(_UserName!=value){ + _UserName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UserName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + DateTime _CreatedOn; + public DateTime CreatedOn + { + get { return _CreatedOn; } + set + { + if(_CreatedOn!=value){ + _CreatedOn=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CreatedOn"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal _Price; + public decimal Price + { + get { return _Price; } + set + { + if(_Price!=value){ + _Price=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Price"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + double _Discount; + public double Discount + { + get { return _Discount; } + set + { + if(_Discount!=value){ + _Discount=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Discount"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal? _Lat; + public decimal? Lat + { + get { return _Lat; } + set + { + if(_Lat!=value){ + _Lat=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Lat"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal? _Long; + public decimal? Long + { + get { return _Long; } + set + { + if(_Long!=value){ + _Long=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Long"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + bool _SomeFlag; + public bool SomeFlag + { + get { return _SomeFlag; } + set + { + if(_SomeFlag!=value){ + _SomeFlag=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SomeFlag"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + bool? _SomeNullableFlag; + public bool? SomeNullableFlag + { + get { return _SomeNullableFlag; } + set + { + if(_SomeNullableFlag!=value){ + _SomeNullableFlag=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SomeNullableFlag"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _LongText; + public string LongText + { + get { return _LongText; } + set + { + if(_LongText!=value){ + _LongText=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LongText"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _MediumText; + public string MediumText + { + get { return _MediumText; } + set + { + if(_MediumText!=value){ + _MediumText=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MediumText"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + this.CreatedOn=NorthwindDB.DateTimeNowTruncatedDownToSecond(); + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Products table in the Northwind Database. + /// + public partial class Product: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Product item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Product(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Product.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Product(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Product(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Product.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Product SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Product single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Product SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Product single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "ProductID"; + } + + public object KeyValue() + { + return this.ProductID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.ProductName.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Product)){ + Product compare=(Product)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.ProductID; + } + + public string DescriptorValue() + { + return this.ProductName.ToString(); + } + + public string DescriptorColumn() { + return "ProductName"; + } + public static string GetKeyColumn() + { + return "ProductID"; + } + public static string GetDescriptorColumn() + { + return "ProductName"; + } + + #region ' Foreign Keys ' + public IQueryable OrderDetails + { + get + { + + var repo=SouthWind.OrderDetail.GetRepo(); + return from items in repo.GetAll() + where items.ProductID == _ProductID + select items; + } + } + + public IQueryable Categories + { + get + { + + var repo=SouthWind.Category.GetRepo(); + return from items in repo.GetAll() + where items.CategoryID == _CategoryID + select items; + } + } + + public IQueryable Suppliers + { + get + { + + var repo=SouthWind.Supplier.GetRepo(); + return from items in repo.GetAll() + where items.SupplierID == _SupplierID + select items; + } + } + + #endregion + + + int _ProductID; + public int ProductID + { + get { return _ProductID; } + set + { + if(_ProductID!=value){ + _ProductID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ProductID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _ProductName; + public string ProductName + { + get { return _ProductName; } + set + { + if(_ProductName!=value){ + _ProductName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ProductName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int? _SupplierID; + public int? SupplierID + { + get { return _SupplierID; } + set + { + if(_SupplierID!=value){ + _SupplierID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="SupplierID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int? _CategoryID; + public int? CategoryID + { + get { return _CategoryID; } + set + { + if(_CategoryID!=value){ + _CategoryID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CategoryID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _QuantityPerUnit; + public string QuantityPerUnit + { + get { return _QuantityPerUnit; } + set + { + if(_QuantityPerUnit!=value){ + _QuantityPerUnit=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="QuantityPerUnit"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + decimal? _UnitPrice; + public decimal? UnitPrice + { + get { return _UnitPrice; } + set + { + if(_UnitPrice!=value){ + _UnitPrice=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UnitPrice"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + short? _UnitsInStock; + public short? UnitsInStock + { + get { return _UnitsInStock; } + set + { + if(_UnitsInStock!=value){ + _UnitsInStock=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UnitsInStock"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + long? _UnitsOnOrder; + public long? UnitsOnOrder + { + get { return _UnitsOnOrder; } + set + { + if(_UnitsOnOrder!=value){ + _UnitsOnOrder=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="UnitsOnOrder"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + short? _ReorderLevel; + public short? ReorderLevel + { + get { return _ReorderLevel; } + set + { + if(_ReorderLevel!=value){ + _ReorderLevel=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReorderLevel"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + bool _Discontinued; + public bool Discontinued + { + get { return _Discontinued; } + set + { + if(_Discontinued!=value){ + _Discontinued=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Discontinued"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Employees table in the Northwind Database. + /// + public partial class Employee: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Employee item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Employee(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Employee.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Employee(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Employee(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Employee.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Employee SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Employee single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Employee SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Employee single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "EmployeeID"; + } + + public object KeyValue() + { + return this.EmployeeID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.LastName.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Employee)){ + Employee compare=(Employee)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.EmployeeID; + } + + public string DescriptorValue() + { + return this.LastName.ToString(); + } + + public string DescriptorColumn() { + return "LastName"; + } + public static string GetKeyColumn() + { + return "EmployeeID"; + } + public static string GetDescriptorColumn() + { + return "LastName"; + } + + #region ' Foreign Keys ' + public IQueryable Employees + { + get + { + + var repo=SouthWind.Employee.GetRepo(); + return from items in repo.GetAll() + where items.EmployeeID == _ReportsTo + select items; + } + } + + public IQueryable EmployeeTerritories + { + get + { + + var repo=SouthWind.EmployeeTerritory.GetRepo(); + return from items in repo.GetAll() + where items.EmployeeID == _EmployeeID + select items; + } + } + + public IQueryable Orders + { + get + { + + var repo=SouthWind.Order.GetRepo(); + return from items in repo.GetAll() + where items.EmployeeID == _EmployeeID + select items; + } + } + + #endregion + + + int _EmployeeID; + public int EmployeeID + { + get { return _EmployeeID; } + set + { + if(_EmployeeID!=value){ + _EmployeeID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="EmployeeID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _LastName; + public string LastName + { + get { return _LastName; } + set + { + if(_LastName!=value){ + _LastName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="LastName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _FirstName; + public string FirstName + { + get { return _FirstName; } + set + { + if(_FirstName!=value){ + _FirstName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="FirstName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Title; + public string Title + { + get { return _Title; } + set + { + if(_Title!=value){ + _Title=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Title"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _TitleOfCourtesy; + public string TitleOfCourtesy + { + get { return _TitleOfCourtesy; } + set + { + if(_TitleOfCourtesy!=value){ + _TitleOfCourtesy=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="TitleOfCourtesy"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + DateTime? _BirthDate; + public DateTime? BirthDate + { + get { return _BirthDate; } + set + { + if(_BirthDate!=value){ + _BirthDate=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="BirthDate"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + DateTime? _HireDate; + public DateTime? HireDate + { + get { return _HireDate; } + set + { + if(_HireDate!=value){ + _HireDate=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="HireDate"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Address; + public string Address + { + get { return _Address; } + set + { + if(_Address!=value){ + _Address=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Address"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _City; + public string City + { + get { return _City; } + set + { + if(_City!=value){ + _City=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="City"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Region; + public string Region + { + get { return _Region; } + set + { + if(_Region!=value){ + _Region=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Region"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _PostalCode; + public string PostalCode + { + get { return _PostalCode; } + set + { + if(_PostalCode!=value){ + _PostalCode=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PostalCode"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Country; + public string Country + { + get { return _Country; } + set + { + if(_Country!=value){ + _Country=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Country"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _HomePhone; + public string HomePhone + { + get { return _HomePhone; } + set + { + if(_HomePhone!=value){ + _HomePhone=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="HomePhone"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Extension; + public string Extension + { + get { return _Extension; } + set + { + if(_Extension!=value){ + _Extension=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Extension"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + byte[] _Photo; + public byte[] Photo + { + get { return _Photo; } + set + { + if(_Photo!=value){ + _Photo=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Photo"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Notes; + public string Notes + { + get { return _Notes; } + set + { + if(_Notes!=value){ + _Notes=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Notes"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + int? _ReportsTo; + public int? ReportsTo + { + get { return _ReportsTo; } + set + { + if(_ReportsTo!=value){ + _ReportsTo=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ReportsTo"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _PhotoPath; + public string PhotoPath + { + get { return _PhotoPath; } + set + { + if(_PhotoPath!=value){ + _PhotoPath=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="PhotoPath"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } + + + /// + /// A class which represents the Categories table in the Northwind Database. + /// + public partial class Category: IActiveRecord + { + + #region Built-in testing + static TestRepository _testRepo; + + + + static void SetTestRepo(){ + _testRepo = _testRepo ?? new TestRepository(new SouthWind.NorthwindDB()); + } + public static void ResetTestRepo(){ + _testRepo = null; + SetTestRepo(); + } + public static void Setup(List testlist){ + SetTestRepo(); + foreach (var item in testlist) + { + _testRepo._items.Add(item); + } + } + public static void Setup(Category item) { + SetTestRepo(); + _testRepo._items.Add(item); + } + public static void Setup(int testItems) { + SetTestRepo(); + for(int i=0;i _repo; + ITable tbl; + bool _isNew; + public bool IsNew(){ + return _isNew; + } + + public void SetIsLoaded(bool isLoaded){ + _isLoaded=isLoaded; + if(isLoaded) + OnLoaded(); + } + + public void SetIsNew(bool isNew){ + _isNew=isNew; + } + bool _isLoaded; + public bool IsLoaded(){ + return _isLoaded; + } + + List _dirtyColumns; + public bool IsDirty(){ + return _dirtyColumns.Count>0; + } + + public List GetDirtyColumns (){ + return _dirtyColumns; + } + + SouthWind.NorthwindDB _db; + public Category(string connectionString, string providerName) { + + _db=new SouthWind.NorthwindDB(connectionString, providerName); + Init(); + } + void Init(){ + TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase); + _dirtyColumns=new List(); + if(TestMode){ + Category.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(_db); + } + tbl=_repo.GetTable(); + SetIsNew(true); + OnCreated(); + + } + + public Category(){ + _db=new SouthWind.NorthwindDB(); + Init(); + } + + + partial void OnCreated(); + + partial void OnLoaded(); + + partial void OnSaved(); + + partial void OnChanged(); + + public IList Columns{ + get{ + return tbl.Columns; + } + } + + public Category(Expression> expression):this() { + + SetIsLoaded(_repo.Load(this,expression)); + } + + + + internal static IRepository GetRepo(string connectionString, string providerName){ + SouthWind.NorthwindDB db; + if(String.IsNullOrEmpty(connectionString)){ + db=new SouthWind.NorthwindDB(); + }else{ + db=new SouthWind.NorthwindDB(connectionString, providerName); + } + IRepository _repo; + + if(db.TestMode){ + Category.SetTestRepo(); + _repo=_testRepo; + }else{ + _repo = new SubSonicRepository(db); + } + return _repo; + } + + internal static IRepository GetRepo(){ + return GetRepo("",""); + } + + public static Category SingleOrDefault(Expression> expression) { + + var repo = GetRepo(); + var results=repo.Find(expression); + Category single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + single.OnLoaded(); + single.SetIsLoaded(true); + single.SetIsNew(false); + } + + return single; + } + + public static Category SingleOrDefault(Expression> expression,string connectionString, string providerName) { + var repo = GetRepo(connectionString,providerName); + var results=repo.Find(expression); + Category single=null; + if(results.Count() > 0){ + single=results.ToList()[0]; + } + + return single; + + + } + + + public static bool Exists(Expression> expression,string connectionString, string providerName) { + + return All(connectionString,providerName).Any(expression); + } + public static bool Exists(Expression> expression) { + + return All().Any(expression); + } + + public static IList Find(Expression> expression) { + + var repo = GetRepo(); + return repo.Find(expression).ToList(); + } + + public static IList Find(Expression> expression,string connectionString, string providerName) { + + var repo = GetRepo(connectionString,providerName); + return repo.Find(expression).ToList(); + + } + public static IQueryable All(string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetAll(); + } + public static IQueryable All() { + return GetRepo().GetAll(); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(string sortBy, int pageIndex, int pageSize) { + return GetRepo().GetPaged(sortBy, pageIndex, pageSize); + } + + public static PagedList GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) { + return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize); + + } + + + public static PagedList GetPaged(int pageIndex, int pageSize) { + return GetRepo().GetPaged(pageIndex, pageSize); + + } + + public string KeyName() + { + return "CategoryID"; + } + + public object KeyValue() + { + return this.CategoryID; + } + + public void SetKeyValue(object value) { + if (value != null && value!=DBNull.Value) { + var settable = value.ChangeTypeTo(); + this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null); + } + } + + public override string ToString(){ + return this.CategoryName.ToString(); + } + + public override bool Equals(object obj){ + if(obj.GetType()==typeof(Category)){ + Category compare=(Category)obj; + return compare.KeyValue()==this.KeyValue(); + }else{ + return base.Equals(obj); + } + } + + + public override int GetHashCode() { + return this.CategoryID; + } + + public string DescriptorValue() + { + return this.CategoryName.ToString(); + } + + public string DescriptorColumn() { + return "CategoryName"; + } + public static string GetKeyColumn() + { + return "CategoryID"; + } + public static string GetDescriptorColumn() + { + return "CategoryName"; + } + + #region ' Foreign Keys ' + public IQueryable Products + { + get + { + + var repo=SouthWind.Product.GetRepo(); + return from items in repo.GetAll() + where items.CategoryID == _CategoryID + select items; + } + } + + #endregion + + + int _CategoryID; + public int CategoryID + { + get { return _CategoryID; } + set + { + if(_CategoryID!=value){ + _CategoryID=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CategoryID"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _CategoryName; + public string CategoryName + { + get { return _CategoryName; } + set + { + if(_CategoryName!=value){ + _CategoryName=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="CategoryName"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + string _Description; + public string Description + { + get { return _Description; } + set + { + if(_Description!=value){ + _Description=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Description"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + byte[] _Picture; + public byte[] Picture + { + get { return _Picture; } + set + { + if(_Picture!=value){ + _Picture=value; + var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Picture"); + if(col!=null){ + if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){ + _dirtyColumns.Add(col); + } + } + OnChanged(); + } + } + } + + + + public DbCommand GetUpdateCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand(); + + } + public DbCommand GetInsertCommand() { + + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + public DbCommand GetDeleteCommand() { + if(TestMode) + return _db.DataProvider.CreateCommand(); + else + return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand(); + } + + + public void Update(){ + Update(_db.DataProvider); + } + + public void Update(IDataProvider provider){ + + + if(this._dirtyColumns.Count>0) + _repo.Update(this,provider); + OnSaved(); + } + + public void Add(){ + Add(_db.DataProvider); + } + + + + public void Add(IDataProvider provider){ + + + var key=KeyValue(); + if(key==null){ + var newKey=_repo.Add(this,provider); + this.SetKeyValue(newKey); + }else{ + _repo.Add(this,provider); + } + SetIsNew(false); + OnSaved(); + } + + + + public void Save() { + Save(_db.DataProvider); + } + public void Save(IDataProvider provider) { + + + if (_isNew) { + Add(provider); + + } else { + Update(provider); + } + + } + + + + public void Delete(IDataProvider provider) { + + + _repo.Delete(KeyValue()); + + } + + + public void Delete() { + Delete(_db.DataProvider); + } + + + public static void Delete(Expression> expression) { + var repo = GetRepo(); + + + + repo.DeleteMany(expression); + + } + + + + public void Load(IDataReader rdr) { + Load(rdr, true); + } + public void Load(IDataReader rdr, bool closeReader) { + if (rdr.Read()) { + + try { + rdr.Load(this); + SetIsNew(false); + SetIsLoaded(true); + } catch { + SetIsLoaded(false); + throw; + } + }else{ + SetIsLoaded(false); + } + + if (closeReader) + rdr.Dispose(); + } + + + } +}