Dependency Injection in CodeBehind Framework



This content originally appeared on DEV Community and was authored by Elanat Framework

When using the CodeBehind framework, you can create controllers with dependency injection capabilities; this can be a better testability structure than the lovely! PageLoad method.

Understanding Constructor Behavior

In CodeBehind’s modern MVC architecture, the constructor of the Controller and Model classes is independent of the CodeBehind constructor. This separation allows developers to inject custom values directly from the View layer, streamlining data flow and improving modularity.

For background, see the CodeBehind Constructor and Controller and Model Constructor articles.

MVC Flow in CodeBehind

Unlike traditional MVC frameworks, CodeBehind routes requests to the View first, not the Controller. With using CodeBehind framework, you can pass values from the context object directly into Controller and Model constructors using new syntax attributes in the View.

Example: Injecting context into Controller

View (Default.aspx)

@page
@controller MyController()
@controllerconstructor(context)
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Controller class constructor</title>
</head>
<body>
    <form method="post" action="/">
        <label for="txt_TextBox">TextBox</label>
        <input name="txt_TextBox" id="txt_TextBox" type="text" />
        <br>
        <input name="btn_Button" type="submit" value="Click to send data" />
    </form>
</body>
</html>

Here, @controllerconstructor(context) injects the entire context object into the Controller constructor.

Controller

using CodeBehind;

public partial class MyController : CodeBehindController
{
    private readonly HttpContext _context;

    public MyController(HttpContext context = null)
    {
        _context = context;
    }

    public void CodeBehindConstructor()
    {
        if (!string.IsNullOrEmpty(_context.Request.Form["btn_Button"]))
            btn_Button_Click();
    }

    private void btn_Button_Click()
    {
        string TextBoxValue = _context.Request.Form["txt_TextBox"];
        Write(TextBoxValue);
        IgnoreViewAndModel = true;
    }
}
  • The HttpContext object is injected via the constructor.
  • Form data is accessed through _context.Request.Form.
  • If the button is clicked, the value of the textbox is printed and the View is suppressed using IgnoreViewAndModel.

Note: If you plan to configure the Controller via routing or call it manually, ensure it has a parameterless constructor. That’s why context is set to null by default.

Conclusion

Possibility of dependency injection (DI) in CodeBehind Framework represents a significant enhancement, aligning the framework with modern best practices for modularity, testability, and maintainability. By allowing constructors of Controller and Model classes to accept dependencies such as HttpContext (or IFormCollection or etc) directly, DI facilitates more flexible and decoupled code, reducing tight coupling with global objects like request data. This approach simplifies unit testing, as dependencies can be easily mocked or injected, and improves the overall architecture by promoting clear separation of concerns. The framework’s support for DI through constructor parameters, combined with flexible syntax options and improved attribute detection, demonstrates a thoughtful evolution toward a more robust and developer-friendly backend framework, encouraging cleaner code organization and easier integration of services.

Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

CodeBehind in NuGet:
https://www.nuget.org/packages/CodeBehind/

CodeBehind page:
https://elanat.net/page_content/code_behind


This content originally appeared on DEV Community and was authored by Elanat Framework