|
Post by joncl84 on Mar 9, 2021 23:05:05 GMT
Hi, I have some questions about the use of the class-based ORM: - Inheritance: If I create an ORM class that inherits from another, will the properties of the parent class be detected when dynamically creating tables?
- Interfaces: If I create an ORM class that implements a property to ensure a specific property is present in the class: would this create any issues when dynamically creating tables? will this work later on to create generic methods to encapsulate the Linq commands?
- Methods: If I put methods in the ORM classes, will they somehow interfere with the analysis required to dynamically create tables?
- Multiple Tables of one Class: As far as I've been able to understand, when using ORM and Linq, each ORM class refers to a single table. So, I'm currently creating ORM classes that are analog to preexisting classes I want to store, creating conversion methods. However, I need to create two different tables to store members of the same class, so I had to create two different ORM classes to create two tables... Is there a way to do this using simple Linq commands, or this would this force me to use sql commands?
Thanks!
|
|
|
Post by echo17 on Mar 10, 2021 10:46:40 GMT
Inheritance: If I create an ORM class that inherits from another, will the properties of the parent class be detected when dynamically creating tables? Yes, inheritance will work with dynamically creating tables. All fields from the parent class will be included. Example: public class NewTableBase { public int ID { get; set; } }
public class NewTable : NewTableBase { public string Name { get; set; } }
public SimpleSQLManager dbManager;
void Start() { dbManager.CreateTable<NewTable>(); }
This will result in a table in the database with both ID and Name fields. Interfaces: If I create an ORM class that implements a property to ensure a specific property is present in the class: would this create any issues when dynamically creating tables? will this work later on to create generic methods to encapsulate the Linq commands? I have not encountered any issues when using interfaces. You can set the constraints, then implement them in your classes without interference. Example:
public class NewTableBase : INewTableConstraint { public int ID { get; set; } }
public class NewTable : NewTableBase { public string Name { get; set; } }
public interface INewTableConstraint { int ID { get; set; } }
public SimpleSQLManager dbManager;
void Start() { dbManager.CreateTable<NewTable>(); }
Methods: If I put methods in the ORM classes, will they somehow interfere with the analysis required to dynamically create tables? You can use methods in your classes without interference. I use them all the time to do data translation between my database and what I need in my code. Example:
public class NewTable : NewTableBase { public string FirstName { get; set; } public string LastName { get; set; }
public string FullName() { return FirstName + " " + LastName; } }
Multiple Tables of one Class: As far as I've been able to understand, when using ORM and Linq, each ORM class refers to a single table. So, I'm currently creating ORM classes that are analog to preexisting classes I want to store, creating conversion methods. However, I need to create two different tables to store members of the same class, so I had to create two different ORM classes to create two tables... Is there a way to do this using simple Linq commands, or this would this force me to use sql commands? So the ORM can be used in two ways: 1. To store the results of a query, whether that is a single table or joined tables SELECT W.WeaponID, T.Description AS WeaponDescription FROM Weapon W JOIN WeaponType T ON W.WeaponTypeID = T.WeaponTypeID
Where the ORM class would be: public class Weapon { [PrimaryKey] public int WeaponID { get; set; }
public string WeaponTypeDescription { get; set; } }
This ORM is the resulting join of two tables and does not correlate directly to the fields in each table, only what is being returned in the query. Note that the class doesn't even need to contain the actual field name for the weapon type description, instead containing an alias. For this use of ORM, it is not suitable to create a new table unless you wanted those exact fields. 2. To create tablesThis is the one you are interested in based on your question. If you have two tables that have the same structure, you can use inheritance to create two different tables, since the table name is the name of the class. Something like: public class TableBase { public int ID { get; set; } public string Name { get; set; } }
public class TableA : TableBase { }
public class TableB : TableBase { }
public SimpleSQLManager dbManager;
void Start() { dbManager.CreateTable<TableA>(); dbManager.CreateTable<TableB>(); }
|
|
|
Post by joncl84 on Mar 10, 2021 14:06:37 GMT
Thanks a lot for your anwer!! Inheritance will save me a lot of work!
|
|