LUNA - Data Layer Code Generator for Vb.Net by Diego Lunadei
Luna is open source and freely downloadable automatic code generator to facilitate and standardize the writing of classes that save data to database. The code is generated in VB.NET language and can be used in both Desktop and Web application.
Luna project start from 'need to write in an optimal manner and easily manage the most boring and repetitive data access, while maintaining an easily understood standard'. You should not embed anything in your project, and all generated code is easily changeable or customizable by the developer. So...
How does it work?
Structure
Interpreter
Converter
Structure
Luna has a logic of its ownership structure tables, fields and relations with which to map an existing database into memory in a standardized format.
Interpreter
It's an interpreter for each specific type of database, so it is possible to support virtually any type of database. The interpreter is responsible for connecting to the origin database specified, read the table structure and relations in memory and create a logical structure that follows the first in a field using the Luna classes, tables and relations.
Converter
Once the database structure has been physically disconnected from the data, the Converter create code that is used to interface Application with the database.
Pratical Example
Luna creates the code of classes that represent each table, the code of the DAO classes that deal with interfacing with the database objects and logical SQL that could be used to replicate the database structure to another storage.
Now we come to the practical aspect. So we start from a simple test database containing the tables Customers, CustomersGroups and and Phones structured as follows:

Step - 1
We start Luna and, after selecting the type of database access and selecting the file, click on the button "Load DB";

* Source type - Luna now support Access Database (*.mdb and *.accdb), SQL Server and Luna Project File (*.lds).
A Luna Project File is a file automatic created by Luna after a code generation, saved in the main folder of generated files. Opening this file as source you can work disconnected from original DataSource taking all the name you have insert for any table and class.
The option "Load View" load any view as a table and generate code for that. Note that this function create method for insert, delete and update. This is wrong and in future release i resolve the problem.
Step - 2
On the next screen, you will find all the tables (TABLE) and view (VIEW) that are found in the selected data source. You can change the Class name generated by double click on the class, so you can support Pluralization. In this example, let's leave them all selected and click on the "Create Code";

What is generated?
Luna store the generated class in separated files, so you can replace them with newer version without loose your custom code. Luna generate:
LunaBaseClass; the base class of all classes generated by Luna
LunaBaseClassEntity; the base class of all Entity object;
LunaBaseClassDAO; the base class of all Dao object;
LunaSearchParameter; the Luna class used for find method;
LunaContext; the class that manage connection to database;
Next, for any selected table, Luna create a Partial Class, an EntityClass and a DaoClass.
PARTIAL Class
#Region "Database Field Map"
'Here Luna generate Property that maph all field of table.
End Region
#Region "Method"
Read(Id) as TableClass
Save(TableClass) as Integer (return Id insert in database)
#End Region
#Region "Embedded Class"
'Here Luna generate Property and List Of object related to this Table.
#End Region
DAO Class
DAO Class contain this method:
Read(Id) as TableClass
Save(TableClass) as Integer (return Id insert in database)
Delete(Id)
Delete(TableClass)
GetAll() as List (Of TableClass)
Find() as List (Of TableClass)
ReadSerialize() - Load object from XML file
SaveSerialize() - Write object to XML file
ENTITY Class
Entity Class it's an empty container, integrated with relative Partial class. ATTENTION!!! You must write your code HERE, so you never lost your custom method or function if you replace luna generated code with newer version.
Here is the Diagram Class generated by Luna:

We have the code!
Now we add to our project an DLL code library and drag and drop all file created by Luna, manteining the Folder structure as is:

Connection to Database
You can connect Db in many modes. The best thing in desktop application is create your own Connection in the Main Project, and after open that set the connection for LunaProject using LunaContext, as is:

If you don't specify any connection in LunaContext, LunaDaoBaseClass try to search for a ConnectionString in setting of project (optimal for web application) . You can anywhere send a Connection to single LunaClass to use different connection in a project. Here the New method of LunaDaoBaseClass:

READ Example
Dim Cust As New Customer
Cust.Read(18) 'object with id 18 loaded
SAVE Example
Dim Cust As New Customer
Cust.CustomerName = "Homer Simpson"
Cust.Address = "Evergreen Terrace, 740"
Dim IdInserted As Integer = Cust.Save
If IdInserted = 0 Then MessageBox.Show("Error")
or
Dim Cust As New Customer
Cust.CustomerName = "Homer Simpson"
Cust.Address = "Evergreen Terrace, 740"
Dim MgrCust as new CustomersDAO
Dim IdInserted As Integer = MgrCust.Save(Cust)
If IdInserted = 0 Then MessageBox.Show("Error")
SEARCH Example
Find all phone number of a Customer
'SETTING PARAMETER
Dim Par as new LunaSearchParameter("CustomerID",1)
Dim Mgr as New TelefoniDAO()
Dim ListPhone as List (Of Phone) = Mgr.Find(Par)
Find all phone number of a Customer that start with 06
'SETTING PARAMETER IN THE CONSTRUCTOR
Dim Par as new LunaSearchParameter()
Par.FieldName = "CustomerID"
Par.Value = 1
'SETTING SECOND PARAMETER EXPLICITY
Dim Par2 as new LunaSearchParameter("PhoneNumber","06%","LIKE")
Dim Mgr as New TelefoniDAO()
Dim ListPhone as List (Of Phone) = Mgr.Find(Par,Par2)
CHILD CLASSES
In each class LUNA create access to the objects of child classes. In any case, it is lazy boot, or which shall be made only if actually called.
In case of relations between the classes of type 1 to 1, is created a property RelatedObject.
In the case of relations between the classes of type 1 to N is created a List Of (RelatedObject).
The list of RelatedObjects are loaded only when accessing methods.
TO MAKE THIS YOU DON'T WRITE A ROW OF CODE!!!
I hope you enjoy this project.
DOWNLOAD
Executable - http://www.diegolunadei.it/luna/Luna.rar
SourceCode - http://www.diegolunadei.it/luna/LunaSource.rar
REFERENCES
For references, questions, suggestions or criticisms, you can find at:
Luna website on Facebook - http://www.facebook.com/LunaCodeGenerator (please click on "I like")
My website - http://www.diegolunadei.it
Email - d.lunadei@gmail.com
One last thing, Luna is an evolving project. Please take the documentation of this tutorial as indicating the general operation because it is a job to keep it updated. To see what actually creates the package, I recommend you download and run a build.