0

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; } }
    }
}
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
  • 1
    Does `BuildContainer()` need to be static? If it can be made an instance member, then I *think* that would solve the problem, right? Also, have you seen WebActivator and PreApplicationStartMethod? - http://ilearnable.net/2010/11/22/webactivator-preapplicationstartmethod/ May be able to solve reusability with this instead – Russ Cam Nov 07 '11 at 17:03
  • maybe i gave up too quickly... that's the problem sometimes when you know people who know more than you are an SO quest. away :) – Aaron Anodide Nov 07 '11 at 17:04
  • 1
    The short answer is no, the long answer is here: [Why can't I have abstract static methods in C#?][1] [1]: http://stackoverflow.com/questions/3284/why-cant-i-have-abstract-static-methods-in-c – rick schott Nov 07 '11 at 17:05

1 Answers1

1

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?

Jodrell
  • 34,946
  • 5
  • 87
  • 124