/// <summary> /// Writes a constructor, that calls the base <see cref="DictionaryModel"/>'s constructor with the ItemKey /// </summary> /// <param name="model"></param> private void WriteConstructor(DictionaryModel model) { string itemKey = model.GenerateCodeItemKey(_config.UseNestedStructure); string className = itemKey.ToCodeString(true).ToPascalCase(); WriteLine($"public _{className}() : base(\"{model.GetItemKey()}\") {{}}"); }
/// <summary> /// Writes a collection of models /// </summary> /// <param name="models">The models to build</param> private void WriteDictionaries(params DictionaryModel[] models) { if (models == null) { return; } // Iterate through all models for (int i = 0; i < models.Length; i++) { DictionaryModel model = models[i]; // Write a start region directive WriteLine($"#region {model.GetItemKey()}"); // Create a list of action starting the the action to build the model itself List <Action <DictionaryModel> > parentModelActions = new List <Action <DictionaryModel> > { m => WriteContentModel(model) }; if (model.RenderParentModel(_config.UseNestedStructure)) { // Get the parent model DictionaryModel parentModel = model.GetParentModel(); // Loop through all parent models while (parentModel != null) { // Prevent access to modified closure, so creating a local variable // https://www.jetbrains.com/help/resharper/2020.1/AccessToModifiedClosure.html DictionaryModel localParentModel = parentModel; // The last added action is the child action to this action Action <DictionaryModel> childAction = parentModelActions.Last(); // Generate the class name string itemKey = localParentModel.GenerateCodeItemKey(_config.UseNestedStructure); string className = itemKey.ToCodeString(true).ToPascalCase(); // Add the action to the list parentModelActions.Add(m => WritePartialClass($"_{className}", childAction, localParentModel)); // Get the next parent model parentModel = parentModel.GetParentModel(); } } // Get the root action. Action <DictionaryModel> rootAction = parentModelActions.Last(); // Execute the root action, to start building the nested class structure rootAction(model); // Write a end region directive WriteLine("#endregion"); // If not the last model, add an empty line if (i < models.Length - 1) { _builder.AppendLine(); } } }
private void WriteContentModel(DictionaryModel model) { if (model == null) { return; } string itemKey = model.GenerateCodeItemKey(_config.UseNestedStructure); // Generate class name string className = itemKey.ToCodeString(true).ToPascalCase(); // Generate field name string fieldName = itemKey.ToCodeString(false).ToCamelCase(); // Write generated code attribute to prevent warnings WriteGeneratedCodeAttribute(); // Write the static field WriteLine($"private static _{className} _{fieldName} = new _{className}();"); // Write an empty line _builder.AppendLine(); // Write the property WriteSummary($"The dictionary item: [{model.GetItemKey()}]"); // Write generated code attribute to prevent warnings WriteGeneratedCodeAttribute(); // If the model is the root model, then add static modifier to the property. Otherwise the property is initialized in the nested class structure WriteLine($"public{(model.IsRootModel(_config.UseNestedStructure) ? " static" : "")} _{className} {className} => _{fieldName};"); // Write an empty line _builder.AppendLine(); // Write the nested DictionaryItem class WriteLine($"public partial class _{className} : {nameof(DictionaryModel)}"); WriteInBrackets(WriteConstructor, model); }