The last two weeks I have been looking into DB4O's object database.
I really wanted to do someting with it and thought why not create something generic to generate a persistance layer for business objects.
Wouldn't it be nice if you would just create your object oriented business layer and generate some object oriented persistance layer so objects can be stored and retrieved with real ease? This persistance layer doesn's have to be the final persistance layer; during development it can sometimes be a hazzle to create and maintain tables, stored procedures and data layer classes. What if we could use a tool which would look into your business DLL's and generate an assembly with persistance and retrieval methods for those objects and would delegate that persistance to DB4O?
First a note... this application is a personal project and in a test phase. Normally this tool is meant for personal, non-commercial use only. If you do want to use this tool otherwise, contact me and DB4O. DB4O deserves credits and payments for their software when used commercially. So if you intend to use this that way, buy sufficient licenses for DB4O.
Okay, let's continue..
Lets have a simple class model with 3 classes (Customer, Order and Product). Our generator will use System.Reflection to look into the classes in your business DLL and simply create a Persist method for each class it encounters. This persist method will take care of storing the object in DB4O.
Then, the generator will examine which private fields are available on the class. It will generate a RetrieveBy method for every private field it encounters. Because I couldn't find out if and how DB4O is capable of retrieving objects using public properties, for now the generator uses the private fields.
So this is simply what it does... how do you use it?
First download the generator here. It contains the .EXE and the DB4O 4.3 dll.
Well, the generator is simply a console application. For the above example I have built a VS.NET 2003 project which is available here. It contains the three classes but there is something you have to take a look at. If you look in the project properties , Common Properties and then Build Events, you can see that there is a Post-Build Event supplied. It contains :
C:\temp\nofgen.exe /o:"c:\temp" /s /a:"$(TargetPath)"
So, put the generator in c:\temp (or adjust this post build event). The $(TargetPath) just points to the DLL built by this project. This is great, don't you think!? Create (and update) a persistance layer every time you build the business layer?....
The /o argument means the outputpath, /s means it should also generate a source code file and the /a targets the business dll.
Off course, it is also possible to run the generator as a stand alone console application :
If the build was succesfull, you will find a DLL called 'NOFPersistanceLayer.dll' in the Bin directory in the supplied output folder.
You can reference this DLL from your application to utilize the persist and retrieval methods :
1: NOFGenTestLibrary.Customer c = new NOFGenTestLibrary.Customer("Robert Jan"); 2: NOFPersistanceLayer.Customer.Persist(c);
3: NOFGenTestLibrary.Customer[] temp = NOFPersistanceLayer.Customer.RetrieveBy_name("Robert Jan");
First this will create an instance of a Customer with the name 'Robert Jan'. Then, it will call the persist method in the generated assembly to store this instance. Afterwards, the retrieval method 'by name' is used to retrieve all Customers with the name 'Robert Jan'.
Cool isn't it?
I am really interested in your opinions and suggestions.. is this usefull or not? what else do you think it should or shouldn't do? let me know!..
Posted
03-11-2005 4:37 PM
by
Robert Jan van Holland