Multiple inheritance in C # - again

I know that C # does not offer multiple inheritance. And I know that there are workarounds, like this one .

But here is the problem that I encountered today cannot find any ELEGANT workaround. I will add some abstract code sample so you get it faster.

(let it be real ASP.NET code, because these code samples of class A, class B are really confusing):

public class AdminPage : System.Web.UI.Page { protected override void OnInit(EventArgs e) { //if not an admin - get out if(!CurrentUserIsAdmin()) Response.End(); base.OnInit (e); } } public class JQueryPage : System.Web.UI.Page { protected override void OnInit(EventArgs e) { RegisterJQueryScript(); base.OnLoad (e); } } //now here what I REALLY miss in C# public class AdminJQueryPage : AdminPage, JQueryPage; 
+4
source share
6 answers

Derive functionality? This is best for single responsibility. You should think carefully about your designers.

 interface IAdminPage { public string AdminPageMethod(); } interface IJQueryPage { public string JQueryPageMethod(); } internal class AdminPage : IAdminpage { private string someString; internal AdminPage(string value) { this.someString = value; } public string AdminPageMethod() { return "AdminPage result with some string: " + this.someString; } } internal JQueryPage : IJQueryPage { private int someNumber; internal JQueryPage(int value) { this.someNumber = value; } public string JQueryPageMethod() { return "JQueryPage result with number: " + this.someNumber; } } class AdminJQueryPage : IQueryPage, IAdminpage { private readonly IAdminPage adminPage; private readonly IJQueryPage jqueryPage; public AdminJQueryPage(string someString, int someNumber) { this.adminPage = new AdminPage(someString); this.jqueryPage = new JQueryPage(someNumber); } public string AdminPageMethod() { return this.adminPage.AdminPageMethod(); } public string JQueryPageMethod() { return this.adminPage.JQueryPageMethod(); } } 

If you really want multiple inheritance, take a look at the Scala signs

Edit: Added transfer of constructor values ​​to grouped classes. In addition, inner classes (cannot be accessed or created outside of the assembly) because they are only created by the AdminJQueryPage class, which is a public class.

+9
source

I also came with C ++ and did not miss it, especially since I read Refactoring [and used the tool not for OOTB for this].

You can use PostSharp to publish based on the placement of attributes on your AdminJQueryPage, which will achieve the same effect.

Or you can extract the method code into helper classes and call this (e.g. Joe example)

Or you can put helpers in one base class and call from it.

In any case, your code will be more understandable.

It is only a matter of time before your mixes begin to overlap, and then your general set of methods for managing this complexity should be involved - in C ++, MI should have been only one tool in the set, not a very sexy hammer.

+3
source

it can be faked by mixin by specifying an interface and creating extension methods for that interface. however, I do not use this, it will help to redefine methods, adding only new ones. of course you can then call the extension method when overriding, but this is basically the same as fetching methods in a helper class, but with a little more sugar

+2
source

Even if it was possible, one problem with the semantics of the MI-based solution of the particular problem you raised is what happens on the markup side? Does the Render () method that generates the markup run first in one class and then in another? This is probably not the behavior you want when both classes generate whole pages.

If you are open to solutions that are outside the language itself, ASP.NET has several elegant options that will determine the type of problem you raised (changing the actions taken during an event in the page life cycle), For example:

  • Page Adapters
  • Test adapters
  • Custom controls
  • HttpModules
  • Main pages
  • Tag display

The best choice, of course, depends on the details of your application. In case this is useful, I will talk about these options in my book, including sample code: Ultra-fast ASP.NET .

+2
source

The easiest way to create a hierarchy is to let AdminPage inherit from JQueryPage like this:

 public class AdminPage : JQueryPage { protected override void OnInit(EventArgs e) { //if not an admin - get out if(!CurrentUserIsAdmin()) Response.End(); base.OnInit (e); } } public class JQueryPage : System.Web.UI.Page { protected override void OnLoad(EventArgs e) { RegisterJQueryScript(); base.OnLoad (e); } } //now here what I REALLY miss in C# public class AdminJQueryPage : AdminPage 

My guess is that some of these awkwardness comes from the ASP.NET page model, which uses overridden methods of the base class.

+1
source

You can do this using interfaces

  public interface IJQueryPage { } public abstract class AdminPage : System.Web.UI.Page { protected override void OnInit(EventArgs e) { //if not an admin - get out if(!CurrentUserIsAdmin()) Response.End(); base.OnInit (e); } protected override void OnLoad(EventArgs e) { if (this is IJQueryPage) { RegisterJQueryScript(); } base.OnLoad (e); } } public class AdminJQueryPage : AdminPage, IJQueryPage { } 
0
source

All Articles