Domain Library¶
Description¶
All your Business Objects (Classes and Enumerations), whether they are defined by you or come with a specific Template (e.g. Web Application Template of the MVP Implementation Strategy), have pre-defined Methods (Operations) that you can use automatically (without defining them as Operations in each class).
For example, anytime you define a new Persisted Class (e.g. a User or a Pet), it will instantly have some useful methods such as Save()
, Find()
and more.
These methods are documented in this page.
Methods¶
Copy(bool, bool)¶
Description
Returns a copy of the Domain.CLASS_NAME instance
Return Type : Domain.CLASS_NAME
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | Yes |
Non-Persisted | Yes | Yes |
Parameters
Name | Data Type | Description |
---|---|---|
deep | bool | If true, all associations and children classes will be copied as well. Otherwise, only the parent's classes Properties will be copied |
asNew | bool | In case of a Persisted Class, the asNew returns a brand new copy of the Domain.CLASS_NAME, with unique Primary Key values. Otherwise, all keys of the source class will be kept. |
Syntax
/*
Domain.CLASS_NAME newObject = oldObject.Copy(true, true)
*/
Domain.Product innuendo = Domain.Product.GetAll().First();
//Original Settings of the Copied Instance. It has an Id, a Name and a full list of Performers
DebugLib.Logger.WriteInfoLine("innuendo.Id: " + innuendo.Id); //5
DebugLib.Logger.WriteInfoLine("innuendo.Name: " + innuendo.Name); //Innuendo
DebugLib.Logger.WriteInfoLine("innuendo.Peformer.Length: " + innuendo.Performer.Length); //1
//Shallow copy and all keys are kept
Domain.Product newCD1 = innuendo.Copy(false, false);
DebugLib.Logger.WriteInfoLine("newCD1.Id: " + newCD1.Id); //5 (kept!)
DebugLib.Logger.WriteInfoLine("newCD1.Name: " + newCD1.Name); //Innuendo (copied)
DebugLib.Logger.WriteInfoLine("newCD1.Performer.Length: " + newCD1.Performer.Length); //0 (because deep = false)
//Shallow copy, new keys
Domain.Product newCD2 = innuendo.Copy(false, true);
DebugLib.Logger.WriteInfoLine("newCD2.Id: " + newCD2.Id); //0 (new)
DebugLib.Logger.WriteInfoLine("newCD2.Name: " + newCD2.Name); //Innuendo (copied)
DebugLib.Logger.WriteInfoLine("newCD2.Performer.Length: " + newCD2.Performer.Length); //0 (because deep = false)
//Deep copy and all keys are kept
Domain.Product newCD3 = innuendo.Copy(true, false);
DebugLib.Logger.WriteInfoLine("newCD3.Id: " + newCD3.Id); //5 (kept!)
DebugLib.Logger.WriteInfoLine("newCD3.Name: " + newCD3.Name); //Innuendo (copied)
DebugLib.Logger.WriteInfoLine("newCD3.Performer.Length: " + newCD3.Performer.Length); //1 (copied from parent)
//Deep copy and new keys
Domain.Product newCD4 = innuendo.Copy(true, true);
DebugLib.Logger.WriteInfoLine("newCD4.Id: " + newCD4.Id); //0 (new)
DebugLib.Logger.WriteInfoLine("newCD4.Name: " + newCD4.Name);//Innuendo (copied)
DebugLib.Logger.WriteInfoLine("newCD4.Performer.Length: " + newCD4.Performer.Length);//1 (copied from parent)
Copy(bool, bool, bool)¶
Description
Returns a copy of the Domain.CLASS_NAME instance
Return Type : Domain.CLASS_NAME
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | Yes |
Non-Persisted | Yes | Yes |
Parameters
Name | Data Type | Description |
---|---|---|
deep | bool | If true, all associations and children classes will be copied as well. Otherwise, only the parent's classes Properties will be copied |
asNew | bool | In case of a Persisted Class, the asNew returns a brand new copy of the Domain.CLASS_NAME, with unique Primary Key values. Otherwise, all keys of the source class will be kept. |
reUseNested | bool | If true, all associations and children classes that have already been copied, will be reused from the previous iterations. If false, all associations/children will be re-copied from scratch. (Hint: a true value will make the copy faster) |
Syntax
/*
Domain.CLASS_NAME newObject = oldObject.Copy(true, true, true)
*/
Domain.ApplicationUser user = Domain.ApplicationUser.GetAll().First();
var start = DateTime.Now();
for int i = 0; i < 10000; i + 1 {
Domain.ApplicationUser newUser = user.Copy(true, true, true);
}
var finish = DateTime.Now();
//[2:21:03 PM] to [2:21:03 PM] (milliseconds)
DebugLib.Logger.WriteInfoLine("["+start+"] to ["+finish+"] ");
//
user = Domain.ApplicationUser.GetAll().First();
var start = DateTime.Now();
for int i = 0; i < 10000; i + 1 {
Domain.ApplicationUser newUser = user.Copy(true, true, false);
}
var finish = DateTime.Now();
//[2:21:05 PM] to [2:21:11 PM] (several seconds!)
DebugLib.Logger.WriteInfoLine("["+start+"] to ["+finish+"] ");
Create()¶
Description
Creates a new Instance of the Domain.CLASS_NAME Class / Enumeration
Return Type : Domain.CLASS_NAME
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | Yes |
Non-Persisted | Yes | Yes |
Syntax
/*
Domain.CLASS_NAME x = Domain.CLASS_NAME.Create();
*/
//Creating a Class
Domain.User user = Domain.User.GetByKey("harrypotter");
if (user == null)
{
user = Domain.User.Create();
user.Name = "Harry Potter";
user.Email = "harry.potter@hogwarts.com";
user.UserName = "HarryPotter";
user.Save();
}
//Creating an Enumeration
Domain.Order order;
order.Status = Domain.Status.Create();
order.User = user;
order.Save();
Delete()¶
Description
Deletes the instance of the Domain.CLASS_NAME Class from the Database
Return Type : void
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
//Delete the first record
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetAll().First();
x.Delete();
*/
Domain.Product product = Model.Product;
//If the Product of the Form has been saved to the DB, delete it
if(!product.IsNew()){
product.Delete();
}
DeleteAll()¶
Description
Delete every instance of the Domain.CLASS_NAME Class from the Database
Return Type : void
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME.DeleteAll();
*/
function void ClearDatabase()
{
Domain.Product.DeleteAll();
Domain.Subcategory.DeleteAll();
Domain.Category.DeleteAll();
Domain.User.DeleteAll();
//.... delete all other tables (instances of classes)
}
Find(Func[Domain.CLASS_NAME, bool])¶
Description
Gets all those records of the Domain.CLASS_NAME class that comply with a predicate function
Return Type : Collection
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Parameters
Name | Data Type | Description |
---|---|---|
filter | Func | Predicate function to be used as the filter of the records |
Syntax
/*
Collection[Domain.CLASS_NAME] x = Domain.CLASS_NAME.Find(y => true);
*/
Collection[Domain.OrderedProduct] paidOrderedProducts = Domain.OrderedProduct.Find(x => x.Order.Status == Domain.Status.Paid);
foreach Domain.OrderedProduct paidOrderedProduct in paidOrderedProducts{
DebugLib.Logger.WriteInfoLine("Product ["+paidOrderedProduct.Product.Name+"] or Order ["+paidOrderedProduct.Order.Id+"] has been paid for!");
}
Find(Func[Domain.CLASS_NAME, bool], int, int, Dictionary[Func[Domain.CLASS_NAME, Object], bool])¶
Description
Gets a subset of the paged records of the Domain.CLASS_NAME class that comply with a predicate function. Returns a filtered, paged and sorted result of the queried data.
Return Type : CommonLib.PagedResults
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Parameters
Name | Data Type | Description |
---|---|---|
filter | Func | Predicate function to be used as the filter of the records |
startRowIndex | int | Zero-based index of the page to fetch (e.g. startRowIndex = 0 → bring the very first records. startRowIndex = 5 → bring the records on the 6th page of the paged results |
pageSize | int | Size of the page to fetch (e.g. 1 = bring one record per page, 10 = bring 10 records per page |
sortByColumnName | Dictionary | A Dictionary containing tuples of predicate functions that set the sorting column and bools that set the sorting direction (true = ascending) |
Syntax
/*
CommonLib.PagedResults[Domain.CLASS_NAME] results = Domain.User.Find(x => x.Age > 10, 0, 10, sortByDictionary)
*/
int pageSize = 20;
int startRow = 0;
Dictionary[Func[Domain.ApplicationOperation, Object], bool] sortByDic;
sortByDic.Add(x => x.ParentControllerName, true);
CommonLib.PagedResults[Domain.ApplicationOperation] ops = Domain.ApplicationOperation.Find(x => x.Name != "Render", startRow, pageSize, sortByDic);
DebugLib.Logger.WriteInfoLine("Total Operations: " + ops.TotalResults);
foreach Domain.ApplicationOperation op in ops.Results {
DebugLib.Logger.WriteInfoLine("Operation [" + op.Name + "]");
}
GetAll()¶
Description
Gets all the records of the Domain.CLASS_NAME class from the Database.
Return Type : Collection
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME.GetAll();
*/
Collection[Domain.Product] allProducts = Domain.Product.GetAll();
ShowMessage("You have["+allProducts.Length+"] Products in your Store!");
GetAll(int, int)¶
Description
Gets a subset of all Domain.CLASS_NAME class' records from the Database, calculating it using the specified paging options
Return Type : Collection
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Parameters
Name | Data Type | Description |
---|---|---|
pageIndex | int | Zero-based index of the page to fetch (e.g. pageIndex = 0 → bring the very first records. pageIndex = 5 → bring the records on the 6th page of the paged results |
pageSize | int | Size of the page to fetch (e.g. 1 = bring one record per page, 10 = bring 10 records per page |
Syntax
/*
//First page (index = 0), fetch 10 records
Domain.CLASS_NAME.GetAll(0, 10);
*/
Collection[Domain.Product] firstTenProducts = Domain.Product.GetAll(0, 10);
Collection[Domain.Product] nextTenProducts = Domain.Product.GetAll(1, 10);
Collection[Domain.Product] fifthFiveProducts = Domain.Product.GetAll(5, 5);
GetByKey(PRIMARY_KEY_DATATYPE)¶
Description
Fetches an object of the Domain.CLASS_NAME class from the Database, based on its Primary Key. If such a record does not exist, an Exception is thrown.
Return Type : Domain.CLASS_NAME
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Parameters
Name | Data Type | Description |
---|---|---|
key | PRIMARY_KEY_DATATYPE | The Primary Key (Identifier) of the Class, to be used to find the record. |
Syntax
/*
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetByKey(THE_KEY_OF_YOUR_CLASS);
*/
Domain.User harryPotter;
try {
harryPotter = Domain.User.GetByKey("harrypotter");
if(Domain.ApplicationRole.GetAll().Length > 0){
Domain.ApplicationRole firstRole = Domain.ApplicationRole.GetByKey(1);
harryPotter.Roles.Add(firstRole);
}
}
catch Exception x {
ShowMessage("Could not find user. Exception caught: " + x.Message);
}
GetByKey(PRIMARY_KEY_DATATYPE, bool)¶
Description
Fetches an object of the Domain.CLASS_NAME class from the Database, based on its Primary Key. If such a record does not exist, an Exception is thrown if the value of the 'throwException' parameter is true. Otherwise, a null
value is returned gracefully.
Return Type : Domain.CLASS_NAME
Static : Yes
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Parameters
Name | Data Type | Description |
---|---|---|
key | PRIMARY_KEY_DATATYPE | The Primary Key (Identifier) of the Class, to be used to find the record. |
throwException | bool | If false, suppresses the Exception thrown when the record with the specified key is not found. True, throws the exception in such cases. |
Syntax
/*
//Throw an exception if record not found
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetByKey(THE_KEY_OF_YOUR_CLASS, true);
//Suppress exception if record not found and just return a 'null' value
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetByKey(THE_KEY_OF_YOUR_CLASS, false);
*/
Domain.User harryPotter;
{
Won't throw an exception if not Found
rryPotter = Domain.User.GetByKey("harrypotter", false);
(harryPotter == null){
arryPotter = Domain.User.Create();
arryPotter.UserName = "harrypotter";
}
(Domain.ApplicationRole.GetAll().Length > 0){
/Will throw an exception if not Found
omain.ApplicationRole firstRole = Domain.ApplicationRole.GetByKey(1, true);
arryPotter.Roles.Add(firstRole);
}
}
ch Exception x {
owMessage("Could not find Application Role with Key [1]. Exception caught: " + x.Message);
}
Insert()¶
Description
Inserts (adds) the instance of the Domain.CLASS_NAME Class into the Database
Return Type : void
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME x = Domain.CLASS_NAME.Create(); //New Instance
//Set values .....
x.Insert();
*/
//Creating a Class
Domain.User user = Domain.User.Create();
user = Domain.User.Create();
user.Name = "Harry Potter";
user.Email = "harry.potter@hogwarts.com";
user.UserName = "HarryPotter";
user.Insert(); //Insert a brand new User
IsNew()¶
Description
Checks whether the instance of the Domain.CLASS_NAME Class exists in the database or not. If true, it has already been saved into the Database. False, otherwise.
Return Type : bool
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetAll().First(); //Existing
//Change values .....
bool isNew = x.IsNew(); //false
Domain.CLASS_NAME y = Domain.CLASS_NAME.Create(); //Freshly created
y.IsNew(); //true
*/
Domain.Product product = Model.Product;
//If the Product of the Form is brand new (non-existent in the DB), Insert it
if(product.IsNew()){
product.Insert();
}
//Otherwise, just update its values
else{
product.Update();
}
Merge()¶
Description
Walks an object graph and merges all objects with the persistence layer.
Return Type : void
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME x;
//...
x.Merge();
*/
CommonLib.Serializer[Domain.ApplicationUser] serializer;
ain.ApplicationUser user1 = serializer.FromJson(userJson);
r1.Merge(); // merge with session
ain.ApplicationUser user2 = Domain.ApplicationUser.GetByKey("User2");
user2.Save(); // saving user2, will casue user1 to be saved, since it is in session!
Refresh()¶
Description
Refreshes the instance of the Domain.CLASS_NAME, by re-fetching its values from the Database
Return Type : void
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetAll().First();
x.Refresh();
*/
//Let Model.Order be an instance of a Domain.Order class
//Change it a bit and save it
Model.Order.Timestamp = DateTime.Now();
Model.Order.Save();
//Refresh it from the database, so as to show the changes in the form
Model.Order.Refresh();
Save()¶
Description
Saves (Inserts or Updates) the instance of the Domain.CLASS_NAME Class into the Database
Return Type : void
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME x;
//Set values .....
x.Save();
*/
//Creating a Class
Domain.User user = Domain.User.Create();
user = Domain.User.Create();
user.Name = "Harry Potter";
user.Email = "harry.potter@hogwarts.com";
user.UserName = "HarryPotter";
user.Save();
Update()¶
Description
Updates the instance of the Domain.CLASS_NAME Class with the new changes
Return Type : void
Static : No
Available for:
Method available for... | Classes | Enumerations |
---|---|---|
Persisted | Yes | No |
Non-Persisted | No | No |
Syntax
/*
Domain.CLASS_NAME x = Domain.CLASS_NAME.GetAll().First(); //Existing
//Change values .....
x.Update();
*/
Domain.User user = Domain.User.GetByKey("harrypotter");
user.PhoneNumberConfirmed = true;
user.Update(); //Save changed, existing user