Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Yeah69/MrMeeseeks.Visitor
https://github.com/Yeah69/MrMeeseeks.Visitor
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/Yeah69/MrMeeseeks.Visitor
- Owner: Yeah69
- License: mit
- Created: 2023-04-01T12:25:58.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-04-26T19:10:45.000Z (over 1 year ago)
- Last Synced: 2024-06-28T15:09:39.170Z (4 months ago)
- Language: C#
- Size: 37.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - MrMeeseeks.Visitor
- csharp-source-generators - MrMeeseeks.Visitor - ![stars](https://img.shields.io/github/stars/Yeah69/MrMeeseeks.Visitor?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/Yeah69/MrMeeseeks.Visitor?style=flat-square&cacheSeconds=86400) Generates the boilerplate code for applications of the Visitor pattern. (Source Generators / Patterns)
README
# MrMeeseeks.Visitor
If you like the Visitor pattern, but don't like to write the boilerplate code for it, then search no more. This Mr. Meeseeks will help you with that.
This is a source generator which generates the boilerplate code for the [Visitor Pattern](https://en.wikipedia.org/wiki/Visitor_pattern).
## Nuget
The easiest way to use DIE is to get it via nuget. Here is the package page:
[https://www.nuget.org/packages/MrMeeseeks.Visitor/](https://www.nuget.org/packages/MrMeeseeks.Visitor/)
Either search for `MrMeeseeks.Visitor` in the nuget manager of the IDE of your choice.
Or call the following PowerShell command:
```powershell
Install-Package MrMeeseeks.Visitor
```Alternatively, you can use `dotnet`:
```sh
dotnet add [your project] package MrMeeseeks.Visitor
```Or manually add the package reference to the target `.csproj`:
```xml
```
## Usage
The usage is very simple. You just need to declare a visitor interface, an base interface for the elements, an interface for each element and an implementation for each element.
Then you just need to add a `VisitorInterfacePair` attribute with the visitor interface and the element interface types as input.
As long as the visitor interface, the element interface and the element implementations are partial, this source generator will generate the boilerplate code for you.Here is an example of the manual effort:
```csharp
namespace MrMeeseeks.Visitor.Sample;[VisitorInterface(typeof(IElement))]
public partial interface IVisitor { }public partial interface IElement { }
public interface IElementA : IElement { }
public partial class ElementA : IElementA { }
public interface IElementB : IElement { }
public partial class ElementB : IElementB { }
public interface IElementC : IElement { }
public partial class ElementC : IElementC { }
```Here is what will be generated. Visitor interface:
```csharp
namespace MrMeeseeks.Visitor.Sample
{
partial interface IVisitor
{
void VisitIElementA(global::MrMeeseeks.Visitor.Sample.IElementA element);
void VisitIElementC(global::MrMeeseeks.Visitor.Sample.IElementC element);
void VisitIElementB(global::MrMeeseeks.Visitor.Sample.IElementB element);
}
}
```Element interface:
```csharp
namespace MrMeeseeks.Visitor.Sample
{
partial interface IElement
{
void Accept(global::MrMeeseeks.Visitor.Sample.IVisitor visitor);
}
}
```One of the element implementations:
```csharp
namespace MrMeeseeks.Visitor.Sample
{
partial class ElementA
{
public void Accept(global::MrMeeseeks.Visitor.Sample.IVisitor visitor)
{
visitor.VisitIElementA(this);
}
}
}
```