BkDebugHooks
BkDebugHooks is a static helper class that allows developers to inspect and debug important objects at different stages of the application lifecycle. It is especially useful during development when you want to "look inside" the framework without modifying any internal code.
Using BkDebugHooks
, you can attach your own custom logic to specific points like:
- Before an entity is saved to the database
- Before a form is submitted
- When a Mediator request or response is handled
- When validation results are produced
- When a component is initialized
These hooks work by assigning a delegate (a small piece of code) to the corresponding property in BkDebugHooks
.
If the delegate is not null, the framework will automatically call it when the event occurs.
When to Use BkDebugHooks
You can use BkDebugHooks to:
- Debug unexpected data before saving to the database
- Inspect form values just before they are submitted
- Track request and response flow when using Mediator
- See which validation errors are triggered
- Understand how your components are being initialized
This is especially useful if you’re learning how the framework works or trying to debug a specific issue.
How to Enable BkDebugHooks
You should only enable these hooks in Development mode.
A common place to do this is in Program.cs
:
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
if(app.Environment.IsDevelopment())
{
BkDebugHooks.InspectEntityBeforeSaveChanges=entity=>
{
Console.WriteLine("Entity before save: "+entity.GetType().Name);
Debugger.Break();// Optional: pause the debugger
};
BkDebugHooks.InspectBkComponentBase=component=>
{
Console.WriteLine("Component initialized: "+component.GetType().Name);
};
}
}
}
You can assign multiple hooks depending on what you want to debug. All hooks are optional, and if a hook is not set, nothing will happen.
Hook List and Description
Hook | Description |
---|---|
InspectEntityBeforeSaveChanges |
Called before an entity is saved to the database. Useful to inspect values, IDs, relationships, etc. |
InspectEditFormModelBeforeSubmit |
Called just before a form model is submitted. Allows inspecting user input before validation or server call. |
InspectMediatorRequest |
Called when a request (command or query) is sent to Mediator. Helps track what requests are being made. |
InspectMediatorResult |
Called when a result is returned from a Mediator handler. Allows viewing output values. |
InspectValidationResult |
Called when validation results are produced (e.g., during form submission). Shows which fields failed validation. |
InspectBkComponentBase |
Called whenever a component that inherits from BkComponentBase is initialized. Perfect for inspecting parameters and state. |
Using Each Hook: Practical Examples
1. InspectEntityBeforeSaveChanges
This hook is triggered just before an entity is saved to the database. You can use it to inspect or debug entity values.
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
BkDebugHooks.InspectEntityBeforeSaveChanges=entity=>
{
Console.WriteLine("Saving entity of type: "+entity.GetType().Name);
Console.WriteLine(JsonSerializer.Serialize(entity,newJsonSerializerOptions{WriteIndented=true}));
};
}
}
2. InspectEditFormModelBeforeSubmit
This hook lets you see the values of a form model just before the form is submitted.
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
BkDebugHooks.InspectEditFormModelBeforeSubmit=model=>
{
Console.WriteLine("Form model submitted:");
Console.WriteLine(JsonSerializer.Serialize(model));
};
}
}
3. InspectMediatorRequest
Use this to monitor all requests sent to the Mediator (commands and queries).
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
BkDebugHooks.InspectMediatorRequest=request=>
{
Console.WriteLine("Mediator request: "+request.GetType().Name);
};
}
}
4. InspectMediatorResult
This hook fires when the Mediator returns a result.
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
BkDebugHooks.InspectMediatorResult=result=>
{
Console.WriteLine("Mediator result:");
Console.WriteLine(JsonSerializer.Serialize(result));
};
}
}
5. InspectValidationResult
Inspect validation errors and results — useful when trying to understand why a form fails.
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
BkDebugHooks.InspectValidationResult=result=>
{
Console.WriteLine("Validation result:");
Console.WriteLine(JsonSerializer.Serialize(result));
};
}
}
6. InspectBkComponentBase
Use this to debug any component inheriting from BkComponentBase
. You can even target a specific one:
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
BkDebugHooks.InspectBkComponentBase=component=>
{
if(componentisEmployeeGridView)
{
Console.WriteLine("Inspecting EmployeeGridView...");
Debugger.Break();// Pause the debugger here
}
};
}
}
Using BkDebugHooks in Your Own Code
BkDebugHooks is not limited to the framework internals — you can also use it in your own components, services, or business logic whenever you want to provide extra inspection or debug points during development.
For example, if you're writing a custom service and want to expose a debug hook, you can safely trigger it like this:
BkDebugHooks.ExecuteDebugHook(BkDebugHooks.InspectMediatorResult, result);
Or, for a cleaner syntax, you can use the TryInspect()
extension method:
result.TryInspect(BkDebugHooks.InspectMediatorResult);
This pattern allows you to keep your code clean while still offering powerful development-time diagnostics.
You can add BkDebugHooks
calls in custom logic, filters, event handlers, or any part of your application
where you think inspection might help during development.
Inside your own classes, it's completely safe to invoke a debug hook. If no delegate is set, nothing happens. If an exception occurs during debug logic, it's caught and logged — your app won't crash.
This makes BkDebugHooks
a great way to make your own code "debug-aware" without adding logging or tracing in production.
myComponent.TryInspect(BkDebugHooks.InspectBkComponentBase);
This is exactly the same as calling BkDebugHooks.ExecuteDebugHook(...)
, but shorter and easier to read.
It also automatically catches and logs any exceptions without crashing your app.
Best Practices and Tips
- Only assign these hooks in
Development
environment. - Wrap hooks with type checks (e.g.,
if (obj is MyType)
) to avoid noise. - Use
Debugger.Break()
orConsole.WriteLine()
for simple inspection. - Do not leave these hooks enabled in production!
- Use JSON serialization to view deep object structures.
- Keep all hook assignments grouped together for better maintainability.
Need More?
This is just the beginning. You can extend BkDebugHooks
anytime by adding more hooks for other lifecycle events, like:
- Component disposal
- Before/after HTTP requests
- Custom validation rules
- Event handler tracing
If you build something cool with BkDebugHooks
, share it with the community — we'd love to see how you're using it!
API Reference
The table below summarizes all available hooks in BkDebugHooks
and their purpose:
Property | Type | Description |
---|---|---|
InspectEntityBeforeSaveChanges |
Action<object>? |
Triggered before saving an entity. Use to inspect or log database entities. |
InspectEditFormModelBeforeSubmit |
Action<object>? |
Triggered before an edit form is submitted. Inspect user input. |
InspectMediatorRequest |
Action<object>? |
Triggered before a Mediator request is processed. |
InspectMediatorResult |
Action<object>? |
Triggered after a Mediator request is handled. View response/result. |
InspectValidationResult |
Action<object>? |
Triggered after validation. See validation messages and outcomes. |
InspectBkComponentBase |
Action<object>? |
Triggered when a component inheriting BkComponentBase is initialized. |
ExecuteDebugHook(...) |
void |
Helper method to safely invoke any debug hook and handle exceptions. |
Troubleshooting
Here are solutions to common issues when using BkDebugHooks
:
- Nothing happens when I set a hook:
Make sure you're assigning the hook inProgram.cs
inside a development environment check:if (app.Environment.IsDevelopment()) { ... }
- I get a null reference or crash:
UseTryInspect()
orExecuteDebugHook(...)
which automatically handles null and exceptions. - Too much output in the console:
Add type filters:if (obj is MyFormModel) { Console.WriteLine(...); }
- Hook runs multiple times:
This may be expected, especially inSetParametersAsync()
. Add guards or checks inside your hook if needed.
Starter Setup Template
You can paste the following block directly into your Program.cs
or a startup helper class
to enable BkDebugHooks
in development:
publicclassProgram
{
publicstaticvoidMain(string[]args)
{
if(app.Environment.IsDevelopment())
{
BkDebugHooks.InspectEntityBeforeSaveChanges=entity=>
{
Console.WriteLine("Entity before save: "+entity.GetType().Name);
};
BkDebugHooks.InspectEditFormModelBeforeSubmit=model=>
{
Console.WriteLine("Form submitted with values:");
Console.WriteLine(JsonSerializer.Serialize(model));
};
BkDebugHooks.InspectBkComponentBase=component=>
{
if(component.GetType().Name.Contains("GridView"))
{
Console.WriteLine("Grid component detected: "+component.GetType().Name);
}
};
}
}
}
You can enable as many or as few hooks as you need. Each hook is completely optional and safe to remove anytime.
Conclusion
BkDebugHooks is a powerful and simple debugging tool built into the framework. It’s ideal for beginners, learners, and even advanced users who want more visibility into what’s happening behind the scenes.
With just one line of code, you can inspect forms, requests, validation, components, and more — without changing the actual logic of your application.
Debugging doesn't have to be hard — and BkDebugHooks makes it easy.