The official Fatica Labs Blog! RSS 2.0
# Saturday, October 17, 2009

Esiste, o meglio esisteva un tool di code generation, per creare automaticamente le classi partendo dai file di mapping di NHibernate. Questo tool (hbm2net, presente in NHContrib ) è stato un po' dimenticato, per cui ho deciso di provare a riesumarlo, e di ammodernarlo un po' dandogli la possibilità di utilizzare il Text Template Transformation Toolkit (T4). Ho previsto un template interno per la semplice generazione delle classi di mapping, ma potenzialmente è possibile generare con facilità qualsiasi altro codice provvedendo un template esterno, ad esempio mascherine di UI, layer WCF etc etc.

La versione attuale è una pre-alfa, serve solo a dare un'idea, e a vedere se ci sono delle dipendenze in deploy di difficile gestione, non è ben chiaro a me se T4 sia presente  in tutte le installazioni di Visual Studio.

Se volete provare il tool  potete scaricarlo da qui. Per utilizzare il templating T4 dovete utilizzare la seguente linea di comand:

hbm2net --config=t4config.xml *.hbm.xml

è importante utilizzare il config indicato, altrimenti hbm2net defaulta sul render di NVelocity.  Verra creata una cartella generated con i file sorgenti corrisondenti agli hbm.

Potete scaricare hbm2net da qui.

Per provare al volo è incluso nello zip anche un file di mapping simple1.hbm.xml.

Fatemi avere dei feedback!

 

Saturday, October 17, 2009 3:50:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
NHibernate

# Tuesday, June 16, 2009
Here you can find an Inflector ( a strategy to singularize, pluralize case convert and much more ) very useful in code generation. Supported languages are now English and spanish.
Tuesday, June 16, 2009 9:37:00 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
Code GEneration

# Sunday, May 31, 2009

Microsoft released the Oslo May CTP, so I'm porting NHModeller to these new bits.

Porting the conole version was easy, just some error with the "null" in the AST graph, but nothing difficult. Fortunatelly NHModeller has a suite of about 100 test that really helped to catch all the problems.

The intellipad plugin is still work in progress,but finally I have something closed to the end:

 

The refactory was big, but the new plugin is simpler in code. The good news is that now is very easy to create a custom DSL editor, with squiggles and highligting. May be I will post an How to in the near future.

Sunday, May 31, 2009 12:23:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
NHModeller

# Monday, May 18, 2009
Monday, May 18, 2009 8:21:00 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
NHModeller

# Friday, May 01, 2009

When an field name conflict with some database dialect keywords, NHibernate offer a syntax in the mapping to escape the identifier so that the mapping can behave properly: quote the identifier with the "`" sign.

Unfortunately the dialect implementer is not obliged to espose a list of keywords that needs to be escaped, so I decided to put something in the NHModeller syntax to achieve the same results.

This is the sample entity:

 

NHModel{
Entity From 
{
@Select:int
@Count:int  
Another:string(30)
} in @From
}
The sign @ forces the correct escape generation in the mapping:
 
 <?xml version='1.0' ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyTest" namespace="MyTest.Entities">
<class name="From" table="`From`">
<id name="Id" column="Id" access="field.camelcase-underscore">
<generator class="native" />
</id>
<property name="Select" column="`Select`" type="System.Int32" not-null="true" />
<property name="Count" column="`Count`" type="System.Int32" not-null="true" />
<property name="Another" column="Another" type="String" not-null="true" length="30" />
</class>
</hibernate-mapping>
and will issue   the correct db script ( ie MsSql2005 ):
 
 
if exists (select * from dbo.sysobjects where id = object_id(N'[From]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [From];
create table [From] (
Id INT IDENTITY NOT NULL,
[Select] INT not null,
[Count] INT not null,
Another NVARCHAR(30) not null,
primary key (Id)
);
 
 

 

	

 

Friday, May 01, 2009 5:13:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
NHModeller

# Sunday, April 19, 2009

Well,

Some words about NHModeller. The basic idea is to have a central point where you define the entities of your model, that is not the Xml Mapping itself,neigther a databse schema nor the classes. The idea actually is not so different from modelling starting from the mapping files, but Xml is orrible to write, even with intellisense. NHModeller aim to make simpler writing a mapping, and generates the real one as one of the artifacts.

The project uses MGrammar as a parser generator for the DSL  and Intellipad as the preferred editor. Both these comes from the Microsoft "Oslo" Project, still CTP at present.

The version I have developed till now deal correctly with:

  • Native Keys ( implicit ) or any other simple key ( not composite )
  • Property Mapping
  • One-To-Many mappings
  • Many-To-One mappings
  • Many-To-Many Mappings
  • Lazy / Eager fetching
  • Subclass

 I will post here a screenshot of the actual present version, running a just for fun blog model:

 



When a model is created, we have to launch the code generation. This is achieved by a console application, and intellipad is just spawning the process after asking the parameters.

The UI is not so good, due to my inexperience both in WPF and intellipad :(

 



After the generation we can have a glance to the artifacts generated we have:

  • Classes files
  • Xml for NHibernate mapping
  • And, expecialli thanks to NHibernate SchemaExport class, the DDL for our preferred DB ( at least if it is NH compliant )

 

 



The DDL is the most interesting part, expecially for me that I hate dealing with DB schemas... Here below an example of a DDL created for the blog model for SQL Server and Oracle.

 

 



Well, there is some more works, even if the generator till now let a newbie to start using NHibernate in minutes. We just need to compile the files in an assembly ( remember to put "Compile as" "Embedd Resource" for the hbm files), and start using the model.

Further Step-- Let use SchemaUpdate to modify a running model, deal with Components, Composite Key, Indices, One-to-One mapping. 

Sunday, April 19, 2009 7:51:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
NHModeller

My Stack Overflow
Contacts

Send mail to the author(s) E-mail

Tags
profile for Felice Pollano at Stack Overflow, Q&A for professional and enthusiast programmers
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Felice Pollano
Sign In
Statistics
Total Posts: 143
This Year: 3
This Month: 0
This Week: 0
Comments: 105
This blog visits
All Content © 2012, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano