开发者

Page inheritance in mixed asp.net Forms and MVC application

开发者 https://www.devze.com 2023-01-01 11:37 出处:网络
I\'m working on a web application.One of my co-workers has written some asp.net forms pages.The page classes all inherit from BasePageClass, which of course inherits from the Page class.I wish to add

I'm working on a web application. One of my co-workers has written some asp.net forms pages. The page classes all inherit from BasePageClass, which of course inherits from the Page class. I wish to add some MVC controllers that I've been told need to use the same logic implemented in the BasePageClass. Ordinarily, I would want to inherit the functions in the BasePageClass in the controller classes, but this breaks the inheri开发者_开发知识库tance hierarchy.

What is the best practice for solving this problem?


If there is common functionality, I would suspect that this functionality should be separated out from the page/controller anyway. This is better OOP/D. Then both your controller base page and your System.Web.UI.Page base can both have properties returning classes that contain this common functionality.

(I've seen a lot of cases where stuff is crammed into the base page that should be elsewhere. Your need for this functionality to be in both the Pages and Controllers is likely just bringing this poor design to light, rather than being a problem in itself.)

Less likely to be what you want, but still a possibility:

  • You could also put your common functionality in a series of overloaded extension methods, where the first parameter is (a) System.Web.UI.Page and on the other overload (b) System.Web.Mvc.Controller.
  • Both your base page and base controller could implement a common interface, wrapping functionality stored in a common place.
  • Pull this functionality out altogether and do it as needed in your controler-logic and code-behinds.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号