Search code examples
c#asp.netcode-reuse

Is it possible to make a reusable GlobalBase/HttpApplication with an "abstract static"?


I'm attempting to eliminate the duplication accross two ASP.NET web projects that use exactly the same code to create a DI container.

I ran into the problem of not being able mark a static as abstract.

Here's the code that is duplicated:

namespace LendingTreeLib
{
    public class Global : HttpApplication, IContainerAccessor
    {
        void Application_Start(object sender, EventArgs e)
        {
            BuildContainer();
        }

        void Application_End(object sender, EventArgs e)
        {
            CleanUp();
        }

        protected static void CleanUp()
        {
            if (Container != null)
            {
                Container.Dispose();
            }
        }

        private static IUnityContainer _container;

        public static IUnityContainer Container
        {
            get
            {
                return _container;
            }
            set
            {
                _container = value;
            }
        }

        IUnityContainer IContainerAccessor.Container
        {
            get
            {
                return Container;
            }
        }

        protected static void BuildContainer()
        {
            IUnityContainer container = new UnityContainer();

//     ----------------------------------------------------------------
// --> this is where a call to an virtual/abstract seems to be required
//     ----------------------------------------------------------------

            Container = container;
        }
    }
}

Any thoughts on how I can achieve my goal?

UPDATE: I think I may have asked this when I could solve it myself, however, I'm going to post my solution here because it would have been helpful to me before I learned it..

Since Application_Start is only called once, and it is also an instance method, there doesn't appear to be any harm in simply switching to non-static methods for my solution.

Here it is (with extra indirection taken out):

namespace DAgents.Common.Web
{
    public abstract class GlobalBase : HttpApplication, IContainerAccessor
    {
        void Application_Start(object sender, EventArgs e)
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            GlobalBase.Container = container;
        }

        protected abstract void RegisterTypes(IUnityContainer container);

        public static IUnityContainer Container { get; set; }

        void Application_End(object sender, EventArgs e)
        {
            if (Container != null)
                Container.Dispose();
        }

        IUnityContainer IContainerAccessor.Container { get { return GlobalBase.Container; } }
    }
}

Solution

  • In response to the question,

    No.

    A static method cannot be abstract, since static methods are not inherited.

    An abstract cannot be static, since static methods must have an implementation.

    C# and Java don't work this way (as yet.) It leads me to the moot and unhelpful question, are static methods Object Orientated?