RSCG – RSCG_Templating

RSCG – RSCG_Templating
 
 

name RSCG_Templating
nuget https://www.nuget.org/packages/RSCG_Templating/
https://www.nuget.org/packages/RSCG_TemplatingCommon
link https://github.com/ignatandrei/rscg_templating/
author Andrei Ignat

Templating every your data ( starting with class)

 

This is how you can use RSCG_Templating .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<AdditionalFiles Include="ClassTypeName.txt" />
		<AdditionalFiles Include="ClassPropByName.txt" />
	</ItemGroup>



	<ItemGroup>
    <PackageReference Include="RSCG_Templating" Version="2023.1007.724" OutputItemType="Analyzer"  ReferenceOutputAssembly="false"   />
    <PackageReference Include="RSCG_TemplatingCommon" Version="2023.1007.724" />
  </ItemGroup>


	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

</Project>


The code that you will use is


using RSCG_TemplatingDemo;

var x = new Person();
Console.WriteLine("The generated string type is " + x.MyTypeName);
x.FirstName = "Andrei";
//set last name via prop
x.SetPropValue(ePerson_Properties.LastName, "Ignat");
Console.WriteLine("called directly first name : " + x.FirstName);
Console.WriteLine("called via enum of prop first name : " + x.GetPropValue(ePerson_Properties.FirstName));
Console.WriteLine("called get property :" + x.GetPropValue(ePerson_Properties.Name));

Console.WriteLine("this will throw error because Name has not set ");
try
{
    x.SetPropValue(ePerson_Properties.Name, "asd");
}
catch (Exception)
{
    Console.WriteLine("this is good!");
}
Console.ReadLine();


using RSCG_TemplatingCommon;

namespace RSCG_TemplatingDemo;

[IGenerateDataFromClass("ClassTypeName")]
[IGenerateDataFromClass("ClassPropByName")]
public partial class Person
{
    public string Name { get { return FullName(" "); } }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public string FullName(string separator = " ")
    {
        return FirstName + separator + LastName;
    }
    public void DisplayNameOnConsole()
    {
        Console.WriteLine(FullName());
    }
    public async Task<string> GetName()
    {
        await Task.Delay(1000);
        return FirstName ?? "";
    }
    public Task<string> GetFullName()
    {
        return Task.FromResult(FullName());
    }
    public Task SaveId(int id)
    {
        if (id < 0)
        {
            throw new ArgumentException("this is an error because is <0 ");
        }
        return Task.CompletedTask;
    }
}


 

The code that is generated is

//autogenerated by RSCG_Templating version 2023.1007.724.0 from file Microsoft.CodeAnalysis.AdditionalTextFile
namespace RSCG_TemplatingDemo {
	public enum ePerson_Properties {
		None = 0,
		
				Name,
				
				FirstName,
				
				LastName,
				
	} 
	partial class Person {

		public object GetPropValue(ePerson_Properties prop){
			switch(prop){
				
									case ePerson_Properties.Name:
									
											return this.Name;
										
								
									case ePerson_Properties.FirstName:
									
											return this.FirstName;
										
								
									case ePerson_Properties.LastName:
									
											return this.LastName;
										
								
				default:
						throw new NotImplementedException();
			}
		}
		public void SetPropValue<T>(ePerson_Properties prop , T value){
			switch(prop){
				
									case ePerson_Properties.Name:
									
											throw new NotImplementedException();
										
								
									case ePerson_Properties.FirstName:
									
											this.FirstName = (string?)(dynamic)value;
											break;
										
								
									case ePerson_Properties.LastName:
									
											this.LastName = (string?)(dynamic)value;
											break;
										
								
				default:
						throw new NotImplementedException();
			}
		}
	}//end class

}//end namespace
//autogenerated by RSCG_Templating version 2023.1007.724.0 from file Microsoft.CodeAnalysis.AdditionalTextFile
namespace RSCG_TemplatingDemo {
	 
	partial class Person {
		public string MyTypeName = "RSCG_TemplatingDemo.Person";		
	}//end class

}//end namespace

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_Templating