locked
Base PageModel in Razor Pages. RRS feed

  • Question

  • User-1042970710 posted

    Hi Experts,

    I am using ASP.NET Core 3.1 Razor pages to build my application.

    Can anyone give me a working example on how I can make base PageModel class which  implements all my DI  (Configuration, Session, Cookies DB)  etc. in one single place rather then keep instantiating and injecting them in the every page's  class constructor.

    I remember when I was using ASP.NET 5  I was making base controller  and the later inherit all my newly  controllers from the base controller.

    Regards;

    Thursday, November 19, 2020 9:20 AM

Answers

  • User-821857111 posted

    Not sure how a BasePageModel will help much. You still have to inject dependencies into each derived page's constructor, whether you need it in that page or not.

    In any event, Razor Pages only effectively cater for two "actions", so their individual dependencies should be a lot less than old style MVC controllers that might cater for a lot of actions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2020 11:59 AM
  • User-474980206 posted

    C# does not support inheriting constructors, so you cannot use a base class to get DI the normal way. A constructor can call a base constructor.

    public Mypage : BasePage
    {
          public MyPage (IServiceProvider services) : base (services) {}
    }
    

    Then the base class can use services to fetch the DI objects.

    note: I think this is poor design and would reject in a code review.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2020 3:37 PM
  • User1120430333 posted

    Can anyone give me a working example on how I can make base PageModel class which  implements all my DI  (Configuration, Session, Cookies DB)  etc. in one single place rather then keep instantiating and injecting them in the every page's  class constructor.

    It's a very poor design, and it would surly not pass any code review.

    You should learn how to write loosely coupled clean code.

    https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection

    https://docs.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/architectural-principles

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2020 6:04 PM

All replies

  • User-821857111 posted

    Not sure how a BasePageModel will help much. You still have to inject dependencies into each derived page's constructor, whether you need it in that page or not.

    In any event, Razor Pages only effectively cater for two "actions", so their individual dependencies should be a lot less than old style MVC controllers that might cater for a lot of actions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2020 11:59 AM
  • User-474980206 posted

    C# does not support inheriting constructors, so you cannot use a base class to get DI the normal way. A constructor can call a base constructor.

    public Mypage : BasePage
    {
          public MyPage (IServiceProvider services) : base (services) {}
    }
    

    Then the base class can use services to fetch the DI objects.

    note: I think this is poor design and would reject in a code review.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2020 3:37 PM
  • User1120430333 posted

    Can anyone give me a working example on how I can make base PageModel class which  implements all my DI  (Configuration, Session, Cookies DB)  etc. in one single place rather then keep instantiating and injecting them in the every page's  class constructor.

    It's a very poor design, and it would surly not pass any code review.

    You should learn how to write loosely coupled clean code.

    https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection

    https://docs.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/architectural-principles

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 19, 2020 6:04 PM