Blog信息 |
blog名称:注册会计师(注会)练习软件 日志总数:398 评论数量:116 留言数量:27 访问次数:3267754 建立时间:2005年6月6日 |

| |
[delpih编程]Write your own PersistenceMapper component[放到这里备查] 软件技术
吕向阳 发表于 2009/3/14 19:29:41 |
Short tutorial on how to write your own database support with Eco II, Update 2
This tutorial is brief, and rather technical. If you are planning to use Eco
together with a database that is already supported (Interbase, MsSql, Oracle, Db2,
Informix, I would like to play the old Jedi trick on you...
Me: You don't need to see this information.
You: I don't need to see his information.
Me: This is not the information you are looking for.
You: This is not the information I'm looking for.
Me: You can go about your business.
You: I can go about my business.
Me: Move along
Shhhhhh... I'm just waiting for the standard users to slip away unnoticed, with a
blank stare... That old Jedi trick works every time
Now, the rest of you, I'm going to assume that you are either database vendors that
want to implement support for your own database, or that you are a die hard fans of
some database that you just can't live without and you have now discovered that you
can't live without Eco either. Welcome!
Eco is designed to support multiple database providers. Out of the box comes support
for Bdp and the native Microsoft SqlConnection. To write your own support for a
another database backend, there are couple of things you need to do:
Write your own PersistenceMapper component
Write your own implementation of the Eco IDatabase interface (and related
interfaces)
Write a set of AttirbuteMappers for the various datatypes that your database
supports
If the database architecture you are planning to support already has a Bdp provider
(but is not yet supported by Eco), you only need to do the last part above, and most
of the attribute mappers can be reused, but if you plan on supporting a new Ado.Net
provider, you will need to do all of the above.
The easiest way to do this is to copy/paste an existing implementation (that's what
I do myself when I add support for another database). Download the example for
MimerSql to use as a starting point. In your Eco source folder there is also the
source code for Bdp and SqlClient support.
Write your own PersistenceMapper component
Your new component should be a subclass of
Borland.Eco.Persistence.PersistenceMapperDb. In the Mimer example, look at the
PersistenceMapperMimer.cs for an example on how to subclass your own
PersistenceMapper.
Write your own implementation of the Eco IDatabase interface
Most of this work is done in the framework class AbstractIDatabaseImpl (and related
classes). In the Mimer example, please refer to the file MimerDatabaseImpl.cs for an
example on how to implement the required interfaces.
Parts of the code to do this is to make sure that the correct adapter is created for
queries and params. There are two methods that are metadata related that should be
implemented in order to support CreateDatabase and Evolve:
public override StringCollection AllTableNames(string pattern, bool systemTables)
public override DataTable GetIndexDefsForTable(string tableName)
The method AllTableNames should return a StringCollection with the names of the
table that exist in the database. The pattern parameter can be used to narrow the
search. If it is a non-empty string, only tables whose name matches the pattern
(contains the pattern as a substring) should be returned (Eco itself will only use
this to search for whole tablenames, never partial tablenames). If the systemTables
parameter is true, then system tables should be included in the result, otherwise
they should be ignored.
GetIndexDefsForTable should return a DataTable with the indices that are defined for
a table. The DataTable should contain columns called "ColumnName" and "IndexName"
and these columns should contain the obvious information... This information is used
by DbEvolution to drop any indices/foreign keys before dropping columns they refer
to. GetIndexDefsForTable is only required for DbEvolution. If you don't want to
support DbEvolution, you can return null/nil. The worst that can happen is that
DbEvolve fails if it tries to drop a column that has an index (if this is not
allowed by your database). For a sample implementation for SqlServer, please look
here
Write a set of AttributeMappers for the various datatypes that your database
supports
AttributeMappers are small classes that handle the conversion from database values
to values to be used in the object layer. Often this conversation is trivial, such
as the case with the column type INTEGER being mapped to a System.Int32 in memory.
For other datatypes the conversion requires a little more effort (datetime,
timestamp, blobs, floats etc). For every type that you are going to use in the
model, you need to install an attribute mapper (these are installed in
PersistenceMapper.SqlDatabaseConfig.PersistenceMappers). Every database has its own
little quirks when it comes to datatypes and how they are handled in Ado.Net. Most
of the standard types are handled by generic attribute mappers (declared in the file
DefaultAttributeMappers.cs, in the source folder of Delphi) To write a new mapper,
you should normally be able to copy/paste an existing similar mapper and adjust the
parts you need.
In the Mimer example, There are new mappers for both the Mimer Bdp provider and the
MimerConnection. Please let me know if you have implemented support for any new
databases, and I'll put a link to your files here.
Questions
I'm sure I have not been able to explain every detail here. If you decide to try
this out, and you run into problems, Send me an email (unspamify the adress) and
I'll try to get you on the right track |
|
|