Παρουσίαση με Ετικέτες

Connecting to any database at runtime
12 Μαρτίου 12 02:53 μμ | tolisss | 0 σχόλια   

In this post we will discuss how straight forward is to provide a solution for this science fiction subject! Our frameworks (XAF and XPO) along with a few smart classes are fully capable of doing all the hard job and spare our resources. At the end we will be able to apply all the XAF tools / modules in any legacy database. The module can connect the database world with powerful tools like the DX grids. In addition we can use use XAF’s main metadata storage (the model), the security system and all XAF’s modules. These tools are are so powerful that are able to produce logic by themselves! Having such great configuration flexibility it is possible to write behavior after distribution thus you enter new markets and ideas! In addition the module allows you to control the logic after generation using Xaf’s meta driven developing model.

Retrieving any database metadata

XPO is the best candidate for this. since it can transparently talk  to more than 15 different database systems!

The metadata API used from XPO to describe its supported databases is very simple as shown,

image

In a nutshell for each supported database, XPO will return a set of DBTable classes. Each DBTable will be associated with one DBPrimaryKey class and a collection of DBColumn and DBForeighKey classes. Next is the code required to get the DBTable classes out of database and  with no surprise again is very simple!

var storeSchemaExplorer = ((IDataStoreSchemaExplorer)XpoDefault.GetConnectionProvider("ConnectionString", AutoCreateOption.None));

var storageTables = storeSchemaExplorer.GetStorageTables(storeSchemaExplorer.GetStorageTablesList());

foreach (DBTable dbTable in storageTables) {

    foreach (DBColumn dbColumn in dbTable.Columns) {

        //now we are ready to do magic!

    }}

Connecting at runtime – The magic part

In order to query and create CRUD operations for a database system XPO requires a set of business classes. However, now there is a runtime factor involved. We need to create these business classes dynamically.

XAF’s modularized architecture made this task too easy for us! We already created a dynamic class generation module called WorldCreator in the past. WorldCreator maps the structure of an assembly into persistent classes.

image

What we need is for each database to create a new PersistentAssemblyInfo, then for each DBTable a new PersistentClassInfo and finally for each DBColumn a new PersistentMemberInfo. In pseudo code this will look like,

var persistentAssemblyInfo = ObjectSpace.CreateObject<PersistentAssemblyInfo>();

foreach (DBTable dbTable in storageTables) {

    var persistentClassInfo = ObjectSpace.CreateObject<PersistentClassInfo>();

    persistentAssemblyInfo.Name = dbTable.Name;

    persistentAssemblyInfo.PersistentClassInfos.Add(persistentClassInfo);

    foreach (DBColumn dbColumn in dbTable.Columns) {

        var persistentMemberInfo = ObjectSpace.CreateObject<PersistentMemberInfo>();

        persistentMemberInfo.Name = dbColumn.Name;

        persistentClassInfo.OwnMembers.Add(persistentMemberInfo);

    }

}

WorldCreator UI allows further extension of the model as simple as writing .NET code or creating new objects. More ideas and contributions are always welcome.

Up to this point everything was extremely easy for us and almost shocking in terms of productivity! I believe there is no reason to change that feeling so lets move to our final task.

WorldCreator is simply a code generation module. It generates assemblies which include XAF modules and loads them at application startup. However that assembly has no difference at all from the assemblies we create at design time. The persistent classes of this dynamic module belong and point back to main XAF database. What we want here is to redirect the generated sql statements back to the original mapped database.

Once more we have most of the job ready! eXpand already uses a proxy version of ObjectSpaceProvider to support scenarios similar to show Business Classes from several databases in a XAF application. Thus associating a WordCreator assembly (set of business classes) with a database is a rather easy job.

Connecting to any database at runtime at first sounded very complicated. However our frameworks proved once more that they can raise our productivity and make our jobs so simple that even such complex task can fit in a small post like this one.

The new module DBMapper, is already released as part of eXpand framework’s modules collection.

We would appreciate your feedback on this post. Has it been useful to you? Feel free to contact us with any further questions.


Δημοσίευση στην κατηγορία: , ,

Search

Go

Το Ιστολόγιο

Ιστορικό Δημοσιεύσεων

Συνδρομές