Over a million developers have joined DZone.

Luna Data Layer Code Generator for Vb.Net

DZone's Guide to

Luna Data Layer Code Generator for Vb.Net

· ·
Free Resource


    Luna is a code generator automatic rifle in order to facilitate and to standardize the writing of the classes that are taken care of the rescue of the data of an application.

    The code comes generated in VB.Net language (the code in C# will come implemented to short) and can be used or in Desktop applications that Web.

    All it is born from the requirement to write in optimal and easy gestibile way the part more repetitive boring and than approached the data, maintaining a comprehensible standard easy. 

    No object in own plan does not have to be incorporated, and all the generated code is easy modifiable or customizzabile from the developer.


    In the first place it is necessary that the base given of the application has been studied and realized. Once fact this, the database created comes given in input to Luna  that will be taken care of the effective creation of the classes.

    The main  Luna  members are:
    • Structure
    • Interpreter
    • Converter


    Luna  has a its structure logical owner of tables, lives and relations with which mappare in memory a database existing in a standardized format.


    A specific interpreter for every tipologia of base is present given, therefore it is virtually possible to support every given type of base. The interpreter takes care itself to be connected to the database indicated, to read to the structure of tables and relations and to create in memory a logical structure that traces it 1 to 1 using the classes  Luna  field, table and relation.

  • Converter

    Once that the structure of database has been untied from the physical origin given, the converter is taken care to create the code that serves for interface completely with the base given.


    Luna  at the moment supports the database of type ACCESS (mdb and accdb) and SQL SERVEUR.  Luna  supports moreover a its format owner Luna  Data Schema (*.lds). Every time that executes a generation of a any plan, in the folder of the rows generated from  Luna  is present relative the rows lds that it allows to reopen the structure created in way svincolata from the database originates them.


    Luna  creates the code of the classes that represent every table, the code of classes DAO that are taken care of interfacciare with database the logical objects and code SQL that could serve to reply the given structure of the base in an other storage.

    We come to the practical one. Therefore we begin from a simple base containing test datum the tables Customers and Telephones connected between they and thus structured:

    Step 1
    In this shielded it is possible to select the origin given on which it will be gone to work. The option Load View allows to inside use like source for the classes not only the tables but also the eventual contained sights of the Database.

    In our example after to have selected the Database type access and chosen the rows, clicchiamo on the push-button “Load DB”; 

    Step 2
    In the successive shielded one the tables [TABLE] or eventual view [VIEW] that they have been found in the selected source given. In order to select the tables that we want to generate enough to put a sign of it dulls on the relative case. 
    Luna  supports the Pluralization, is sufficient to double change the name of the class generated making click on the relative node in the right tree. In this example for the Customers table it will come thus generated class the Customer and via. We select all, we change to the names and clcik on the push-button “Created Code”;

    What comes generated?
    In the first place  Luna  generates three classes base, LunaBaseClass from which the other created classes inherit all, LunaBaseClassEntity from which inherits all the other classes entity and LunaBaseClassDAO from which other classes DAO inherit all. Moreover it comes created a LunaSearchParameter class of which we will see the operation later on.

    After which it comes created for every table a class entity, a class partial and a class of access to data (DAO)

    Entity class
    The generated class is an empty container, than integral with the relative Partial class and this allows to approach all the methods and the properties of relative the Partial class, thus organized:

    #Region “Logic Field”
    #End Region

    #Region “Method”
    #End Region

    It comes then created a Partial Class Table that inherits from LunaBaseClassEntity and that integral with the relative Entity class, thus organized:

    #Region “Author”

    End Region 

    #Region “Database Field Map”
    (property with the already ready fields of the table for the optional serializzazione)
    End Region 

    #Region “Method”
    Read (Id) as Table

    Save (Table) as Integer (returns the id inserted in the database)

    SaveCascade (Table) as Integer (returns the id inserted in the database)

    #End Region

    #Region “Embedded Class”
    (property or lists of objects relazionate to this class)
    #End Region

    It comes at last created a TabellaDAO Class that inherits from LunaBaseClassDAO with the following methods:

    Read (Id) as Table

    Save (Table) as Integer (returns the id inserted in the database)

    SaveCascade (Table) as Integer (returns the id inserted in the database)

    Delete (Id)

    Delete (Table)

    GetAll ()

    Find ()

    ReadSerialize ()

    SaveSerialize ()

    In the specific example:
    • Customer; mapping of the Customers table inherits from LunaBaseClassEntity
    • Phone; mapping of the Phones table inherits from LunaBaseClassEntity
    • CustomerGroup; mapping of the CustomerGroups table inherits from LunaBaseClassEntity
    • CustomersDAO; methods of reading, rescue, search, list and elimination of the Customer class inherit from LunaBaseClassDAO
    • PhonesDAO; methods of reading, rescue, search, list and elimination of the Phone class inherit from LunaBaseClassDAO
    • CustomerGroupsDAO; methods of reading, rescue, search, list and elimination of the CustomerGroup class inherit from LunaBaseClassDAO

    We analyze to the several methods and generated properties:

    Classes entity

    Read (ID) - This method allows to read from db a determined object based on its Id; inside it comes istanziata its relative class DAO and carried out all the readings;
    Save () - This method is taken care of the persistence of the data, inside comes istanziata relative class DAO that carries out the rescue. It is important to know that it comes managed the effective modification, in fact will be only carried out the rescues of the new objects or the really modified objects, in order to avoid a useless use of the DB.
    SaveCascade () - This method is taken care to save the object and all the objects relative sons;

    Classes DAO

    Read, Save and SaveCascade, are the same ones of the class entity. In more besides the methods than cancellation (Delete) we have the methods Find and GetAll that are taken care of the searches and return a List (of Table). 
    At last there are the methods ReadSerialize and SaveSerialize that are taken care to read or to write on rows a copy XML of the object that comes passed to it. 

    And now that we have the code?
    Now the simpler thing is to cliccare on the Save push-button and to select a folder where  Luna  will save all the rows that have been generated.

    Then we start Visual Study and we create a project of type Application Windows Form and then we add in the same solution a project of type Library of Classes. 
    In the first place in the project Windows Form we add to the references to the projectLibrary of classes. In the projectLibrary of classes click on add existing element, we select all the rows that have been saved from  Luna  and the game is made. We directly drag or them within from Explores Resources with drag and the drop.

    IMPORTANT! You write all your logical methods or personal and the properties custom in the Entity classes, so that if you must regenerate the Database with  Luna  , you will not lose nothing of the added code.

    Connection to the db
    The LunaBaseClassDAO class from which classes DAO inherit all has three constructors of the New:
    • New () from for discounted that us it is a string of ConnectionString name in the settings of the plan with the relative string of connection and is alone taken care to create the connection (comfortable for the Web Application);
    • New (Connection) chip ax in input a type object connection;
    • New (ConnectionString) chip ax in input a ConnectionString, passing an empty string the BaseClass does not open the connection (useful for the single methods of serializzazione);
    The Connection is of Shared type therefore if in a procedure must istanziare several objects and you do not want to use the connection string will be enough to you to use a single New declaration in which specified the connection , in the others not is necessary.

    Obviously in the applications desktop or with permanent connection it will be enough to comment the calls in the New () of the LunaBaseClassDAO and to comment the declaration of the logon _Cn rendering publishes it, and all it will continue to work.

    Important! Moon is not taken care to close the connection to the Database. It works of closing comes inserted in every class DAO but it does not come called in order to leave to the developer the decision on like managing and when to close the logon.


    In order to make an insertion in our case of a Customer it will be enough to write:

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


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


    In order to make an insertion in our case of a Customer with two it telephones will be enough to write:

    Dim Cust As New Customer

    Cust.CustomerName = “Homer Simpson”
    Cust.Address = “Evergreen Terrace, 740”

    Dim Phone1 As New Phone
    Phone1.PhoneNumber = “555.123”

    Cust.ListPhone.Add (Phone1)

    Dim Phone2 As New Phone
    Phone2.PhoneNumber = “555.456”

    Cust.ListPhone.Add (Phone2)

    Dim Group As New CustomerGroup
    Group.Read (2)

    Cust.CustomerGroup = Group

    Dim IdInserted As Integer = Cust.SaveCascade
    If IdInserted = 0 Then MessageBox.Show (“Error”)

    All the methods Save and SaveCascade hardly return the inserted Id in the Database. In the example over the SaveCascade it is taken care to save the new Customer and to save the relative Phone objects.


    For it tries  Luna  creates in every class DAO two methods; 

    the GetAll method that returns a List (Of ClasseOggetto) of all the objects of that class, to which it is possible to pass a search criterion eventually. 

    The Find method instead returns a List (Of ClasseOggetto) based on or more parameters than search.

    The method expects an Array of parameters and arranges the search based on all the parameters that come passed. 
    The parameters are objects of LunaSearchParameter type, see two simple examples:

    We try all telephones of a determined Customer

    Dim Par as new LunaSearchParameter (“CustomerID”, 1)

    Dim Mgr as New TelefoniDAO ()
    Dim ListPhone as List (Of Phone) = Mgr.Find (Par)

    We try all telephones of a determined Customer that they begin for 06

    Dim Par as new LunaSearchParameter ()
    Par.FieldName = “CustomerID”
    Par.Value = 1

    Dim Par2 as new LunaSearchParameter (“PhoneNumber”, “06%”, “LIKE”)

    Dim Mgr as New TelefoniDAO ()
    Dim ListPhone as List (Of Phone) = Mgr.Find (Par, Par2)

    Classes daughters

    In every class they come created the accesses to the objects of the classes daughters. In any case draft of lazy loadings, that is that they come only carried out if effectively recalled.

    In case of relations between the classes of type 1 to 1 it comes created in every ClassePadre a property of ClasseFiglio type.

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

    Or the ClasseFiglio object that the List of ClasseFiglio objects, is loaded alone in the moment in which really it is approached the methods.

    This is only a simple example, to understand the operation of the several created methods is banal. The important thing is that it has not been necessary to write a line of codefor interfacciarsi with the base given.


    In importance order:
    • Within and distributed transactions
    • The relations between fields come only read from MS Access, are writing the part that it finds to them also from MS Sql Serveur
    • Interpreter for DB SQL Compact 4
    • To widen the information shown on the tables loaded from the Database
    • Interpreter for DB Oracle
    • Interpreter for DB Orient


    Luna  is opensource and liberations releasable and usable: 

    Code Source - http://www.diegolunadei.it/luna/lunasource.rar

    Package EXE - http://www.diegolunadei.it/luna/luna.rar


    I feel myself to only say a thing, I have not invented nothing.  Luna  is simply a tool free that can help the developer in the drawing up of code repetitive in order to avoid errors and to concur a standardization and cleansing of the code. 
    To write code manutenibile and easy to manage also from others is not a simple thing, species in great work groups. Moreover to interface with database for the operations the standard becomes a banal operation.

    For every reference, ask, suggestion or insult you can find to me here: 

    http://www.diegolunadei.it and here d.lunadei@gmail.com

    A last thing,  Luna  is a projectin continuous evolution therefore takes the documentation of this tutorial like indicative of the general operation because to maintain modernized it is a hard work. In order to see what effectively council creates you to unload the package and to execute a generation.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}