A modular framework for chaining processing links with middleware support, designed for robust .NET applications.
This package supports the llm.txt standard for easy AI/LLM integration. See llm-full.txt for comprehensive documentation.
CodeUChain C# provides a clean, async-first architecture for building processing pipelines with:
- Immutable Context: Thread-safe data passing between processing steps
- Link Interface: Pluggable processing units
- Chain Orchestration: Sequential execution with error handling
- Middleware Support: Cross-cutting concerns like logging, authentication, etc.
dotnet add package CodeUChain --version 1.0.0# Clone the repository
git clone https://github.com/codeuchain/codeuchain.git
cd codeuchain/packages/csharp
# Build the library
dotnet build CodeUChain.csproj
# Run the example
dotnet run --project examples/MathProcessingExample.csprojusing CodeUChain;
// Create a processing chain
var chain = new Chain();
// Add processing links
chain = chain.AddLink("validate", new ValidationLink());
chain = chain.AddLink("process", new ProcessingLink());
chain = chain.AddLink("save", new SaveLink());
// Add middleware
chain = chain.UseMiddleware(new LoggingMiddleware());
chain = chain.UseMiddleware(new ErrorHandlingMiddleware());
// Execute the chain
var input = Context.Create(new Dictionary<string, object>
{
["data"] = "some input"
});
var result = await chain.RunAsync(input);Immutable data container that flows through the processing chain:
// Create context
var context = Context.Create();
var contextWithData = Context.Create(new Dictionary<string, object>
{
["key"] = "value"
});
// Manipulate data
var newContext = context.Insert("newKey", "newValue");
var removedContext = context.Remove("oldKey");
// Access data
var value = context.Get<string>("key");
var hasKey = context.ContainsKey("key");Processing units that transform the context:
public class MyLink : ILink
{
public async Task<Context> CallAsync(Context context)
{
// Process the context
var data = context.Get<string>("input");
var result = ProcessData(data);
return context.Insert("output", result);
}
}Cross-cutting concerns that intercept execution:
public class LoggingMiddleware : IMiddleware
{
public Task<Context> BeforeAsync(ILink? link, Context context)
{
var linkName = link?.GetType().Name ?? "Chain";
Console.WriteLine($"Executing: {linkName}");
return Task.FromResult(context);
}
public Task<Context> AfterAsync(ILink? link, Context context)
{
var linkName = link?.GetType().Name ?? "Chain";
Console.WriteLine($"Completed: {linkName}");
return Task.FromResult(context);
}
public Task<Context> OnErrorAsync(ILink? link, Exception exception, Context context)
{
var linkName = link?.GetType().Name ?? "Chain";
Console.WriteLine($"Error in {linkName}: {exception.Message}");
return Task.FromResult(context);
}
}Orchestrator that manages link execution and middleware:
var chain = new Chain()
.AddLink("step1", new Step1Link())
.AddLink("step2", new Step2Link())
.UseMiddleware(new LoggingMiddleware());
var result = await chain.RunAsync(inputContext);CodeUChain C# emphasizes:
- Harmony: Clean interfaces and predictable behavior
- Immutability: Thread-safe data flow
- Composability: Easy combination of components
- Error Resilience: Comprehensive error handling
- Observability: Middleware-based monitoring
See the examples/ directory for complete working examples:
- MathProcessingExample: Demonstrates basic chain execution with logging middleware
- More examples coming soon...
# Run tests (when test project is properly configured)
dotnet test- Follow the established patterns from other language implementations
- Maintain immutability and async-first design
- Add comprehensive tests for new features
- Update documentation
See the main repository for licensing information.