Split template output

Developer
Jan 25, 2010 at 8:05 PM

Hi Josip,

Have you ever considered generating each CSLA Business Object to its own file ?

I would find this easier to maintain in the long run.

Oleg describes how in this post:

http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/

Regards.

 

Coordinator
Jan 26, 2010 at 6:02 AM

Yes I did think about it, but did not find enough reasons to generate code in multiple files. You should not manually modify generated file, so it is same for you if there is one file per BO, or one file per T4 template. Something to think about is version control, that can be easier if there is one file per BO. Maybe this feature should be a generation option.

~Josip

Developer
Jan 26, 2010 at 11:17 AM

Josip,

Revision Control is exactly what I had in mind :-)

Also, if we have one file per BO, then it becomes easier to "attach" a partial class file to each BO as needed.

Regards

Developer
Jan 30, 2010 at 7:37 PM

Josip,

I've modified the csla.tt template to generate multiple files, one per entity.

Is there a place I can upload it ?

Regards

Developer
Jan 30, 2010 at 7:44 PM

Here's the diff patch:

 

 Index: Csla.tt
===================================================================
--- Csla.tt (revision 46111)
+++ Csla.tt (working copy)
@@ -1,6 +1,6 @@
 <#@ template language="C#" debug="true" hostspecific="true"#>
 <#@ include file="EF.Utility.CS.ttinclude"#>
-<#@ output extension=".cs"#>
+<#@ output extension=".txt"#>
 
 <#
 
@@ -31,28 +31,9 @@
 ModelNamespace = loader.GetModelNamespace(SourceCsdlPath);
 string namespaceName = userSettings.LibraryNamespace;//code.VsNamespaceSuggestion() ?? GetObjectNamespace(ModelNamespace);
 UpdateObjectNamespaceMap(namespaceName);
-#>
 
+TemplateFileManager fm = TemplateFileManager.Create(this);
 
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using <#=userSettings.DataNamespace#>;
-using Csla;
-using Csla.Data;
-
-<#
-    if (!String.IsNullOrEmpty(namespaceName))
-    {
-#>
-
-namespace <#=namespaceName#>
-{
-<#
-        PushIndent(CodeRegion.GetIndent(1));
-    }
-
  IEnumerable<AssociationType> associations = GetSourceSchemaTypes<AssociationType>().Where(i => i is AssociationType);
  foreach (AssociationType item in associations)
  {
@@ -72,7 +53,6 @@
     ////////
     //////// Write Entities
     ////////
-    region.Begin(GetResourceString("Template_RegionEntities"));
     foreach (EntityType entity in GetSourceSchemaTypes<EntityType>().OrderBy(e => e.Name))
     {
   if (entity.Documentation == null || entity.Documentation.Summary == string.Empty)
@@ -90,6 +70,36 @@
    
    if (!IsList(cslaType))
    {
+    fm.StartNewFile(entityName + ".cs");
+   }
+   else if (IsList(cslaType))
+   {
+    fm.StartNewFile(listName + ".cs");
+   }
+   //System.Diagnostics.Debugger.Break();
+   
+#>
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using <#=userSettings.DataNamespace#>;
+using Csla;
+using Csla.Data;
+
+<#
+    if (!String.IsNullOrEmpty(namespaceName))
+    {
+#>
+
+namespace <#=namespaceName#>
+{
+<#
+        PushIndent(CodeRegion.GetIndent(1));
+    }
+
+   if (!IsList(cslaType))
+   {
     
 #>
 [Serializable]
@@ -464,24 +474,29 @@
 }
 
 <#
+ if (!String.IsNullOrEmpty(namespaceName))
+    {
+        PopIndent();
+#>
+
+}
+<#
+    }
+#>
+
+
+<#
   
   }
 
   }
-   region.End();
+fm.WriteFiles();
  
 #>
 
-<#
- if (!String.IsNullOrEmpty(namespaceName))
-    {
-        PopIndent();
-#>
+File generated: <#= DateTime.Now #> by <#= Environment.UserName #>
 
-}
 <#
-    }

     VerifyTypeUniqueness();
  
 #>
@@ -562,9 +577,6 @@
 #>
 
 
-
-
-
 <#+
 
     ////////