So i've read already about its not possible to make a abstract property static but then i'm a bit stuck and hoping you guys can show me a other way.
The setup:
Expand|Select|Wrap|Line Numbers
- _____________________
- ___IBaseService
- public interface IBaseService<T> where T : BaseObject
- {
- T get(int id);
- BaseCollection<T> getAll();
- bool add(T obj);
- bool remove(int id);
- bool remove(T obj);
- bool removeAll(BaseCollection<T> collection);
- }
- ___BaseService
- public abstract class BaseService
- {
- public abstract string SystemName
- {
- get;
- }
- public bool update()
- {
- try
- {
- return true;
- }
- catch (Exception exc)
- {
- return false;
- }
- }
- }
- ___ServicesRepository
- public class ServicesRepository
- {
- public static string SystemName = "ServicesRepository";
- //The services
- private static Dictionary<string, BaseService> services;
- //Instance
- private static ServicesRepository instance;
- /// <summary>
- /// Get the instance of ServicesRepository
- /// </summary>
- public static ServicesRepository Instance
- {
- get
- {
- if (instance == null)
- instance = new ServicesRepository();
- return instance;
- }
- }
- /// <summary>
- /// ServicesRepository: Centralization off the Services.
- /// </summary>
- public ServicesRepository()
- {
- if (services == null)
- this.registerServices();
- }
- private void registerServices()
- {
- }
- public static BaseService Get(string systemName)
- {
- if (services.ContainsKey(systemName))
- return services[systemName];
- else
- return null;
- }
- }
What I basiclly want to accomplish it that I can register all my services that are derived from BaseService to have a SystemName so I can register them in the ServicesRepository. Now I just need one instance of the service and want to register it by a name. So I want to be able to do something along of:
UserService userService = ServicesRepository.Get(UserService.SystemName);
So I think(!) I need a static abstact property because I want it to static(call with no instance, if I make a instance I defeat all purpose of this setup) but I want it in the BaseService or IBaseService so all the child classes must have systemName.
Any thoughts?
Many thanks,
Bryan