示例#1
0
 public CodeGenStackDomainServices(GenieBase genie)
     : base(genie)
 {
     outFileName = "DomainServices.cs";
     genie.Config.Macro.SetMacro("%ClassName_DomainQueryFactory%", ClassName_DomainQueryFactory);
     genie.Config.Macro.SetMacro("%ClassFullName_DomainQueryParams%", DomainLayerConfig.GetClassName_QueryParams(true));
 }
 /// <summary>
 /// Generates domain infrastructure classes, interfaces and helpers.
 /// </summary>
 /// <param name='genie'>
 /// NHibernate genie.
 /// </param>
 public CodeGenDomainSupport(NHibernateGenie genie)
     : base(genie)
 {
     outFileName = "DomainSupport.cs";
     genie.Config.Macro.SetMacro("%InterfaceName_PersistentObject%", InterfaceName_PersistentObject);
     genie.Config.Macro.SetMacro("%ClassName_CommonEntityInterceptor%", ClassName_CommonEntityInterceptor);
     // Quering
     genie.Config.Macro.SetMacro("%ClassName_QueryFactory%", ClassName_QueryFactory);
     genie.Config.Macro.SetMacro("%ClassName_DomainQueryParams%", DomainLayerConfig.GetClassName_QueryParams(false));
     genie.Config.Macro.SetMacro("%ClassName_SortOrder%", DomainLayerConfig.GetClassName_SortOrder(false));
     // Events
     genie.Config.Macro.SetMacro("%InterfaceName_OnSave%", InterfaceName_OnSave);
     genie.Config.Macro.SetMacro("%InterfaceName_OnDelete%", InterfaceName_OnDelete);
     genie.Config.Macro.SetMacro("%InterfaceName_OnFlush%", InterfaceName_OnFlush);
     genie.Config.Macro.SetMacro("%ClassName_EventHandlerBase%", ClassName_EventHandlerBase);
     genie.Config.Macro.SetMacro("%ClassName_DomainEventHandler%", ClassName_DomainEventHandler);
     // Patterns
     genie.Config.Macro.SetMacro("%ClassName_EntityAuditor%", ClassName_EntityAuditor);
     genie.Config.Macro.SetMacro("%InterfaceName_UsesRegistry%", InterfaceName_UsesRegistry);
     genie.Config.Macro.SetMacro("%IUsesRegistry_Property_Registry%", IUsesRegistry_Property_Registry);
     genie.Config.Macro.SetMacro("%InterfaceName_UsesAudit%", InterfaceName_UsesAudit);
 }
示例#3
0
        private void WriteOnGet(IEntity entity)
        {
            cw.BeginFunction("public override object OnGet({0} {1})",
                             NamingHelperStack.ToServiceRequestName(entity, interfacesEnvironment),
                             VarName_Request);
            cw.WriteLine("{0} {1} = new {0}();",
                         NamingHelperStack.ToServiceResponseName(entity, interfacesEnvironment),
                         VarName_Response);
            WriteInitResponse(VarName_Response);
            cw.WriteLine("{0} {1} = null;",
                         domainEnvironment.ToTypeName(entity, true),
                         VarName_DomainObject);
            cw.BeginTry();

            cw.If(entity.Constraints.PrimaryId.Attributes.ToNamesString(" && ", VarName_Request + ".", " != null"));      // Global
            // Request by primary id
            cw.WriteLine("{0} = {1}.{2}({3});",
                         VarName_DomainObject,
                         domainEnvironment.ToTypeName(entity, true),
                         DomainLayerConfig.Methods.GetById(entity.Constraints.PrimaryId, domainEnvironment).Name,
                         cw.ToSeparatedString(entity.Constraints.PrimaryId.Attributes.ToList(), ", ",
                                              delegate(object item, int count) {
                return(String.Format("{0}.{1}{2}",
                                     VarName_Request,
                                     (item as IAttribute).Name,
                                     environment.IsNullable(item as IAttribute) ? ".Value" : ""));
            })
                         );

            // Request by uid
            foreach (IUniqueId uid in entity.Constraints.UniqueIds)
            {
                cw.ElseIf(uid.Attributes.ToNamesString(" && ", VarName_Request + ".", " != null"));
                cw.WriteLine("{0} = {1}.{2}({3});",
                             VarName_DomainObject,
                             domainEnvironment.ToTypeName(entity, true),
                             DomainLayerConfig.Methods.GetByUniqueId(uid, domainEnvironment).Name,
                             cw.ToSeparatedString(uid.Attributes.ToList(), ", ",
                                                  delegate(object item, int count) {
                    string arg = String.Empty;
                    if ((item as IAttribute).Type is IEnumerationType)
                    {
                        arg = String.Format("({0})",
                                            domainEnvironment.ToTypeName((item as IAttribute), true));
                    }
                    arg += String.Format("{0}.{1}{2}",
                                         VarName_Request,
                                         (item as IAttribute).Name,
                                         environment.IsNullable(item as IAttribute) ? ".Value" : "");
                    return(arg);
                })
                             );
            }

            // Request by relation
            foreach (IRelation r in entity.Parents)
            {
                cw.ElseIf(r.ChildAttributes.ToNamesString(" && ", VarName_Request + ".", " != null"));
                cw.WriteLine("{0} {1} = new {0}();",
                             NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                             VarName_ResponseList);
                WriteInitResponse(VarName_ResponseList);
                cw.BeginTry();
                cw.WriteLine("IList<{0}> list = ",
                             domainEnvironment.ToTypeName(entity, true));
                cw.Indent++;
                cw.WriteLine("{0}.{1};",
                             domainEnvironment.ToTypeName(entity, true),
                             DomainLayerConfig.Methods.GetByRelationParent(r, domainEnvironment)
                             .Call(r.ChildAttributes.ToNamesString(", ", VarName_Request + ".", ".Value"))
                             );
                cw.Indent--;
                cw.WriteLine("{0}.{1} = {2}(list);",
                             VarName_ResponseList,
                             NamingHelperStack.ToDTOCollectionPropertyName(entity),
                             MethodName_DomainCollectionToDTO);
                cw.EndTry();
                WriteCatch(VarName_ResponseList);
                cw.WriteLine("return {0};", VarName_ResponseList);
            }

            cw.Else();             // Collection request TOP MaxPageSize or by page
            cw.WriteLine("{0} {1} = new {0}();",
                         NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                         VarName_ResponseList);
            WriteInitResponse(VarName_ResponseList);
            cw.BeginTry();
            cw.WriteLine("IList<{0}> list = null;",
                         domainEnvironment.ToTypeName(entity, true));
            cw.If("{0}.{1} != null && {0}.{2} != null",
                  VarName_Request,
                  NamingHelperStack.ParamName_PageNumber,
                  NamingHelperStack.ParamName_PageSize);
            cw.If("{0}.{1} != null && {0}.{2} != null",
                  VarName_Request,
                  NamingHelperStack.ParamName_SortOrderProperty,
                  NamingHelperStack.ParamName_SortOrderAsc);
            cw.WriteLine("list = {0}.GetPage({1}.{2}.Value, {1}.{3}.Value, new {4}[] {{ new {4}({1}.{5}, {1}.{6}.Value) }});",
                         domainEnvironment.ToTypeName(entity, true),
                         VarName_Request,
                         NamingHelperStack.ParamName_PageNumber,
                         NamingHelperStack.ParamName_PageSize,
                         DomainLayerConfig.GetClassName_SortOrder(true),
                         NamingHelperStack.ParamName_SortOrderProperty,
                         NamingHelperStack.ParamName_SortOrderAsc);
            cw.Else();
            cw.WriteLine("list = {0}.GetPage({1}.{2}.Value, {1}.{3}.Value, null);",
                         domainEnvironment.ToTypeName(entity, true),
                         VarName_Request,
                         NamingHelperStack.ParamName_PageNumber,
                         NamingHelperStack.ParamName_PageSize);
            cw.EndIf();
            cw.Else();
            cw.WriteLine("list = {0}.GetPage(0, {1}, null);",
                         domainEnvironment.ToTypeName(entity, true),
                         RestServiceHelper.MaxPageSize);
            cw.EndIf();
            cw.WriteLine("{0}.{1} = {2}(list);",
                         VarName_ResponseList,
                         NamingHelperStack.ToDTOCollectionPropertyName(entity),
                         MethodName_DomainCollectionToDTO);
            cw.EndTry();
            WriteCatch(VarName_ResponseList);
            cw.WriteLine("return {0};", VarName_ResponseList);

            cw.EndIf();             // Global
            cw.WriteLine();


            cw.If("{0} != null", VarName_DomainObject);
            cw.WriteLine("{0} {1} = new {0}();",
                         NamingHelperStack.ToDTOTypeName(entity, interfacesEnvironment),
                         VarName_DTO);
            cw.WriteLine("{0}.{1}({2}, {3});",
                         NamingHelperStack.ToServiceName(entity, null),
                         MethodName_DomainObjectToDTO,
                         VarName_DomainObject,
                         VarName_DTO);
            cw.WriteLine("{0}.{1} = {2};",
                         VarName_Response,
                         NamingHelperStack.ToDTOPropertyName(entity),
                         VarName_DTO);
            cw.EndIf();
            cw.EndTry();
            WriteCatch(VarName_Response);
            cw.WriteLine("return {0};", VarName_Response);

            cw.EndFunction();
        }
示例#4
0
        private void WriteUtilsFunctions(IEntity entity)
        {
            // GetById
            cw.WriteLine("public static {0}{1} {2}",
                         entity.HasSupertype ? "new " : "",
                         entity.Name,
                         DomainLayerConfig.Methods.GetById(entity.Constraints.PrimaryId, environment).Signature);
            cw.BeginScope();
            cw.WriteLine("return {0}.CreateCriteria<{1}>()",
                         Call_GetSession,
                         entity.Name);
            cw.Indent++;
            foreach (IAttribute a in entity.Constraints.PrimaryId.Attributes)
            {
                cw.WriteLine(".Add(Expression.Eq(\"{0}\", {1}))",
                             a.Name, environment.ToParamName(a));
            }
            cw.WriteLine(".UniqueResult<{0}>();", entity.Name);
            cw.Indent--;
            cw.EndScope();
            cw.WriteLine();

            // GetByUid
            foreach (IUniqueId uid in entity.Constraints.UniqueIds)
            {
                cw.WriteLine("public static {0} {1}",
                             entity.Name,
                             DomainLayerConfig.Methods.GetByUniqueId(uid, environment).Signature);
                cw.BeginScope();
                cw.WriteLine("return {0}.CreateCriteria<{1}>()",
                             Call_GetSession,
                             entity.Name);
                cw.Indent++;
                foreach (IAttribute a in uid.Attributes)
                {
                    string propertyName = a.Name;
                    if (a.IsUsedInRelations)
                    {
                        foreach (IRelation r in a.UsedInRelations)
                        {
                            if (r.ChildEntity == entity)
                            {
                                propertyName = String.Format("{0}.{1}", r.ChildName, r.ParentEntity.PrimaryId.Attributes[0].Name);
                            }
                        }
                    }

                    cw.Write(".Add(Expression.Eq(\"{0}\", ", propertyName);
                    if (a.TypeDefinition.Required || !environment.IsNullable(a))
                    {
                        cw.Write(environment.ToParamName(a));
                    }
                    else
                    {
                        cw.Write("{0}.HasValue ? {0}.Value : ({1})null",
                                 environment.ToParamName(a),
                                 environment.ToTypeName(a, true));
                    }
                    cw.WriteLine("))");
                }
                cw.WriteLine(".UniqueResult<{0}>();", entity.Name);
                cw.Indent--;
                cw.EndScope();
                cw.WriteLine();
            }

            // Get by HQL
            cw.WriteLine("public static {0}System.Collections.Generic.IList<{1}> {2}",
                         entity.HasSupertype ? "new " : "",
                         entity.Name,
                         DomainLayerConfig.Methods.GetByHQL().Signature);
            cw.BeginScope();
            cw.WriteLine("return {0}.{1}.CreateQuery(hql, hqlParams)",
                         DomainLayerConfig.QueriesNamespace,
                         CodeGenDomainSupport.ClassName_QueryFactory);
            cw.Indent++;
            cw.WriteLine(".SetFirstResult(firstResult < 0 ? 0 : firstResult)");
            cw.WriteLine(".SetMaxResults(maxResult < firstResult ? 100 : maxResult)");
            cw.WriteLine(".List<{0}>();", entity.Name);
            cw.Indent--;
            cw.EndScope();
            cw.WriteLine();

            cw.WriteLine("public static {0}System.Collections.Generic.IList<{1}> {2}",
                         entity.HasSupertype ? "new " : "",
                         entity.Name,
                         DomainLayerConfig.Methods.GetPageByHQL().Signature);
            cw.BeginScope();
            cw.WriteLine("return {0}.{1}(hql, hqlParams, page * pageSize, pageSize);",
                         entity.Name,
                         DomainLayerConfig.Methods.GetByHQL().Name);
            cw.EndScope();
            cw.WriteLine();

            // Get by SQL
            cw.WriteLine("public static {0}System.Collections.Generic.IList<{1}> {2}",
                         entity.HasSupertype ? "new " : "",
                         entity.Name,
                         DomainLayerConfig.Methods.GetBySQL().Signature);
            cw.BeginScope();
            cw.WriteLine("return {0}.CreateSQLQuery(sql)", Call_GetSession);
            cw.Indent++;
            cw.WriteLine(".AddEntity(typeof({0}))", entity.Name);
            cw.WriteLine(".SetFirstResult(firstResult < 0 ? 0 : firstResult)");
            cw.WriteLine(".SetMaxResults(maxResult < firstResult ? 100 : maxResult)");
            cw.WriteLine(".List<{0}>();", entity.Name);
            cw.Indent--;
            cw.EndScope();
            cw.WriteLine();

            cw.WriteLine("public static {0}System.Collections.Generic.IList<{1}> {2}",
                         entity.HasSupertype ? "new " : "",
                         entity.Name,
                         DomainLayerConfig.Methods.GetPageBySQL().Signature);
            cw.BeginScope();
            cw.WriteLine("return {0}.{1}(sql, page * pageSize, pageSize);",
                         entity.Name,
                         DomainLayerConfig.Methods.GetPageBySQL().Name);
            cw.EndScope();
            cw.WriteLine();

            // DeleteById
            if (!entity.HasSupertype)
            {
                cw.WriteLine("public static void {0}",
                             DomainLayerConfig.Methods.DeleteById(entity.Constraints.PrimaryId, environment).Signature);
                cw.BeginScope();
                cw.WriteLine("{0} o = {1}({2});",
                             entity.Name,
                             DomainLayerConfig.Methods.GetById(entity.Constraints.PrimaryId, environment).Name,
                             environment.ToArguments(entity.Constraints.PrimaryId.Attributes));

                cw.WriteLine("if (o != null) o.Delete();");
                cw.EndScope();
                cw.WriteLine();
            }

            // Refresh
            cw.WriteLine("public {0} void {1}",
                         entity.HasSupertype ? "override" : "virtual",
                         DomainLayerConfig.Methods.Refresh().Signature);
            cw.BeginScope();
            cw.WriteLine("{0}.Refresh(this);",
                         Call_GetSession);
            cw.EndScope();
            cw.WriteLine();

            // Criteria
            cw.WriteLine("public static {0}ICriteria CreateCriteria()",
                         entity.HasSupertype ? "new " : "");
            cw.BeginScope();
            cw.WriteLine("return {0}.CreateCriteria<{1}>();",
                         Call_GetSession,
                         entity.Name);
            cw.EndScope();
            cw.WriteLine();

            // Get by foreign keys
            foreach (IRelation r in entity.Relations)
            {
                if (r is ISubtypeRelation ||
                    r.ParentSide == RelationSide.None ||
                    !r.IsChild(entity))
                {
                    continue;
                }

                cw.WriteCommentLine("{0}", r);
                cw.WriteLine("public static System.Collections.Generic.IList<{0}> {1}",
                             entity.Name,
                             DomainLayerConfig.Methods.GetByRelationParent(r, environment).Signature);
                cw.BeginScope();
                cw.WriteLine("return CreateCriteria()");
                cw.Indent++;
                foreach (IRelationAttributeMatch am in r.AttributesMatch)
                {
                    cw.WriteLine(".Add(Expression.Eq(\"{0}.{1}\", {2}))",
                                 r.ChildName,
                                 (r.ParentSide == RelationSide.Left ? am.Attribute : am.Attribute2).Name,
                                 environment.ToParamName(r.ParentSide == RelationSide.Left ? am.Attribute2 : am.Attribute)
                                 );
                }
                cw.WriteLine(".List<{0}>();", entity.Name);
                cw.Indent--;
                cw.EndScope();
                cw.WriteLine();
            }

            // Pagination
            cw.BeginFunction("public static {0}System.Collections.Generic.IList<{1}> {2}",
                             entity.HasSupertype ? "new " : "",
                             entity.Name,
                             DomainLayerConfig.Methods.GetPage().Signature);
            cw.WriteLine("int firstResult = (pageSize < 1 ? 1 : pageSize) * (page < 0 ? 0 : page);");
            cw.WriteLine("ICriteria criteria = CreateCriteria().SetFirstResult(firstResult).SetMaxResults(pageSize);");
            cw.WriteLine("if (sortOrders != null)");
            cw.Indent++;
            cw.WriteLine("foreach({0} sort in sortOrders)",
                         DomainLayerConfig.GetClassName_SortOrder(true));
            cw.Indent++;
            cw.WriteLine("criteria = criteria.AddOrder(new Order(sort.PropertyName, sort.Ascending));");
            cw.Indent--;
            cw.Indent--;
            cw.WriteLine("return criteria.List<{0}>();", entity.Name);
            cw.EndFunction();
            cw.WriteLine();

            // IPersistentObject implementation
            if (!entity.HasSupertype)
            {
                WritePersistenceMethod("Save", "SaveOrUpdate", entity);
                WritePersistenceMethod("Delete", null, entity);
            }

            // Lambda expression queries
            cw.BeginFunction("public static System.Collections.Generic.IList<{0}> GetList(System.Linq.Expressions.Expression<Func<{0}, bool>> predicate)",
                             environment.ToTypeName(entity, false));
            cw.WriteLine("return {0}.CreateCriteria().Add(Expression.Where<{0}>(predicate)).List<{0}>();",
                         environment.ToTypeName(entity, false));
            cw.EndFunction();
            cw.WriteLine();

            cw.BeginFunction("public static {0} GetUnique(System.Linq.Expressions.Expression<Func<{0}, bool>> predicate)",
                             environment.ToTypeName(entity, false));
            cw.WriteLine("return {0}.CreateCriteria().Add(Expression.Where<{0}>(predicate)).UniqueResult<{0}>();",
                         environment.ToTypeName(entity, false));
            cw.EndFunction();
            cw.WriteLine();
        }