Code Generation (Extension classes)

Description

Main purpose of these classes- is to be used as table rows instead of generic BGEntity class.

This is the main type of Code Generation. It removes all boilerplate code and provides super easy and slim API for accessing table rows and their fields.

Advantages of using CodeGen

  1. No boilerplate code
  2. Fastest available method for accessing field values
  3. Compile time check (if field names are changed you'll get compilation error)

Details

Accessing fields without CodeGeneration

Accessing fields with CodeGeneration

Accessing rows without/with CodeGeneration

More Details

Generated classes are inherited from generic BGEntity class. These classes can not be added to GameObject, cause they are not Unity's MonoBehaviour (there are also another generated classes, which can be added to GameObjects, learn more here). When this CodeGen is enabled, database populated not with basic BGEntity objects, but with these extension classes. So when you access database like this var entity=meta[0]; entity variable has this extension type with all the fields generated as properties.

Additionally, you will get all main table(meta) methods alternatives as static methods of that particular class, like this:

public static BGE_MyTable FindEntity(Predicate<BGE_MyTable> filter)
{
}
public static List<BGE_MyTable> FindEntities(Predicate<BGE_MyTable> filter)
{
}
public static void ForEachEntity(Action<BGE_MyTable> action, Predicate<BGE_MyTable> filter=null)
{
}
public static BGE_MyTable GetEntity(BGId entityId)
{
}
public static BGE_MyTable GetEntity(int index)
{
}
public static BGE_MyTable NewEntity()
{
}

so instead of doing this, for example:

    var repo = BGRepo.I;
    var meta = repo["MyTable"];
    BGE_MyTable entity = (BGE_MyTable) meta[new BGId("HThOdRCxlEGU1aefSG+Nbw")];
you can do this:
    BGE_MyTable entity = BGE_MyTable.GetEntity(new BGId("HThOdRCxlEGU1aefSG+Nbw"));

using generated BGE_MyTable class.

How to enable/disable

1) Open BGDatabase window from Unity menu Window->BGDatabase

2) Chose Addons->CodeGen

3) Toggle enabled on and fill all parameters:

  1. Code Generator - leave it as it is
  2. Source file - all generated classes will use one single .cs file. You can place it anywhere in your project, but not under Editor folder
  3. Package - package (namespace) for you classes.
  4. Classes names prefix - prefix for your classes names. For example you can use prefix like D_ to distinct your database classes from the rest of the project classes
  5. Field names prefix - the same as classes names prefix, but for fields.

After you fill all parameters, press Generate button to generate classes.

Note, you should regenerate these classes after each change to database structure (e.g. if you add/remove tables or fields, or changing their names)