MorrisMoxy by Peter Morris
Nuget / site data
Details
Info
Name: MorrisMoxy
A C# mix-in code generator
Author: Peter Morris
NuGet: https://www.nuget.org/packages/Morris.Moxy/
You can find more details at https://github.com/mrpmorris/Morris.Moxy
Original Readme
Morris.Moxy
Morris.Moxy is a code mix-in code generator for Microsoft .NET
Overview
Moxy allows you to write code templates at development time, which are then processed as Roslyn code-generators in real-time, and the results mixed-in to target classes.
Goal
- Write your code patterns once.
namespace {{ moxy.Class.Namespace }}
{
partial class {{ moxy.Class.Name}}
{
public string FullName => $"{Salutation} {GivenName} {FamilyName}"
public string Salutation { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
}
}
- Moxy automatically creates a .Net attribute for each pattern, which you can then apply to multiple targets in your source code.
[PersonName]
public partial class Contact
{
}
- The Moxy Roslyn code-generator executes the code pattern to generate additional C# code
namespace MyApp
{
partial class Contact
{
public string FullName => $"{Salutation} {GivenName} {FamilyName}"
public string Salutation { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
}
}
- Moxy is FAST. Changes to the template should reflect in the code in real-time. No need to recompile C# source code between changes.
Getting started
The easiest way to get started is to read the documentation.
Which includes tutorials that are numbered in an order recommended for learning
Morris.Moxy. Each will have a README
file that explains how the tutorial was created.
Installation
You can download the latest release / pre-release NuGet packages from the official Morris.Moxy Nuget page
Release notes
See the Releases page for release history.
Licence
About
Generate C# code for classes from template using attributes
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Employee.cs
- Department.cs
- IDName.mixin
This is the CSharp Project that references MorrisMoxy
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<None Remove="mixin\IDName.mixin" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="mixin\IDName.mixin" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Morris.Moxy" Version="1.5.0" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
This is the use of MorrisMoxy in Program.cs
// See https://aka.ms/new-console-template for more information
using MorrisMoxyDemo;
var e = new Employee();
e.Name = "Andrei";
var d = new Department();
d.Name = "IT";
Console.WriteLine(e.ID);
This is the use of MorrisMoxy in Employee.cs
namespace MorrisMoxyDemo;
[IDName]
partial class Employee
{
}
This is the use of MorrisMoxy in Department.cs
namespace MorrisMoxyDemo;
[IDName]
partial class Department
{
}
This is the use of MorrisMoxy in IDName.mixin
namespace {{ moxy.Class.Namespace }}
{
partial class {{ moxy.Class.Name}}
{
public string Name {get;set;}
public long ID { get; set; }
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- IDName.MixinAttribute.Moxy.g.cs
- MorrisMoxyDemo.Department.IDName.Instance1.MixinCode.Moxy.g.cs
- MorrisMoxyDemo.Employee.IDName.Instance1.MixinCode.Moxy.g.cs
// Generated from mixin\IDName.mixin at 2024-05-11 16:36:04 UTC
namespace MorrisMoxyDemo
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
internal class IDNameAttribute : Attribute
{
}
}
// Generated at 2024-05-11 16:36:06 UTC
namespace MorrisMoxyDemo
{
partial class Department
{
public string Name {get;set;}
public long ID { get; set; }
}
}
// Generated at 2024-05-11 16:36:06 UTC
namespace MorrisMoxyDemo
{
partial class Employee
{
public string Name {get;set;}
public long ID { get; set; }
}
}
Usefull
Download Example (.NET C# )
Share MorrisMoxy
https://ignatandrei.github.io/RSCG_Examples/v2/docs/MorrisMoxy