Databases Reference
In-Depth Information
If you need to access the list of all tables, you can use the
Tables
property. It is a read-only
collection of
MetaTable
instances, which you can iterate through using a
foreach
loop or
interrogate using LINQ to Objects. If you are interested only in tables that will be visible
to the current user, you can use the
VisibleTables
property.
NOTE
The
VisibleTables
property of the
MetaModel
class returns a read-only list of tables,
despite its type,
List<MetaTable>
, which implies the ability to change it. Any changes
you make to the
VisibleTables
list are ignored, and you should treat it as a read-only
collection.
Keep in mind that iterating through the list of tables at runtime can be costly, so if you
need a particular table, use one of the methods the
MetaModel
class provides for that
purpose instead:
public MetaTable GetTable(string uniqueTableName);
public MetaTable GetTable(Type entityType);
The
GetTable
method that takes a string that contains a table name is the most efficient
way to retrieve a specific table from the
MetaModel
. This method retrieves the
MetaTable
from the dictionary of table names and objects that
MetaModel
maintains internally. You
typically use one of these methods when building custom pages that don't rely on
dynamic data URL routing:
MetaTable table = Global.DefaultModel.GetTable(“Products”);
MetaTable table = Global.DefaultModel.GetTable(typeof(Product));
Although using the
GetTable
overload that takes an entity
Type
is more elegant (because it
allows you to avoid using hard-coded literals or string constants to specify the table
name), it is actually less efficient as it iterates through the entire list of tables to find the
one with a matching type. Although the performance impact of a single table lookup
might not be noticeable, keep in mind that it quickly increases as your data model
becomes more complex and as the number of users accessing your application increases.
NOTE
The
GetTable(string)
method is also used by the
DynamicDataRouteHandler
when
routing dynamic URLs to page templates. It extracts the table name from the URL, calls
the
GetTable
method of the
MetaModel
, and stores the
MetaTable
object it returns in
the
Items
dictionary of the
HttpContext
. If you simply need the
MetaTable
object for the
current request, the fastest way to access it is by calling the static
GetRequestMetaTable
method of the
DynamicDatarouteHandler
class, as shown here:
MetaTable table = DynamicDataRouteHandler.GetRequestMetaTable(Page.Context);