public void GenerateModelCode(ViewModel model, DirectoryInfo saveDir) { //TODO: Make code more modular, add more comments CodeCompileUnit compileUnit = new CodeCompileUnit(); CodeNamespace globalNamespace = new CodeNamespace(); globalNamespace.Imports.AddRange( configuration.Namespaces.Select(x => new CodeNamespaceImport(x)).ToArray()); //Add global using statements compileUnit.Namespaces.Add(globalNamespace); CodeTypeDeclaration modelClass = new CodeTypeDeclaration(model.Name); // Sets the member attributes for the type to public modelClass.Attributes = MemberAttributes.Public; // Set the base class which the model inherits from modelClass.BaseTypes.Add(model.BaseClass.Name); //Add the View Model Attribute modelClass.CustomAttributes.Add(new CodeAttributeDeclaration("ViewModel", new CodeAttributeArgument(new CodePrimitiveExpression(model.SchemaName)), //Schema Name new CodeAttributeArgument(new CodePrimitiveExpression(true)) //Is Default )); foreach (var field in model.FieldProperties) { if (configuration.FieldAttributeTypes.ContainsKey(field.FieldType)) { CodeAttributeDeclaration fieldAttribute = new CodeAttributeDeclaration( configuration.FieldAttributeTypes[field.FieldType].Name, //Field Attribute Name new CodeAttributeArgument(new CodePrimitiveExpression(field.FieldName))); //Schema Field Name if (field.FieldType == FieldType.Linked) { //Field is a Linked Component, we need to assemble the list of possible Types var modelTypes = new List<CodeExpression>(); //Populate list of Types foreach (var modelName in field.LinkedComponentTypeNames) { modelTypes.Add(new CodeTypeOfExpression(modelName)); } if (modelTypes.Count > 0) { //Create Array of Types and pass in the list of Types CodeArrayCreateExpression codeArrayCreate = new CodeArrayCreateExpression("Type", modelTypes.ToArray()); //Add the array of Types to the Attribute arguments fieldAttribute.Arguments.Add( new CodeAttributeArgument(configuration.LinkedComponentTypesAttributeParameterName, codeArrayCreate)); } } else if (field.FieldType == FieldType.Embedded && !string.IsNullOrEmpty(field.EmbeddedTypeName)) { //Add the required Type argument for embedded fields fieldAttribute.Arguments.Add( new CodeAttributeArgument(new CodeTypeOfExpression(field.EmbeddedTypeName))); } if (field.IsMultiValue) { //Add boolean value for multiple values fieldAttribute.Arguments.Add(new CodeAttributeArgument("AllowMultipleValues", new CodePrimitiveExpression(true))); } if (field.IsMetadata) { //Add boolean for metadata fields fieldAttribute.Arguments.Add(new CodeAttributeArgument("IsMetadata", new CodePrimitiveExpression(true))); } //Create and populate the model Property CodeMemberField property = new CodeMemberField { Name = field.PropertyName }; //Use CodeMemberField as a hack to add a blank getter/setter property.Attributes = MemberAttributes.Public; property.Type = GetPropertyType(model.BaseClass, field); property.CustomAttributes.Add(fieldAttribute); //Add the Field Attribute property.Name += " { get; set; }"; //Hack to add empty get/set modelClass.Members.Add(property); } } if (configuration.ModelAttributeTypes.ContainsKey(model.ModelType)) { var attrs = configuration.ModelAttributeTypes[model.ModelType]; //loop through all the "extra" model properties in the config that don't necessarily have a direct link to the Schema i.e. multimedia foreach (var attr in attrs) { CodeAttributeDeclaration modelAttribute = new CodeAttributeDeclaration(attr.Name); //Field Attribute Name CodeMemberField property = new CodeMemberField { Name = attr.DefaultPropertyName }; //Use CodeMemberField as a hack to add a blank getter/setter property.Attributes = MemberAttributes.Public; property.Type = new CodeTypeReference(attr.ReturnTypeName); property.CustomAttributes.Add(modelAttribute); //Add the Field Attribute property.Name += " { get; set; }"; //Hack to add empty get/set modelClass.Members.Add(property); } } var ns = new CodeNamespace(namespaceName); ns.Types.Add(modelClass); compileUnit.Namespaces.Add(ns); if (!saveDir.Exists) saveDir.Create(); DirectoryInfo dir = new DirectoryInfo(Path.Combine(saveDir.FullName, model.ContainingFolder)); if (!dir.Exists) dir.Create(); string filePath = Path.Combine(dir.FullName, model.Name + ".cs"); CreateFile(filePath, compileUnit); }
private ViewModel CoreCreateModel(SchemaData schema, SessionAwareCoreServiceClient client) { Console.WriteLine("\nTitle:" + schema.Title + ", Schema Purpose: " + schema.Purpose); IList<FieldProperty> modelProperties = new List<FieldProperty>(); ViewModel model = new ViewModel(); switch (schema.Purpose) { case SchemaPurpose.Embedded: model.BaseClass = typeof(ViewModelBase); model.ModelType = ModelType.EmbeddedSchemaFields; break; case SchemaPurpose.Component: model.BaseClass = typeof(ViewModelBase); model.ModelType = ModelType.ComponentPresentation; break; case SchemaPurpose.Metadata: return null; case SchemaPurpose.Multimedia: model.BaseClass = typeof(ViewModelBase); model.ModelType = ModelType.MultimediaComponent; break; case SchemaPurpose.Protocol: return null; case SchemaPurpose.TemplateParameters: return null; case SchemaPurpose.UnknownByClient: return null; case SchemaPurpose.VirtualFolderType: return null; case SchemaPurpose.Bundle: return null; default: break; } SchemaFieldsData fields = client.ReadSchemaFields(schema.Id, false, expandedOptions); if (fields.Fields != null) { ProcessFields(fields.Fields, false, ref modelProperties); } if (fields.MetadataFields != null) { ProcessFields(fields.MetadataFields, true, ref modelProperties); } model.Name = schema.Title.ResolveModelName(); model.SchemaName = schema.Title; model.FieldProperties = modelProperties; model.ContainingFolder = schema.LocationInfo.OrganizationalItem.Title.ResolveModelName(); return model; }