Generated code - DataAccessAdapter Functionality, Adapter
Preface
Using the Adapter template group which ships with LLBLGen Pro, you'll notice that there will be two VS.NET projects generated.
This section describes the DataAccessAdapter class which is located in the database specific project and which is the class that performs all
database activity for entities, typed lists, typed views and stored procedure calls: the DataAccessAdapter object provides the persistence service
to the developer. For successful usage of the Adapter template group, it's important to understand which functionality is offered by the DataAccessAdapter
class and this section shows a brief overview of that functionality. In-depth discussions of various aspects of the functionality can
be found in the remainder of the Adapter documentation.
Functionality
The DataAccessAdapter class is the single class you'll need to interact with the database to fill entities, store changed data, call a stored procedure,
start a transaction etc. Below is a brief overview of the various aspects of the DataAccessAdapter class.
Note:
|
The DataAccessAdapter class is not thread safe and should not be used as such. Each thread should use its own instance, it's not safe to share
a DataAccessAdapter instance among multiple threads.
|
Persistence Info
The entity classes, typed list classes and typed view classes do not contain any persistence information. When you want to read an entity from the
database into an entity class, the DataAccessAdapter consults a class called PersistenceInfoProvider, which produces based on field/object name,
the correct persistence information for the DataAccessAdapter. This is done behind the scenes, so a developer will not notice this. The PersistenceInfoProvider
uses a caching mechanism to supply the information as quickly as possible. Because it is a separate class, you are free to modify this class to retrieve
the information from another source than the generated code, for example from an XML file or database.
Connection strings
The DataAccessAdapter reads the connection string automatically from the *.config file available, however it also accepts a connection string
if you supply one. This means that you can target different databases under different users on a per-call basis.
Catalog specific persistence info (SqlServer, Sybase ASE)
Adapter uses catalog specific persistence information. This means that the catalog name is generated into the persistence information.
the result of this is that although the connection string might contain a different catalog name, the queries will use the catalog name generated into the
persistence information for each field and database object (table/view). This can be very helpful in the scenario with multiple catalogs per project,
but might not be what you want in some situations. You can overwrite the catalog name to be used on a per-call basis by specifying catalog name overwriting
information to the DataAccessAdapter's constructor or set some properties later on. Below is an overview of the options available to you:
- Single name setting (provided for backwards compatibility, not recommended).
This option lets you specify a single new name for the catalog to use, or clear the catalog names altogether. It affects all catalog names
known in the project, and therefore not very flexible. To use this option, use either the constructor of the DataAccessAdapter class which
accepts CatalogNameToUse and CatalogNameUsageSetting or set these properties of the DataAccessAdapter class after instantiation
of that class. CatalogNameToUse is important for the CatalogNameUsageSetting.ForceName as it will become the new name to use as catalog name.
- Multi name setting
Preferred way of performing catalog name overwriting. To do this, create a new CatalogNameOverwriteHashtable (type provided by the
SD.LLBLGen.Pro.ORMSupportclasses assembly). You can specify a CatalogNameUsageSetting, but this isn't required. You then add key-value
pairs, where the key is the name to overwrite and the value is the name to overwrite it with. If you specify '*' as key, all catalog names
will be set to the name specified as value, if CatalogNameUsageSetting is set to ForceName. Please see the LLBLGen Pro reference manual for more details on this object.
The created CatalogNameOverwriteHashtable is then passable to the DataAccessAdapter constructor or you can set the CatalogNameOverwrites
properties to an instance of this special hashtable.
You can also specify extra appSettings
add-tags in your application's .config file's
appSettings tag to set these overwrites.
This is provided for backwards compatibility, and not recommended.
Add an add-tag with
CatalogNameUsageSetting as value for the
key and for the
value one of the following: "0" (default),
"1" (forceName) or "2" (clear), and an add-tag
CatalogNameToUse, which should have as value the catalog name to use for each database call.
Example: (which will force a catalog name write on all database calls and will use the name "MyProductionCatalog".)
<configuration>
<appSettings>
<add key="Main.ConnectionString" value="data source=..."/>
<add key="CatalogNameUsageSetting" value="1"/>
<add key="CatalogNameToUse" value="MyProductionCatalog" />
...
</appSettings>
</configuration>
If you've specified these
settings in your application's *.config file (web.config or app.config file (which results in
executable name.exe.config)), you can just use the default
DataAccessAdapter constructors and with each call these values are read from the config file. If you specify
catalogNameToUse and
catalogNameUsageSetting
in the constructor of the DataAccessAdapter class and you specify for catalogNameUsageSetting something else than CatalogNameUsage.Default, the values specified
in the *.config file will be ignored for that particular DataAccessAdapter instance, so it is still possible to override the settings specified in the *.config
file on a per-call basis. Be aware that this is provided for backwards compatibility. See
Application configuration through .config files for a more flexible solution.
Schema specific persistence info (DB2, Oracle, PostgreSql, SqlServer, Sybase ASA, Sybase ASE)
Adapter uses schema specific persistence information. This means that the schema name is generated into the persistence information and
that the queries will use the schema name generated into the persistence information for
each field and database object (table/view). This can be very helpful, but might not be what you want in some situations, like when you've defined
global synonyms for tables in a particular schema and you want the generated code to target these synonyms in production. Having the ability to change the
schema name at runtime can also be helpful if you want to target multiple schemas with the same schema objects.
You can overwrite the schema name to be used on a per-call basis by specifying schema name overwriting
information to the DataAccessAdapter's constructor or set some properties later on.
This all sounds familiar from the previous catalog name overwriting paragraph, so the schema name overwriting options look the same as for the
catalog name ovewriting options. Below is an overview of the options you have:
- Single name setting (provided for backwards compatibility, not recommended. Only available on Oracle).
This option lets you specify a single new name for the schema to use, or clear the schema names altogether. It affects all schema names
known in the project, and therefore not very flexible. To use this option, use either the constructor of the DataAccessAdapter class which
accepts SchemaNameToUse and SchemaNameUsageSetting or set these properties of the DataAccessAdapter class after instantiation
of that class. SchemaNameToUse is important for the SchemaNameUsageSetting.ForceName as it will become the new name to use as schema name.
- Multi name setting
Preferred way of performing schema name overwriting. To do this, create a new SchemaNameOverwriteHashtable (type provided by the
SD.LLBLGen.Pro.ORMSupportclasses assembly). You can specify a SchemaNameUsageSetting, but this isn't required. You then add key-value
pairs, where the key is the name to overwrite and the value is the name to overwrite it with. If you specify '*' as key, all schema names
will be set to the name specified as value, if SchemaNameUsageSetting is set to ForceName. Please see the LLBLGen Pro reference manual for more details on this object.
The created SchemaNameOverwriteHashtable is then passable to the DataAccessAdapter constructor or you can set the SchemaNameOverwrites
properties to an instance of this special hashtable.
You can also specify extra appSettings
add-tags in your application's .config file's
appSettings tag to set these overwrites (not available
on SqlServer, for SqlServer, use the preferred way explained in the
Application configuration through .config files section). This is provided for backwards compatibility, and
not recommended.
Add an add-tag with
SchemaNameUsageSetting as value for the
key and for the
value one of the following: "0" (default),
"1" (forceName) or "2" (clear), and an add-tag
SchemaNameToUse, which should have as value the schema name to use for each database call.
Example: (which will force a schema name write on all database calls and will use the name "MyProductionSchema".)
<configuration>
<appSettings>
<add key="Main.ConnectionString" value="data source=..."/>
<add key="SchemaNameUsageSetting" value="1"/>
<add key="SchemaNameToUse" value="MyProductionSchema" />
...
</appSettings>
</configuration>
If you've specified these
settings in your application's *.config file (web.config or app.config file (which results in
executable name.exe.config)), you can just use the default
DataAccessAdapter constructors and with each call these values are read from the config file. If you specify
schemaNameToUse and
schemaNameUsageSetting
in the constructor of the DataAccessAdapter class and you specify for schemaNameUsageSetting something else than SchemaNameUsage.Default, the values specified
in the *.config file will be ignored for that particular DataAccessAdapter instance, so it is still possible to override the settings specified in the *.config
file on a per-call basis. Be aware that this is provided for backwards compatibility. See
Application configuration through .config files for a more flexible solution.
Command timeouts
Sometimes a query can take a long time to complete, for example with data-processing stored procedure calls. With Adapter, you can set the timeout for
each query on a per-call basis, using the property
DataAccessAdapter.CommandTimeOut. The default is 30 (seconds). Firebird and SqlServer CE don't support command timeouts and a CommandTimeOut value is ignored.
Connection control
It can be useful to open a connection and keep it open for multiple actions and then close it. This can give extra performance, especially in code where multiple
database fetches are used in one routine. The property KeepConnectionOpen is used to set this behaviour.
Recursive saves
The DataAccessAdapter object supports recursive saves. This also works with entity collections. The logic automatically determines the order in which actions need to
take place. For example:
- Instantiate a Customer entity, add a new Order object to its Orders collection. Now add OrderDetails objects to the new Order object,. You can
simply save the Customer entity and all included new/dirty entities will be saved and any PK-FK relations will be updated/synchronized
- Alter the Customer object in the example above, and save the Order object. The Customer object is saved first, then the Order and then the
OrderDetails objects with all PK-FK values being synced
This synchronization of FK-PK values is already done at the moment you set a property to a reference of an entity object, for example myOrder.Customer = myCustomer, if
the entity (in this case myCustomer) is not new. Synchronization is also performed after a save action, so identity/sequenced columns are also synchronized.
Fetching/deleting/saving entities/typed lists/typed views
The DataAccessAdapter object offers full support for fetching/deleting/saving entities and entity collections and filling typed lists and typed views.
It also supports, as SelfServicing, directly updating/deleting of entities in the persistent storage.
Calling stored procedures
Calling stored procedures is fully supported by the DataAccessAdapter object. The DataAccessAdapter object controls the transactions, so you can now call a stored
procedure inside an existing transaction.
Transactions
Adapter fully supports both COM+ transactions and ADO.NET transactions through the ComPlusAdapterContext (for COM+ transactions) and the DataAccessAdapter object
(for normal ADO.NET transactions). You can start a
transaction using the DataAccessAdapter class and all actions performed after that are executed inside that transaction. It doesn't matter if you fetch collections,
typed lists, delete / save entities or call a stored procedure. All multi-entity affecting actions like recursive saves and the save of an entity collection, or
the deletion of a set of entities, is, as in SelfServicing, always performed inside a transaction: if an existing transaction is present, that transaction is used,
otherwise a new transaction is created and used.
Intercepting activity calls
The DataAccessAdapter class has functionality on board to let you perform actions during various stages of a process, for example right before a save action, or
when an entity is fetched. These methods start with 'On' and are defined as virtual and are by definition implemented as an empty method (no-op). Please
consult the LLBLGen Pro reference manual and inspect the
DataAccessAdapterBase class' members for the details about these methods. DataAccessAdapterBase
is the base class for every DataAccessAdapter class.
If you want to perform a given action when one of
these methods are called, you can override them in the generated DataAccessAdapter class, preferably
using the methods discussed in
Adding your own code to the generated classes. Please consult the
LLBLGen Pro reference manual, available in the LLBLGen Pro installation folder, for details about these methods (DataAccessAdapterBase.On..)
when they're called and what is passed in.
ArithAbort flag (SqlServer only)
If an entity is saved into a table which is part of an indexed view, SqlServer requires that SET ARITHABORT ON is specified prior to the actual save action.
You can tell LLBLGen Pro to set that option, by calling the method DataAccessAdapter.
SetArithAbortFlag(bool) method. After each SQL statement a
SET ARITHABORT OFF statement will be executed if the ArithAbort flag is set to true. Setting this flag affects all INSERT statements following the call to
SetArithAbortFlag(), until you call that method again.
DQE Compatibility mode(SqlServer only)
With the arrival of SqlServer 2005 and its new features, it was required to make the SqlServer DQE be configurable so it could generate SQL which was optimal for
the database type used. To set the
compatibility mode of the SqlServer DQE in code, you can use the DataAccessAdapter method
SetSqlServerCompatibilityLevel, as shown in the following example which sets the compatibility mode to SqlServer 2005:
// C#
DataAccessAdapter.SetSqlServerCompatibilityLevel( SqlServerCompatibilityLevel.SqlServer2005 );
' VB.NET
DataAccessAdapter.SetSqlServerCompatibilityLevel( SqlServerCompatibilityLevel.SqlServer2005 )
The different compatibility modes are:
- SqlServerCompatibilityLevel.SqlServer7 (or the value 0)
- SqlServerCompatibilityLevel.SqlServer2000 (or the value 1)
- SqlServerCompatibilityLevel.SqlServer2005 (or the value 2)
The default is SqlServer2000. The values 0, 1 or 2 have to be used when you're using the .config file parameter. See for more details about that parameter
Generated code - Application configuration through .config files.
Setting the compatibility level controls the sequence retrieval logic to use by default (@@IDENTITY on Sqlserver 7 or SCOPE_IDENTITY() on 2000/2005), the
ability to use NEWSEQUENTIALID() (SqlServer 2005) and the SQL produced for a paging query: a temptable approach is used on SqlServer 7 or 2000, and a CTE
approach is used on SqlServer 2005.