|
| 1 | +# Advanced Usage |
| 2 | + |
| 3 | +PolylineAlgorithm is designed for extensibility and integration with advanced .NET scenarios. |
| 4 | +This guide covers custom types, integrations, and best practices for power users. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Custom Coordinate and Polyline Types |
| 9 | + |
| 10 | +You can encode and decode custom coordinate or polyline representations by extending the abstract base classes: |
| 11 | + |
| 12 | +- `AbstractPolylineEncoder<TCoordinate, TPolyline>` |
| 13 | +- `AbstractPolylineDecoder<TPolyline, TCoordinate>` |
| 14 | + |
| 15 | +### Example: Custom Encoder |
| 16 | + |
| 17 | +```csharp |
| 18 | +public sealed class MyPolylineEncoder : AbstractPolylineEncoder<(double Latitude, double Longitude), string> |
| 19 | +{ |
| 20 | + public MyPolylineEncoder() : base() { } |
| 21 | + |
| 22 | + public MyPolylineEncoder(PolylineEncodingOptions options) |
| 23 | + : base(options) { } |
| 24 | + |
| 25 | + protected override double GetLatitude((double Latitude, double Longitude) coordinate) |
| 26 | + => coordinate.Latitude; |
| 27 | + |
| 28 | + protected override double GetLongitude((double Latitude, double Longitude) coordinate) |
| 29 | + => coordinate.Longitude; |
| 30 | + |
| 31 | + protected override string CreatePolyline(ReadOnlyMemory<char> polyline) |
| 32 | + => polyline.ToString(); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Example: Custom Decoder |
| 39 | + |
| 40 | +```csharp |
| 41 | +public sealed class MyPolylineDecoder : AbstractPolylineDecoder<string, (double Latitude, double Longitude)> |
| 42 | +{ |
| 43 | + public MyPolylineDecoder() : base() { } |
| 44 | + |
| 45 | + public MyPolylineDecoder(PolylineEncodingOptions options) |
| 46 | + : base(options) { } |
| 47 | + |
| 48 | + protected override (double Latitude, double Longitude) CreateCoordinate(double latitude, double longitude) |
| 49 | + => (latitude, longitude); |
| 50 | + |
| 51 | + protected override ReadOnlyMemory<char> GetReadOnlyMemory(ref string polyline) |
| 52 | + => polyline.AsMemory(); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +# Registering Custom Encoder and Decoder with `IServiceCollection` |
| 59 | + |
| 60 | +For ASP.NET Core or DI-enabled .NET applications, you can easily register your custom polyline encoder and decoder as services with `IServiceCollection` by defining an extension method. This enables constructor injection and central DI management. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Example: Register Custom Polyline Encoder/Decoder |
| 65 | + |
| 66 | +Suppose you have the following custom encoder and decoder (see [Advanced Usage](./advanced.md)): |
| 67 | + |
| 68 | +```csharp |
| 69 | +public sealed class MyPolylineEncoder : AbstractPolylineEncoder<(double Latitude, double Longitude), string> |
| 70 | +{ |
| 71 | + public MyPolylineEncoder(PolylineEncodingOptions options = null) |
| 72 | + : base(options) { } |
| 73 | + |
| 74 | + // ... override required members ... |
| 75 | +} |
| 76 | + |
| 77 | +public sealed class MyPolylineDecoder : AbstractPolylineDecoder<string, (double Latitude, double Longitude)> |
| 78 | +{ |
| 79 | + public MyPolylineDecoder(PolylineEncodingOptions options = null) |
| 80 | + : base(options) { } |
| 81 | + |
| 82 | + // ... override required members ... |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## IServiceCollection Extension Method |
| 89 | + |
| 90 | +```csharp |
| 91 | +using Microsoft.Extensions.DependencyInjection; |
| 92 | +using PolylineAlgorithm; |
| 93 | + |
| 94 | +public static class PolylineServiceCollectionExtensions |
| 95 | +{ |
| 96 | + public static IServiceCollection AddMyPolylineEncoderDecoder( |
| 97 | + this IServiceCollection services, |
| 98 | + PolylineEncodingOptions options = null) |
| 99 | + { |
| 100 | + // Register encoder and decoder as singletons (adjust lifetime as needed) |
| 101 | + services.AddSingleton<IPolylineEncoder<(double Latitude, double Longitude), string>>( |
| 102 | + _ => new MyPolylineEncoder(options)); |
| 103 | + services.AddSingleton<IPolylineDecoder<string, (double Latitude, double Longitude)>>( |
| 104 | + _ => new MyPolylineDecoder(options)); |
| 105 | + return services; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## Usage |
| 113 | + |
| 114 | +In your application startup (e.g., `Program.cs` or `Startup.cs`): |
| 115 | + |
| 116 | +```csharp |
| 117 | +using PolylineAlgorithm; |
| 118 | + |
| 119 | +var builder = WebApplication.CreateBuilder(args); |
| 120 | + |
| 121 | +builder.Services.AddMyPolylineEncoderDecoder( |
| 122 | + PolylineEncodingOptionsBuilder.Create() |
| 123 | + .WithStackAllocLimit(1024) |
| 124 | + .Build() |
| 125 | +); |
| 126 | + |
| 127 | +// Now you can inject IPolylineEncoder<(double, double), string> and IPolylineDecoder<string, (double, double)> |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Benefits |
| 133 | + |
| 134 | +- **Central DI management** for polyline components |
| 135 | +- Plug-and-play integration with ASP.NET Core and modern .NET project styles |
| 136 | +- Easily swap out or configure encoders/decoders for different environments |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +> **Tip:** |
| 141 | +> You can generalize the extension method for different encoder/decoder types or include multiple algorithms by adding extra parameters. |
| 142 | +
|
| 143 | +--- |
| 144 | + |
| 145 | +## Integration Guidance |
| 146 | + |
| 147 | +- **Batch or incremental processing:** |
| 148 | + For large datasets, control the stack allocation limit via `PolylineEncodingOptions.StackAllocLimit`. |
| 149 | +- **Thread safety:** |
| 150 | + Default encoders/decoders are stateless and thread-safe. If extending for mutable types, ensure synchronization. |
| 151 | +- **Logging:** |
| 152 | + Integrate with .NET's `ILoggerFactory` when diagnostics or audit trails are needed. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## Best Practices |
| 157 | + |
| 158 | +- Always validate input data—leverage built-in validation or extend for custom rules. |
| 159 | +- Document all public APIs using XML comments for seamless integration with the auto-generated docs. |
| 160 | +- For non-standard coordinate systems or precision, clearly specify semantics in your custom encoder/decoder. |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## More Resources |
| 165 | + |
| 166 | +- [Configuration](./configuration.md) |
| 167 | +- [FAQ](./faq.md) |
| 168 | +- [API Reference](https://petesramek.github.io/polyline-algorithm-csharp/) |
0 commit comments