At this page we collect solutions to questions arising in the NDO support forum, or other code usefull for NDO developers. Check this page regularily, because we'll add additional articles to it.
Most of the solutions are written using the NDO Enterprise or Trial Editions. You might get some compile errors, if you use the sample projects with any other NDO Edition, than the one, the solution is written with. In that case just remove the references to the NDO.dll and add a reference to the NDO.dll, which is installed at your system.
NDO Installation on Vista
Even members of the Administrator group don't have Administrator privileges on Vista. If an action requires administrative privileges, the user is presented either with a prompt for credentials or a dialog, asking, if the action is to be continued. If you do this with the NDO .msi files, it won't work. Due to some technical problems described here, the installation of .msi files with custom actions may fail if the custom actions need administrative privileges. That's exactly the case with NDO's installation scripts. The workaround is to run the msi file under the Administrator account:
- Press the Start button and type "cmd" in the search box. Cmd.exe appears above the search box.
- Right click at the cmd.exe entry and choose "Start as Administrator".
- Navigate to the folder, the NDOEnhancerEnterpriseXX.msi resides and start it by simply typing the file name and pressing enter.
Using NDO in Automated Builds
NDO can be used in automated builds. But sometimes it turns out to be a little bit tricky to really get the enhanced assemblies instead of the not enhanced assemblies. This article shows, how to use NDO in automated builds. Along the way we show some hints how to organize these kind of projects.
Object Viewer sample application shows how to dynamically load persistent types
This Windows Forms application doesn't know anything about the persistent objects it has to handle. All you have to do is to open a mapping file which resides in a directory along with assemblies containing the persistent types. After loading the mapping file you can query for objects and view the object tree in a TreeView window. You can change objects using a PropertyGrid and you can create and delete objects and assign it to relation containers. You can use this tool to edit the persistent objects of your applications.
Indexes on Guid Primary Keys
By default Sql Server creates a clustered index on each primary key column. That means, that the physical order of the data records is rearranged to reflect the order of the primary key values. This makes sense with autoincremented integer PKs because any new record will always have the highest PK value and therefore the new record will simply be added at the end of the index. With Guid Keys any insert might cause the rearrange of the data records to reflect the new order of the primary keys. Currently NDO produces primary key constraints with clustered indexes even if the PK type is Guid. This will be changed in the next release. In the mean time you can do the following:
- For new tables alter the primary key constraint in the DDL script to "PRIMARY KEY NONCLUSTERED".
- For existing tables use the following code for each table:
ALTER TABLE [dbo].[TableName] DROP CONSTRAINT [PK_TableName];
ALTER TABLE [dbo].[TableName] ADD CONSTRAINT [PK_TableName] PRIMARY KEY NONCLUSTERED ([ID]);
This might cause a better performance of inserts in big tables.
Using more than one Sql Server database file in an application
An NDO user asked, if it is possible, to work with different copies of Sql Server database files, all having the same structure but different content. In the link above you can find the Support Forum entry in which we describe how to accomplish this.
Extending the NDO provider for Sql Server to support nvarchar(max)
NDO providers are small software layers between NDO and the ADO.NET providers. You can easily adapt the providers delivered with NDO to suit your needs... more Info. (Note, that nvarchar(max) is supported since NDO 1.2 SP 1.
Sample for a User-defined PersistenceHandler, allowing to map to Multi-column Primary Key
NDO can be extended with so called PersistenceHandlers. Normally they are intended to support storing objects in Non-Sql-Environments. In this sample the NewPersistenceHandler class intercepts the standard PersistenceHandler to change query texts and parameters. All column names are hard coded in this sample, but they could easily be read from the mapping file. Note, that NDO will support mapping to multi-column PKs in the upcoming Version 2.0.
Working with Transactions
The sample code shows an association between two classes. A parent and child object are created and saved within the same transaction. The app shows the Sql dump, so you can see the statement order and the beginning and end of the Tx.
Simple NDO Based Website
The sample code shows, how persistent objects can be bound to grids in an web application. Don't expect a lot of functionality ;-). The sample requires VS 2005.
Container Based Replication
This article describes a system consisting of a Website and a Desktop Application which both use the same set of persistent business classes. Thus they can share the same logic on the web server and on the desktop machines. While the Web Application provides an online catalog and the possibility of ordering products online, the Desktop Application can be used to send bills and process payment (which is actually not implemented here). The two applications communicate with each other using a Web Service and share a subset of the same data. But the Web Server needs to work with it's own database. Therefore we need a possibility to transfer objects from the Desktop Application to the Web Server and vice versa. The NDO Enterprise Edition allows the replication of objects using distributable object containers.
DataBinding in ASP.NET Applications
The article Building a Web Site with Container Based Replication contains a chapter about how to use the DataBinding capabilities of .NET 2.0 and VS 2005 with persistent objects. It's worth to read it!
DataBinding in Windows Forms Applications
Data Binding to lists of objects is as easy as working with datasets in .NET 2.0 / VS 2005. Have a look at this sample app, to see, how it works. It uses a BindingNavigator control and an overloaded BindingSource component. That way it's possible to write a NDO based CRUD application with only three lines of code. (CRUD = Create Read Update Delete).
Part List Sample
This sample code shows, how to deal with the part list problem, also known as the "Composite Pattern". The class Part has two relations to itself, in the roles subParts and parentPart. Note, that this pattern must use assoziations and must not use composite relations. We recommend strongly to use Guid id's, making it possible to store a whole tree of objects like shown in the code. The code shows also how to move a subtree from one parent to another parent.
Always use parameters in queries...
...in other words: never use literal values in queries. Parameters in NDOql will be translated in Sql parameters. Queries using Sql parameters are not vulnerable to sql injection attacks and they run faster, if the same query occurs oftener. That's because the Sql Server (like other databases) compiles queries and puts the compiled queries in a cache, where the compiled results can be reused. With parameters, more queries appear to be the same query and the compiled query can be taken from the cache. Read more Information about this matter in this CodeProject article.