Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:10+00:00 2026-05-15T15:33:10+00:00

Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }

  • 0

Consider this abstract class

public abstract class Foo
{
    public Injectable Prop {get;set;}
}

i have an application that i want to enhance and simultaneously refactor for simplicity.
I have over 100 classes that call some same stuff (e.g Class Injectable) and i am thinking that this behaviour can be abstracted and set to a base class, and everybody will inherit from this base class so as to remove copy/paste code.

However, i want to avoid copy/paste code in the spring configuration file by defining an Injectable object and then define a child object foreach and every class that inherits from Foo.

I am looking for a way to set the abstract class’s properties and then all child elements to automatically get them via configuration. I want to avoid to make the abstract class like this:

public abstract class Foo
{
    public Injectable Prop 
    { 
       get { return (Injectable)ContextRegistry.GetContext()["Injectable"]; }
    }
}

thanks for any suggestions

EDIT:
to make things a bit more complicated the child classes are ASP.NET pages, so i have limited control as to how they are generated.

Currently i’m employing the above mentioned code where the abstract class makes a reference to a DI created object with Id “Injectable”. I would like to avoid flying strings

UPDATE (With solution)

Consider:
Classes

public abstract class BasePage : System.Web.UI.Page
{
   public IInjectable FooProp {get;set;}
}

public abstract class BaseControl : System.Web.UI.UserControl
{
   public IInjectable FooProp {get;set;}
}

public partial class ChildPage : BasePage
{
   protected void Page_Load(object sender, EventArgs e)
   {
       FooProp.DoSomeThing();
   }
}

public partial class ChildControl : BaseControl
{
   protected void Page_Load(object sender, EventArgs e)
   {
       FooProp.DoSomeThing();
   }
}

spring cfg
…

<object id="Injectable" type="ConcreteInjectable">
    <property name="SomeProp" value="Injected!!" />
</object>
<!--The requirement is to declare something like:-->
<object type="BasePage" abstract="true">
  <property name="FooProp" ref="Injectable />
</object>
<!--it works for usercontrols too-->
<object type="BaseControl" abstract="true">
  <property name="FooProp" ref="Injectable />
</object>

and the effect will be for each inheritor of BasePage will have the FooProp property injected with what i configured.

it matters little if this can be achieved with some kind of convention binding but i do not want use strings and using DI references from inside my code.

2nd UPDATE AND SOLUTION
thanks to tobsen and Erich Eichinger the solution was found:
Firstly, this is not supported natively, but the solution is not very ugly nor breaks DI pattern norms in a bad way

Now, all the spring configuration required is given above (in the update)
Erich’s solution is this, make an IHttpModule like so:

public class PageModuleInjecter : IHttpModule
{
  public void Dispose() {}

  public void Init(HttpApplication context) {
     context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
  }

  void context_PreRequestHandlerExecute(object sender, EventArgs e) {
    IHttpHandler handler = ((HttpApplication )sender).Context.Handler;
    if (handler is BasePage)
        Spring.Context.Support.WebApplicationContext.Current
           .ConfigureObject(handler, typeof(BasePage).FullName);
  }
}

and that’s it!
(don’t forget to register the module in web.config as always)

In search of functionality (and perhaps elegance) i found that instead of using the IHttpModule (i did’t want to add yet another class) you can declare the BasePage as such

public abstract class BasePage : System.Web.UI.Page
{
   public IInjectable FooProp {get;set;}

   protected override OnPreInit(EventArgs e)
   {
       Spring.Context.Support.WebApplicationContext.Current
           .ConfigureObject(this, typeof(BasePage).FullName);
       base.OnPreInit(e);
   }
}

and it works like a charm without requiring any added modules etc.

Gladly this works on UserControls as well (albeit in a different event in the lifecycle since OnPreInit does not exist for usercontrols):

public abstract class BaseControl : System.Web.UI.UserControl
{
   public IInjectable FooProp {get;set;}

   protected override OnInit(EventArgs e)
   {
       Spring.Context.Support.WebApplicationContext.Current
          .ConfigureObject(this, typeof(BaseControl).FullName);
       base.OnInit(e);
   }
}

thanks for watching!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:33:11+00:00Added an answer on May 15, 2026 at 3:33 pm

    sorry I have only limited time atm, plz ping me if the description below should be too short/abstract

    First, there is no such functionality yet available. But with a few lines of code you can do this on your own.

    In general below I will be pursuing the idea of mapping a particular class onto an object-definition to be used for configuring the instance. Something along the lines of

    if (object is MyBaseClass)
         applicationContext.Configure(object, "myObjectDefinitionName");
    

    Here’s the outline of a solution:
    Since your problem is ASP.NET WebForms-related, an HttpModule is a good starting point to hook into creating and configuring .aspx pages.
    The idea is to write your own IHttpModule that performs the page configuration right before it gets executed. Basically all you need is

    public class MyBaseClassConfigurationModule : IHttpModule {
    
      private System.Collections.Generic.Dictionary<Type, String> typeObjectDefinitionMap;
    
      public void Dispose() {}
    
      public void Init(HttpApplication context) {
         context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
      }
    
      void context_PreRequestHandlerExecute(object sender, EventArgs e) {
        IHttpHandler handler = ((HttpApplication )sender).Context.Handler;
        foreach(Type t in typeObjectDefinitionMap.Keys) {
            if (t.IsAssignableFrom(app.Context.Handler.GetType)) {
                Spring.Context.Support.WebApplicationContext.Current
                  .ConfigureObject(handler, typeObjectDefinitionMap[t]);
            }
        }
      }
    }
    

    and configure your module according to 22.4.2. Injecting dependencies into custom HTTP modules.

    hth,
    Erich

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Original Question Consider the following scenario: public abstract class Foo { public string Name
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
Consider this piece of code: public abstract class Validator { protected Validator() { }
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...
Consider the following example. I have an interface MyInterface, and then two abstract classes
Consider this: One mySQL database that has tables and rows and data within it.
consider this string prison break: proof of innocence (2006) {abduction (#1.10)} i just want
On my WPF application, I am using DataContractSerializer to serialize object. I observed that
Possible Duplicate: Is there a printf converter to print in binary format? Consider this
I need to run an application in an embedded system continuously. While implementing this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.