SapphireDb logo SapphireDb

Fluent configuration

SapphireDb supports the configuration of models and action handlers through a fluent API.
This method is optional and additional to the configuration through attributes but it has some advantages.
You can separate the code of the pure model class and the code for the configuration into different files for better readability for example.

Configure model

To use the fluent API you first have to create a class that implements the interface ISapphireModelConfiguration<T>.
This class contains a method where you can use the fluent API.

\f:(csharp:Message.cs) public class Message\n {\n \t[Key]\n \tpublic Guid Id { get; set; }\n\n \tpublic DateTime CreatedOn { get; set; }\n\n \tpublic string From { get; set; }\n\n \tpublic string To { get; set; }\n\n \tpublic string Content { get; set; }\n } \f:(csharp:MessageConfiguration.cs) public class MessageConfiguration : ISapphireModelConfiguration<Message>\n {\n \tpublic void Configure(SapphireModelBuilder<Message> modelBuilder)\n \t{\n \t\tmodelBuilder.SetQueryFunction(information => { return message => message.Content == "test123"; });\n \t\tmodelBuilder.AddQueryAuth("requireAdmin");\n\n \t\tmodelBuilder.AddCreateEvent(before: (message, information) =>\n \t\t{\n \t\t\tConsole.WriteLine(message.Content.ToString());\n \t\t});\n\n \t\tmodelBuilder.Property(m => m.CreatedOn).AddQueryAuth("requireAdmin");\n \t\tmodelBuilder.Property(m => m.Content).MakeNonCreatable();\n \t}\n }

Configure action handler

The configuration of an action handler pretty much equals the configuration of a model.

\f:(csharp:ExampleActions.cs) public class ExampleActions : ActionHandlerBase\n {\n \tprivate readonly ExampleDb db;\n\n \tpublic ExampleActions(ExampleDb db)\n \t{\n \tthis.db = db;\n \t}\n\n \tpublic async Task<int> AsyncTask()\n \t{\n \t\tfor (int i = 0; i <= 100; i++)\n \t\t{\n \t\t\tThread.Sleep(10);\n \t\t}\n\n \t\treturn 33;\n \t}\n\n \tpublic string MethodWithParameters(string param1, string param2)\n \t{\n \t\treturn param1 + param2;\n \t}\n\n \tpublic void NoReturn()\n \t{\n \t\tConsole.WriteLine("This is a test");\n \t}\n } \f:(csharp:ExampleActionsConfiguration.cs) public class UserActionsConfiguration : ISapphireActionConfiguration<ExampleActions>\n {\n \tpublic void Configure(SapphireActionHandlerBuilder<ExampleActions> actionHandlerBuilder)\n \t{\n \t\tactionHandlerBuilder.Action("MethodWithParameters").AddActionAuth("requireAdmin");\n \t}\n }

This website uses Google Analytics. If you want to disable it just unselect the option: