473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. _____________________
  2. ___IBaseService
  3.     public interface IBaseService<T> where T : BaseObject
  4.     {
  5.         T get(int id);
  6.         BaseCollection<T> getAll();
  7.         bool add(T obj);
  8.         bool remove(int id);
  9.         bool remove(T obj);
  10.         bool removeAll(BaseCollection<T> collection);
  11.     }
  12.  
  13. ___BaseService
  14. public abstract class BaseService
  15.     {
  16.         public abstract string SystemName
  17.         {
  18.             get;
  19.         }
  20.  
  21.         public bool update()
  22.         {
  23.             try
  24.             {
  25.                 return true;
  26.             }
  27.             catch (Exception exc)
  28.             {
  29.                 return false;
  30.             }
  31.         }
  32.     }
  33.  
  34. ___ServicesRepository
  35. public class ServicesRepository
  36. {
  37.     public static string SystemName = "ServicesRepository";
  38.  
  39.     //The services
  40.     private static Dictionary<string, BaseService> services;
  41.  
  42.     //Instance
  43.     private static ServicesRepository instance;
  44.  
  45.     /// <summary>
  46.     /// Get the instance of ServicesRepository
  47.     /// </summary>
  48.     public static ServicesRepository Instance
  49.     {
  50.         get
  51.         {
  52.             if (instance == null)
  53.                 instance = new ServicesRepository();
  54.             return instance;
  55.         }
  56.     }
  57.  
  58.     /// <summary>
  59.     /// ServicesRepository: Centralization off the Services.
  60.     /// </summary>
  61.     public ServicesRepository()
  62.     {
  63.         if (services == null)
  64.             this.registerServices();
  65.     }
  66.  
  67.     private void registerServices()
  68.     {
  69.     }
  70.  
  71.     public static BaseService Get(string systemName)
  72.     {
  73.         if (services.ContainsKey(systemName))
  74.             return services[systemName];
  75.         else
  76.             return null;
  77.     }
  78. }
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 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
Nov 17 '10 #1
5 2493
hype261
207 100+
I don't think you actually need a static abstract property. It looks to me you are trying to implement a Singleton for the ServicesRepository. If this is correct then to access the Service you need to do the following

Expand|Select|Wrap|Line Numbers
  1. 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 ServiceRepository is messed up. I would look at the Singleton Design Pattern.
Nov 17 '10 #2
Hello hype261,

The ServicesRepository 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
Nov 17 '10 #3
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
Nov 17 '10 #4
Curtis Rutland
3,256 Expert 2GB
Why make it abstract? Just make it public static, with an internal set.

Something like this:

Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         TestBase b = new TestClass();
  6.         Console.WriteLine(TestClass.TestProperty);
  7.         Console.ReadKey();
  8.     }
  9. }
  10.  
  11. public class TestClass : TestBase
  12. {
  13.     static TestClass()
  14.     {
  15.         TestProperty = "TestClass";
  16.     }
  17. }
  18.  
  19. public abstract class TestBase
  20. {
  21.     public static string TestProperty { get; internal set; }
  22.  
  23.     static TestBase()
  24.     {
  25.         TestProperty = "TestBase";
  26.     }
  27. }
Let me know if I completely missed the point.
Nov 19 '10 #5
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
Nov 20 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new...
1
by: flyaflya | last post by:
i use a class static member function to for_each: class t1 { static test(string s); } main() { list<string> slist; ...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
7
by: Nan Li | last post by:
Hello, I got stuck in a situation I think many of you must have already experienced. I want to have a simple lookup map inside my class. All the instances share that map. The code is as follows....
12
by: craigkenisston | last post by:
Hi, I will use an example to do a simplified description of my problem, please don't laugh. I just "believe" I would have to use either interface or abstract classes, but I've not ever write an...
4
by: Hei | last post by:
Hi is vb.net' "interface" is sound like C++ "abstract class" ? Hei
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.