Print Page | Close Window

Forcing full-qualified types in DomainModelTemplate

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1940
Printed Date: 18-May-2025 at 10:32am


Topic: Forcing full-qualified types in DomainModelTemplate
Posted By: leeatkinson
Subject: Forcing full-qualified types in DomainModelTemplate
Date Posted: 05-Jul-2010 at 8:30am
Hi

Is it possible to force DomainModelTemplate to use fully-qualified type names (e.g. 'System.String' vs 'String')?

For now, I've extended it as below, but it would be nice if the template itself had this ability. This is to avoid clashes with types already existing in one's model.

Thanks

Lee

public class MyDomainModelTemplate : DomainModelTemplate
     {
          public MyDomainModelTemplate(TextTransformation textTransformation)
               : base(textTransformation)
          {
          }
          override protected void WriteEntityManagerCtors(CsdlWrapper csdl) {
            if (!csdl.IsPrimaryEdmx) return;
      
            WriteRegion("Constructors", () => {
               var className = csdl.EntityManagerName;
               var baseClassName = "IbEm.EntityManager";
               WriteCtor(AccessType.Public, className, baseClassName, new String[] { }, new String[] { });
               WriteCtor(AccessType.Public, className, baseClassName, new[] { "System.Boolean" }, new[] { "shouldConnect" });
               WriteCtor(AccessType.Public, className, baseClassName, new[] { "System.Boolean", "System.String" }, new[] { "shouldConnect", "dataSourceExtension" });
               WriteCtor(AccessType.Public, className, baseClassName, new[] { "System.Boolean", "System.String", "IbEm.EntityServiceOption" }, new[] { "shouldConnect", "dataSourceExtension", "entityServiceOption" });
               WriteCtor(AccessType.Public, className, baseClassName, new[] { "IbEm.EntityManager" }, new[] { "entityManager" });
               WriteCtor(AccessType.Public, className, baseClassName, new[] { "IbEm.EntityManager", "System.Boolean", "System.String", "IbEm.EntityServiceOption" }, new[] { "entityManager", "shouldConnect", "dataSourceExtension", "entityServiceOption" });
               WriteEntityManagerStaticCtor(csdl);
            });
          }
          override protected void WriteEntityPathForMethod(EntityOrComplexTypeWrapper entityOrComplexType)
          {
               WriteSummary("Returns the property path for the given expression.");

               WriteXComment("<example>");
               WriteXComment("Usage:");
               WriteXComment("<code>");
               WriteXComment("   var r = Employee.PathFor(e => e.Manager.City); // returns \"Manager.City\"");
               WriteXComment("</code>");
               WriteXComment("</example>");
               var funcTypeName = FmtGenericTypeName("Func", entityOrComplexType.Name, "System.Object");
               var exprTypeName = FmtGenericTypeName("Expression", funcTypeName);
               var methDef = new MethodDef("PathFor", "System.String", AccessType.Public,
                new[] { exprTypeName }, new[] { "expr" });
               methDef.SetStatic(true);
               var entityType = entityOrComplexType as EntityTypeWrapper;
               if (entityType != null)
               {
                    methDef.SetOverload(entityType.IsDerived);
               }
               WriteCodeDef(methDef, () => WriteEntityPathForMethodCore(entityOrComplexType));
          }
          override protected void WriteEntityPropertyNamesInnerClassCore(String propertyName) {
            var fmt = (this.CodeGenLanguage.IsCSharp)
               ? "public const System.String {0} = \"{0}\";"
               : "Public Const {0} As System.String = \"{0}\"";
            WriteLine(String.Format(fmt, FmtName(propertyName)));
          }

          private String FmtName(String name) {
            return NamingService.FmtName(name);
          }



Replies:
Posted By: ting
Date Posted: 06-Jul-2010 at 8:35pm
In the .tt file, you can add one line of code that will fully qualify all system types:
 
...
var template = new MyTemplate(this);
template.NamingService.FullyQualifySystemTypes = true;  // Add this line
template.Generate();
...
 


Posted By: leeatkinson
Date Posted: 07-Jul-2010 at 1:26am
Many thanks.

However, it only seems to fully-qualify some of the system types - for example, the property types of entities.

Some aren't - for example, PathFor is still defined:

public static String PathFor(Expression<Func<Entity, Object>> expr) {
      return IbCore.PropertyPath.For<Entity>(expr);
    }


Lee


Posted By: ting
Date Posted: 07-Jul-2010 at 7:37pm

;)  Ok.  We'll get that fixed.




Print Page | Close Window