You generate DTO classes and strongly-typed command adapters. Target language is C#. Underlying database is accessed through SQL and ADO.NET.

This is simple solution for the projects which don't require disconnected model.

Idea is to use XSD file of typed DataSet as source of SQL statements and parameters info.

dalmpe-T4.png

Add plain typed DataSet to your project. To switch off default code generation, go to DataSet properties and remove 'MSDataSetGenerator' from 'Custom Tool'.

SQL statements are managed like in the typical scenario: Server Explorer, Query Builder, etc.

As ADO.NET providers, you can use:
  • System.Data.SqlClient
  • System.Data.SqlServerCe.3.5
  • System.Data.OracleClient
  • Oracle.DataAccess.Client (ODP.NET)
  • MySql.Data.MySqlClient
  • System.Data.SQLite
  • Devart.Data.PostgreSql (the only problem here is buggy DDEX)
Generated code is compatible with .NET Framework and .NET Compact Framework.

Below are examples of typed DataSet and relevant T4 template for code generation:

TermsDS.png

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="DalMpe.ttinclude" #>
<#@ include file="SQLite.Meddict.ttinclude" #>
namespace SQLite.Meddict.Term
{
<#
    PushIndent("\t");
    DalMpeCodeGenerator gen = new DalMpeCodeGenerator();
    gen.OpenConnection(dbProviderName, connectionUrl);
    try
    {
        RenderDtoClass(gen, "Term", xsd, ".GetTermsByGroupId");
        RenderQueryDtoList(gen, "Term", "CaGetTermsByGroupId", xsd,
               ".GetTermsByGroupId");
    }
    finally
    {
        gen.CloseConnection();
    }
    PopIndent();
#>
}

Resulting code looks like this:

namespace SQLite.Meddict.Term
{
	public partial class Term
	{
		public System.Int64 tId { get; set; }
		public System.String tNameSrc { get; set; }
		public System.String tNameDst { get; set; }
		public System.Byte[] tFile { get; set; }
		public System.Int64 tOrder { get; set; }
	}
	
	public partial class CaGetTermsByGroupId : IDisposable
	{
		private DbCommand cmd;
		
		public const string sql = @"SELECT terms.t_id, terms.t_name_src, 
						terms.t_name_dst, terms.t_file,
						terms.t_order
		                  FROM            terms INNER JOIN
		                  term_groups ON terms.t_id = term_groups.t_id
		                  WHERE        (term_groups.g_id = @g_id)
		                ";
		
		public CaGetTermsByGroupId(DbConnection conn)
		{
			DbProviderFactory providerFactory =
                                    DbProviderFactories.GetFactory("System.Data.SQLite");
			cmd = providerFactory.CreateCommand();
			cmd.CommandText = sql;
			cmd.CommandType = System.Data.CommandType.Text;
			cmd.Connection = conn;
			{
			    DbParameter p = providerFactory.CreateParameter();
			    p.ParameterName = "@g_id";
			    cmd.Parameters.Add(p);
			}
		}
		
		public static List<Term> Exec(DbTransaction tr, System.Int64 g_id)
		{
		    using (CaGetTermsByGroupId  ca = 
				new CaGetTermsByGroupId (tr.Connection))
		    {
		        ca.SetTransaction(tr);
		        return ca.Exec(g_id);
		    }
		}
		
		public static List<Term> Exec(DbConnection conn, System.Int64 g_id)
		{
		    using (CaGetTermsByGroupId  ca = new CaGetTermsByGroupId (conn))
		    {
		        return ca.Exec(g_id);
		    }
		}
		
		public List<Term> Exec(System.Int64 g_id)
		{
			cmd.Parameters["@g_id"].Value = g_id;

			List<Term> res = new List<Term>();
			
			DbDataReader rdr = cmd.ExecuteReader();
			
			try
			{
				while (rdr.Read())
				{
					Term p = new Term();
					p.tId = (System.Int64)rdr["t_id"];
					p.tNameSrc = (System.String)rdr["t_name_src"];
					p.tNameDst = (System.String)rdr["t_name_dst"];
					p.tFile = (System.Byte[])rdr["t_file"];
					p.tOrder = (System.Int64)rdr["t_order"];
					            
					res.Add(p);
				}
			}
			finally
			{
			    rdr.Close();
			}
			
			return res;
		}
		
		public void SetTransaction(DbTransaction tr)
		{
		    cmd.Transaction = tr;
		}
		
		public void Prepare()
		{
		    cmd.Prepare();
		}
		
		public void Dispose()
		{
		    cmd.Dispose();
		}
	}
}

There are 11 methods for code generation:
  • RenderDtoClass
  • RenderCrudDtoClass
  • RenderCrudCreate
  • RenderCrudRead
  • RenderCrudUpdate
  • RenderCrudDelete
  • RenderQuery
  • RenderQueryList
  • RenderQueryDto
  • RenderQueryDtoList
  • RenderUpdate

Download project documentation for the details:
http://sourceforge.net/projects/utils4dal/files/site.zip/download

Implementation is ported from DAL MPE for Java:
http://plugins.intellij.net/plugin/?idea&pluginId=7092
http://sourceforge.net/projects/utils4dal

Project Startup Guide.

Last edited May 1 at 7:31 AM by ss20, version 95