4

I think this is a good question for here but I'm not 100% sure. Please flag if it's too vague.

I've worked on many websites where common framework classes are overridden.

Since I work mostly in .net these days examples would include Page, Masterpage, UserControl, DbContext ect. I believe this could apply to any language though.

Sometimes I can see why it's done, I overrode entity framework's DbContext to include some logging code.

Allot of the time though I don't see why these base classes were created. Because I've inherited most of the sites I can't really ask why.

My question: Is it a good idea/practice when creating a new site to Override the common framework classes mentioned above? Would doing so be considered more of an old style?

4 Answers 4

1

No it is not a good idea. Unless you are doing a very special job you don't need overriding framework main classes. Problem with rich frameworks i.g. .net and java is understanding the framework takes time based on the parts you mostly use. Only after that, frameworks reduce your work load. These frameworks are designed by top engineers so if you need to overwrite a lot of them, probably you don't know where the feature you are looking for is or how to implement it. I read somewhere time is like money:

  1. You can spend it (doing something)
  2. You can save it (using proper tools)
  3. You can invest it (spending some of it now, to save much more later)
0

When you create an application, most of what you are doing ought to be framework independent. The framework really ought to be a detail on the side, not an integral part at the centre of your application. Think of it as a delivery mechanism or so. Same goes for databases and whatnot. If you create proper boundaries, you'll be able to switch database, UI, routing framework, etc, without having to rewrite your application.

This means that in general it is not a good idea to have your app work with classes that inherit from things outside of your package boundary. Hard to create a tighter coupling or greater dependency then inheriting. Esp mixing in logging there seems wrong.

On the logging topic - be very careful on what logging code you put in a DbContext. If this DbContext takes a logger using some nice Logger interface, then it can work. Else you are probably violating the Single responsibility principle.

0

It's not a good idea to override framework classes, even when it's supposed to do by authors of the framework. "Always prefer composition over inheritance" - widely known rule. Overriding code of framework makes your code more dependent and tightly coupled with code of framework.

And just to note: "Masterpage" name looks like "God-object" antipattern case.

0

In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by additional user-written code, thus providing application-specific software. A software framework is a universal, reusable software platform to develop applications, products and solutions. Software frameworks include support programs, compilers, code libraries, tool sets, and application programming interfaces (APIs) that bring together all the different components to enable development of a project or solution.

Framework is a set of classes and utilities which has some basic functions but at the same time it is not standalone. Framework has many hooks which you can override to your own logic and has many holes which you must fill up with your own logic. Like interface and abstract class for example. Unless you are doing special work which need your own logic you are free to use the basic general framework logic.

For an example, we can use ASP.NET membership system in our ASP.NET application. It will act with the general membership logic. But if we need a special situation which is special for your application then you have to inherit and override the membership provider classes to give your own implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.