Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Luna Data Layer Code Generator for VB.NET

, 9 Sep 2011
Rate this:
Please Sign up or sign in to vote.
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:

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

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";

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

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.

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:

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

'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

'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)

Share

About the Author

Lunadix

Italy Italy
No Biography provided

Comments and Discussions

 
QuestionNew official WebSite PinmemberLunadix25-Jun-13 2:57 
QuestionPublished Version 3.5.2.18 PinmemberLunadix24-Jun-13 3:14 
QuestionPublished version 3.3.0.46 PinmemberLunadix24-Jul-12 23:51 
QuestionMSSQL - Database diagrams PinmemberHomebunny21-May-12 1:23 
AnswerRe: MSSQL - Database diagrams PinmemberLunadix31-May-12 4:58 
QuestionGreat code !!! Pinmembermpino15-Sep-11 5:00 
AnswerRe: Great code !!! PinmemberLunadix31-May-12 4:58 
GeneralGood code. PinmemberLus Oliveira9-Sep-11 19:28 
GeneralRe: Good code. PinmemberLunadix17-Oct-11 2:03 
AnswerRe: Good code. Pinmvpthatraja26-Jan-12 20:32 
QuestionLuna 3.0.12.18 published PinmemberLunadix7-Sep-11 23:30 
GeneralEffective Code Generator PinmemberMuhammad Adnan Umar5-May-11 3:04 
GeneralRe: Effective Code Generator PinmemberMember 330085519-May-11 2:52 
GeneralNice Code Generator Pinmemberipadilla22-Apr-11 22:58 
GeneralRe: Nice Code Generator PinmemberMember 330085522-Apr-11 23:47 
Generalsource code? Pinmembermindserve21-Apr-11 11:41 
GeneralRe: source code? PinmemberMember 330085522-Apr-11 2:51 
GeneralRe: source code? Pinmembermindserve22-Apr-11 2:55 
Thank you!
It's a great tool and worked well on Access 2003.
GeneralRe: source code? PinmemberMember 330085522-Apr-11 22:06 
GeneralUpdate PinmemberMember 330085520-Apr-11 5:23 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 9 Sep 2011
Article Copyright 2011 by Lunadix
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid