Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Visual Basic

Luna Data Layer Code Generator for VB.NET

Rate me:
Please Sign up or sign in to vote.
4.56/5 (8 votes)
9 Sep 2011CPOL6 min read 60.3K   4.1K   38   21
This article discusses Luna Data Layer Code Generator for VB.NET.

Luna is open source and freely downloadable.

Index

What Is It?

Luna is an automatic code generator to facilitate and standardize the writing of classes that deal with saving data to an application.

The code is generated in VB.NET language and can be used in both a Desktop and Web.

Everything comes 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.

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 database specified, read the table structure and relations in memory and create a logical structure that follows the first in a field using the classes, table and report to the Luna.

Converter

Once the database structure has been physically disconnected from the data, the drive is to create code that is used to interface seamlessly with the database.

Practical 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.

We come to the practical aspect. So we start from a simple test database containing the tables Customers and phones connected to each other and structured as follows:

Image 1

Now we can launch the Luna and, after selecting the type of database access and selecting the file, click on the button "Load DB";

Image 2

On the next screen, you will find all the tables that are found in the selected data source. In this example, let's leave them all selected and click on the "Create Code";

Image 3

What is Generated?

First, Luna LunaBaseClass generates a class from which all other inherited classes are created. It then creates a class that inherits from LunaBaseClassDAO LunaBaseClass which has more references to the data connection and to inherits the DAO classes. It also creates a class of which we will see later LunaSearchParameter operation.
After that, a table is created for each class - a class of logic and data access (DAO).

Specific Example

  • Customers from the Customers table mapping inherits from LunaBaseClass
  • Telephones inherited from the Products table mapping LunaBaseClass
  • ClientiDAO methods of reading, saving, searching, listing and deleting the class inherits from cClienti LunaBaseClassDAO
  • TelefoniDAO methods of reading, saving, searching, listing and deleting the class inherits from cProdotti LunaBaseClassDAO
Image 4

Image 5

And Now We Have the Code?

Now the easiest thing is to click the Save button and select a folder where Luna will save all the files that were generated.

Then, we start Visual Studio and create a test project of type Windows Application Form in the same solution and then add a Class Library project type.

First in the Windows Forms project, add the references to Class Library project. In the class library project, click on Add Existing Item, select all the files that were saved by Luna and you're done.

Image 6

Connecting to db

LunaBaseClassDAO inherits the class from which all classes of New DAO have three constructors:

  • New () assumes that there is a name string ConnectionString in the settings of the project with its connection string and takes care of itself to create the connection (good for the Web Application)
  • New (Connection) accepts as input an object of type connection
  • New (ConnectionString) accepts as input a ConnectionString

The connection is Shared in a procedure, so if you need to instantiate objects and do not want to use the connection string you can just use a single statement in New specified in the connection, the other will not be necessary.

Of course, in desktop applications or just comment Permanent connection calls in the New () and comment on the statement of LunaBaseClassDAO _Cn making public the connection, and everything will still work.

Important! Luna is not about closing the database connection. The locking function is inherent in any class DAO but not called to give the developer the decision on when and how to handle closing the connection.

Inserting

To make an entry in our case of a mobile, just type:

VB.NET
Dim Mgr as New TelefoniDAO() 
Dim Tel as New Telefoni()
 
Tel.NumeroTel = "06.181818" 
Tel.IdCli = 1 

Mgr.Save(Tel) 

After the Save method, Tel.IdTel will be filled with the value Id in the database.

Search

To search the Luna creates two DAO methods in each class:

  • getAll method that returns a List (Of ClasseOggetto) of all objects of that class, that you can possibly pass a search criterion.
  • The Find method instead returns a List (Of ClasseOggetto) under one or more search parameters.

The method expects an array of parameters and combines the search according to all parameters that are passed. The parameters are objects of type LunaSearchParameter, we see two simple examples:

Let us see all phones of a particular customer

VB.NET
'IMPOSTIAMO IL PARAMETRO 
Dim Par as new LunaSearchParameter("IdCli",1) 

Dim Mgr as New TelefoniDAO() 
Dim Lista as List (Of Telefoni) = Mgr.Find(Par) 

Let us see all phones of a particular customer starting with 06

VB.NET
'IMPOSTIAMO IL PRIMO PARAMETRO NEL COSTRUTTORE 
Dim Par as new LunaSearchParameter() 
Par.FieldName = "IdCli" 
Par.Value = 1 

'IMPOSTIAMO IL SECONDO PARAMETRO IN MODO ESPLICITO 
Dim Par2 as new LunaSearchParameter("NumeroTel","06%","LIKE") 

Dim Mgr as New TelefoniDAO() 
Dim Lista as List (Of Telefoni) = Mgr.Find(Par,Par2) 

First, it is necessary that the database application has been designed and built. Once that is done, the database is created as input to the Luna that will create the actual classes.

The main components of the Luna are:

Child Classes

In each class, you 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 the case of relations between the classes of type 1 to 1 is created in any type of property ClassePadre a ClasseFiglio.

In the case of relations between the classes of type 1 to N is created in each ClassePadre a List Of (ClasseFiglio).

The list of objects ClasseFiglio are loaded only when actually accessing methods.

This is just a simple example to understand how the various methods created are trivial. The important thing is that it was not necessary to write a line of code to interface with the database.

Implement Programs

In order of importance:

  • The relations between fields are read only by Microsoft Access, I'm writing these notes from Microsoft SQL Server
  • Interpreter for SQL Compact DB 4
  • Expand the information shown on the tables loaded from the database
  • Save and implement more elaborate methods to deal to save the child classes also in the relevant tables
  • Interpreter for Oracle DB
  • Interpreter for DB Orient
  • Entering the code in the resource file to allow the automatic creation of code in any language (e.g. C#)

Conclusions and References

I can only say one thing, I have not invented anything. Luna is simply a free tool that can help the developer in writing repetitive code to avoid errors and permit standardization and code cleanup.

Writing code that is maintainable and easy to manage by others is not an easy task, especially in large groups. Also, interface with the database becomes a standard for trivial operations.

For references, questions, suggestions or criticisms, you can find me at:

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat code Pin
funnyshikhar1-Feb-15 0:40
funnyshikhar1-Feb-15 0:40 
QuestionNew official WebSite Pin
Lunadix25-Jun-13 2:57
Lunadix25-Jun-13 2:57 
QuestionPublished Version 3.5.2.18 Pin
Lunadix24-Jun-13 3:14
Lunadix24-Jun-13 3:14 
QuestionPublished version 3.3.0.46 Pin
Lunadix24-Jul-12 23:51
Lunadix24-Jul-12 23:51 
QuestionMSSQL - Database diagrams Pin
Homebunny21-May-12 1:23
Homebunny21-May-12 1:23 
AnswerRe: MSSQL - Database diagrams Pin
Lunadix31-May-12 4:58
Lunadix31-May-12 4:58 
QuestionGreat code !!! Pin
mpino15-Sep-11 5:00
mpino15-Sep-11 5:00 
AnswerRe: Great code !!! Pin
Lunadix31-May-12 4:58
Lunadix31-May-12 4:58 
GeneralGood code. Pin
Luis Oliveira 19669-Sep-11 19:28
Luis Oliveira 19669-Sep-11 19:28 
GeneralRe: Good code. Pin
Lunadix17-Oct-11 2:03
Lunadix17-Oct-11 2:03 
AnswerRe: Good code. Pin
thatraja26-Jan-12 20:32
professionalthatraja26-Jan-12 20:32 
QuestionLuna 3.0.12.18 published Pin
Lunadix7-Sep-11 23:30
Lunadix7-Sep-11 23:30 
GeneralEffective Code Generator Pin
Muhammad Adnan Umar5-May-11 3:04
Muhammad Adnan Umar5-May-11 3:04 
GeneralRe: Effective Code Generator Pin
Lunadix19-May-11 2:52
Lunadix19-May-11 2:52 
GeneralNice Code Generator Pin
ipadilla22-Apr-11 22:58
ipadilla22-Apr-11 22:58 
GeneralRe: Nice Code Generator Pin
Lunadix22-Apr-11 23:47
Lunadix22-Apr-11 23:47 
thanx. for update check at http://www.diegolunadei.it

i will write on my blog when i publish a newer version.
Generalsource code? Pin
mindserve21-Apr-11 11:41
mindserve21-Apr-11 11:41 
GeneralRe: source code? Pin
Lunadix22-Apr-11 2:51
Lunadix22-Apr-11 2:51 
GeneralRe: source code? Pin
mindserve22-Apr-11 2:55
mindserve22-Apr-11 2:55 
GeneralRe: source code? Pin
Lunadix22-Apr-11 22:06
Lunadix22-Apr-11 22:06 
GeneralUpdate Pin
Lunadix20-Apr-11 5:23
Lunadix20-Apr-11 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.