System.Web.Http.Controllers.ServicesContainer abstract class and Generic methods

Topics: ASP.NET Web API
May 23, 2012 at 4:46 PM
Edited May 23, 2012 at 5:22 PM

ServicesContainer abstract class is implemented as below:

 

public abstract class ServicesContainer : IDisposable
    {
        protected ServicesContainer();

        public void Add(Type serviceType, object service);
        public void AddRange(Type serviceType, IEnumerable<object> services);
        public virtual void Clear(Type serviceType);
        protected virtual void ClearMultiple(Type serviceType);
        protected abstract void ClearSingle(Type serviceType);
        public virtual void Dispose();
        public int FindIndex(Type serviceType, Predicate<object> match);
        public abstract object GetService(Type serviceType);
        protected abstract List<object> GetServiceInstances(Type serviceType);
        public abstract IEnumerable<object> GetServices(Type serviceType);
        public void Insert(Type serviceType, int index, object service);
        public void InsertRange(Type serviceType, int index, IEnumerable<object> services);
        public abstract bool IsSingleService(Type serviceType);
        public bool Remove(Type serviceType, object service);
        public int RemoveAll(Type serviceType, Predicate<object> match);
        public void RemoveAt(Type serviceType, int index);
        public void Replace(Type serviceType, object service);
        protected virtual void ReplaceMultiple(Type serviceType, object service);
        public void ReplaceRange(Type serviceType, IEnumerable<object> services);
        protected abstract void ReplaceSingle(Type serviceType, object service);
        protected virtual void ResetCache(Type serviceType);
    }

 

I was wondering if generic methods would be a better fit for those classes. For example, can this abstract class be rewritten as below?

 

public abstract class ServicesContainer : IDisposable
    {
        protected ServicesContainer();

        public void Add<T>(T service);
        public void AddRange<T>(IEnumerable<T> services);
        public virtual void Clear<T>();
        protected virtual void ClearMultiple<T>();
        protected abstract void ClearSingle<T>();
        public virtual void Dispose();
        public int FindIndex<T>(Predicate<T> match);
        public abstract T GetService<T>();
        protected abstract List<T> GetServiceInstances<T>();
        public abstract IEnumerable<T> GetServices<T>();
        public void Insert<T>(int index, T service);
        public void InsertRange<T>(int index, IEnumerable<T> services);
        public abstract bool IsSingleService<T>();
        public bool Remove<T>(T service);
        public int RemoveAll<T>(Predicate<object> match);
        public void RemoveAt<T>(int index);
        public void Replace<T>(T service);
        protected virtual void ReplaceMultiple<T>(T service);
        public void ReplaceRange<T>(IEnumerable<T> services);
        protected abstract void ReplaceSingle<T>(T service);
        protected virtual void ResetCache<T>();
    }

 

Or at least, would it be possible to provide generic methods as option along with the current ones?

Edit:

Even better for multiple services as it is used inside DefaultServices class:

public void AddRange<T>(params T[] services);

May 23, 2012 at 6:12 PM
Generic extension methods can be written, but the core methods must remain non-generic (it's never acceptable in these situations to have generic without non-generic, because in order to call generic versions you must know the type T at compile time, and there are plenty of cases where that's not the case).
May 23, 2012 at 6:49 PM

Thanks Brad.

I guess the best case here is to supply them as extensions as you indicated.