A interface and abstract class with static member

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan Cheung

    A interface and abstract class with static member

    Hi guys,

    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:
    Code:
    _____________________
    ___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;
        }
    }
    It's a pretty simple setup actually, I believe most of you would use the naming repository but I use Service because my services does a bit more than just keep and get the objects.

    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 ServicesReposit ory. 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 = ServicesReposit ory.Get(UserSer vice.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
    Last edited by NeoPa; Nov 17 '10, 04:42 PM. Reason: CODE tags
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    I don't think you actually need a static abstract property. It looks to me you are trying to implement a Singleton for the ServicesReposit ory. If this is correct then to access the Service you need to do the following

    Code:
    UserService userService = ServicesRepository.Instance.Get(UserService.SystemName);
    Also this function public static BaseService Get(string systemName) shouldn't be static and your constructor for the ServiceReposito ry is messed up. I would look at the Singleton Design Pattern.

    Comment

    • Bryan Cheung

      #3
      Purpose

      Hello hype261,

      The ServicesReposit ory is good right now. The Repository should not be singleton because its.. a repository, I want the possibility to make more then just one. But! there should only be one of each service in it. And the services should also not be singleton because I want to be able to create more then one, because I can have more Repository's then one.

      The constructor is fine because I should load the services at construction of the object and then I should register all services to the services property. This way all the services that should(!) be loaded are loaded. Maybe in the future I'll extend it in a way that the services are registered in config file(XML), corresponding with the SystemName.

      This way I can have different repository classes with different types of services for different purposes :) and still be easy in use because of the config files(like modules).

      Sorry, I should have told before what I exactly wanted.

      So, is there a way to enforce the SystemName at the child services but still be static so there is one for each type of service child class?

      Kind regards,
      Bryan

      Comment

      • Bryan Cheung

        #4
        Sorry for the double post, I should register -_- but I see I said "call with no instance, if I make a instance I defeat all purpose of this setup". With this I mean, at the time of getting the service, I don't want to create a new service but get the one from the repository. The repository is kept in a Base class for UserControls so don't worry about that one.

        As example but not in use: There could be a repository for the base class of the masterpages and a different repository for the base class of the pages. I see no purpose for that now, but it could be :P and I'd like the option open :)

        Kind regards,
        Bryan Cheung

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Why make it abstract? Just make it public static, with an internal set.

          Something like this:

          Code:
          class Program
          {
              static void Main(string[] args)
              {
                  TestBase b = new TestClass();
                  Console.WriteLine(TestClass.TestProperty);
                  Console.ReadKey();
              }
          }
          
          public class TestClass : TestBase
          {
              static TestClass()
              {
                  TestProperty = "TestClass";
              }
          }
          
          public abstract class TestBase
          {
              public static string TestProperty { get; internal set; }
          
              static TestBase()
              {
                  TestProperty = "TestBase";
              }
          }
          Let me know if I completely missed the point.

          Comment

          • Bryan Cheung
            New Member
            • Nov 2010
            • 55

            #6
            Hi Curtis,

            But this way, you don't enforce TestProperty, in the subclasses, right? you give it a default. I'll keep this one in mind as my last resort if I cant solve it.

            -Bryan

            Comment

            Working...