private void WriteEntityDTOAdapterCollection(IEntity entity)
        {
            string baseClass = String.Format("{0}<{1}",
                                             entity.Persistence.Persisted ? ClassName_PersistentDTOAdapterCollection : ClassName_DTOAdapterCollection,
                                             NamingHelperStack.ToDTOAdapterTypeName(entity, environment));

            if (entity.Persistence.Persisted)
            {
                baseClass += ", " + NamingHelper.ToDTOTypeName(entity, dtoEnvironment) + ">";
            }
            else
            {
                baseClass += ">";
            }

            cw.BeginClass(AccessLevel.Public,
                          true,
                          NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
                          baseClass);

            cw.BeginRegion("Constructors");
            cw.BeginFunction("public {0} ()",
                             NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null));
            cw.EndFunction();
            cw.BeginFunction("public {0} ({1} {2})",
                             NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
                             NamingHelperStack.ToDTOCollectionTypeName(entity, dtoEnvironment),
                             VarName_DTOCollection);
            cw.WriteLine("foreach ({0} {1} in {2})",
                         NamingHelper.ToDTOTypeName(entity, dtoEnvironment),
                         VarName_DTO,
                         VarName_DTOCollection);
            cw.BeginScope();
            cw.WriteLine("this.InternalAdd(new {0}({1}));",
                         NamingHelperStack.ToDTOAdapterTypeName(entity, environment),
                         VarName_DTO);
            cw.EndScope();
            cw.EndFunction();
            cw.EndRegion();
            cw.WriteLine();

            // Get page
//			cw.BeginFunction("public static {0} {1}(int pageNum = 0, int pageSize = 20)",
//			                 NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
//			                 MethodName_GetPage);
//		    cw.WriteLine("return null;");
//			cw.EndFunction();
//		    cw.WriteLine();

            // Get by query
            cw.BeginFunction("public static {0} {1}(string query, {2}.{3} queryParams, int pageNum = 0, int pageSize = {4})",
                             NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
                             MethodName_GetByQuery,
                             interfacesEnvironment.BaseNamespace,
                             NamingHelper.ClassName_ServicesQueryParams,
                             RestServiceHelper.MaxPageSize);
            cw.WriteLine("{0} request = new {0}();", NamingHelperStack.ToServiceRequestName(entity, interfacesEnvironment));
            cw.WriteLine("request.{0} = query;", NamingHelperStack.ParamName_Query);
            cw.WriteLine("request.{0} = queryParams;", NamingHelperStack.ParamName_QueryParams);
            cw.WriteLine("request.{0} = pageNum;", NamingHelperStack.ParamName_PageNumber);
            cw.WriteLine("request.{0} = pageSize;", NamingHelperStack.ParamName_PageSize);
            cw.WriteLine("{0} {1} =",
                         NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                         VarName_Response);
            cw.Indent++;
            cw.WriteLine("WebClientFactory.GetJsonClient()");
            cw.WriteLine(".Post<{0}>(\"/{1}\", request);",
                         NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                         NamingHelperStack.ToServiceName(entity, null));
            cw.Indent--;
            cw.WriteLine("WebClientFactory.CheckResponseStatus({0}.ResponseStatus);",
                         VarName_Response);
            cw.WriteLine("return new {0}({1}.{2});",
                         NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
                         VarName_Response,
                         NamingHelperStack.ToDTOCollectionPropertyName(entity));
            cw.EndFunction();
            cw.WriteLine();

            // Get by relations
            foreach (IRelation r in entity.Parents)
            {
                cw.BeginFunction("public static {0} {1}",
                                 NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
                                 ServicesLayerConfig.Methods.GetByRelationParent(r, interfacesEnvironment).Signature);
                cw.WriteLine("{0} request = new {0}();", NamingHelperStack.ToServiceRequestName(entity, interfacesEnvironment));
                cw.WriteLine("{0} {1} = WebClientFactory.GetJsonClient()",
                             NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                             VarName_Response);
                cw.Indent++;
                cw.WriteLine(".Get<{0}>(String.Format(\"/{1}/{2}\", {3}));",
                             NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                             NamingHelperStack.ToServiceName(entity, null),
                             cw.ToSeparatedString(r.ChildAttributes.ToList(),
                                                  "/",
                                                  delegate(object item, int count)
                                                  { return(String.Format("{0}/{{{1}}}", (item as IAttribute).Name, count)); }),
                             environment.ToArguments(r.ChildAttributes)
                             );
                cw.Indent--;
                cw.WriteLine("WebClientFactory.CheckResponseStatus({0}.ResponseStatus);",
                             VarName_Response);
                cw.WriteLine("return new {0}({1}.{2});",
                             NamingHelperStack.ToDTOAdapterCollectionTypeName(entity, null),
                             VarName_Response,
                             NamingHelperStack.ToDTOCollectionPropertyName(entity));
                cw.EndFunction();
                cw.WriteLine();
            }

            cw.EndClass();
        }