1

as I am always use db table as source for codesmith, but currently I don't have a table definition,just class definition, for example:

   public class RespObj
    {
        public string Status { get; set; }
        public string Msg { get; set; }
        public object Resp { get; set; }
        public string ErrCode { get; set; }
    }

above code using c#,not the db table,how to use codesmith to do this thing?

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
strife
  • 11
  • 2
  • There is also a Business Objects template that would generate this as well as active snippets that generate properties based on a database templates. These ship out of the box with Generator. – Blake Niemyjski Nov 01 '16 at 16:22

1 Answers1

0

This is one of the main use cases for CodeSmith. I am writing this TEMPLATE in VB, but it can be used to generate any sort of file. Codesmith can use C#, feel free to convert this to C#, fairly straight forward exercise

The first part defines some assemblies that must be referenced. The Imports then allow the template to reference those assemblies

The Property 'SourceTable' is where the magic happens. When you compile the table the first time, hit F4 and the template properties screen will appear.

Click the elipses '...' to the right of the SourceTable line and point to your DB and table. You will need to define a new DataSource , this requires a connection string to your database.

An example is

Data Source=DEVMACHINE;Initial Catalog=MyDB;Persist Security Info=True;User ID=myUserId;Password=myPassword

This will get you started. In the routine DeclarationFromColumn, you will want to declare a c# type from the SQL server type.

What has worked for me is to take a complete working class that you already have, paste it into the template, and then start looking for places where you run through all the fields.

In each of those instances, write some template code to generate that list.

All of the functions need to go in the <Script></Script> tag.

<%@ Template Language="VB"%>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>

 <%@ Import Namespace="SchemaExplorer" %>
 <%@ Import Namespace="System.Data" %>

    <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table to get the data from." %>

<% dim i as integer %>

 public class RespObj
    {
    <% for  i = 0 to  SourceTable.Columns.Count - 1  step 1 %>
            <%= DeclarationFromColumn(SourceTable.Columns(i))  %>
    <% next %>

      } 

  <script runat="template">
    function DeclarationFromColumn(clm as ColumnSchema) as string

        return "public " & clm.NativeType & " " & clm.Name & " {get; set;}"

    end function 

    </Script>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
greg
  • 1,673
  • 1
  • 17
  • 30
  • I think you have not properly read the question. It is not about generating code from database, it is generating code from a class def. I guess some sort of reflection is required to get it done. Not sure though. – Murtuza Kabul Jun 04 '18 at 06:27