static methods

K

kownacek

Hello.

I have some classes which resemble tables in a database. I'd like to
create CRUD-like methods for each class, for example Create(),
GetAll(), GetById(Guid id), DeleteById(Guid id), etc...

I can implement an interface to force Create() for example, but
GetAll() should return a list/array of object and it should be static.
I cannot make static methods in an interface in C#.

Why do I want interface at all? So I can make a universal unit test
for those CRUD methods.
I've already created a universal property comparization method (using
reflection) which could be used in this universal unit test...

So I thought (as I do know a bit 'bout Java) - i'll just create an
interface...

I know I can use reflection in the unit test aswell, but interface
seemed easyer and more natural.

I dont know if i'm following the correct path at all. Ty in advance.
 
M

Marc Gravell

Correct; it can't be be both static and interface-based. For something
similar, I use a provider model - i.e. I register the *real* providers
via configuration (a provider must implement one-or-more interfaces),
then my static methods act as wrappers to obtain the provider and
invoke - i.e.

public interface IWhateverProvider {
Whatever Get(int id);
}
public static class Whatever {
public static Whatever Get(int id) {
return ProviderManager.GetProvider<IWhateverProvider>().Get(id);
}
}
public static class ProviderManager {
public static T GetProvider<T>() {...}
}

There is *lots* that isn't shown, but that gives the flavour. Easy to
mock; easy to swap providers just through configuration; in my
implementation I also have it set up to look for WCF as providers
(with some "lease" stuff to support IDisposable etc) - which means my
app doesn't need to know if it is getting data from a mock, a DAL, or
a web-service - just that there is an IWhatever interface and
*somebody* is providing it.

I can't post a full solution as it would be large... but is that
enough?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top