tags:

views:

254

answers:

4

I have a data provider project to access the database. this is composed by various classes (PersonDataProvider, JobDataProvider ...) I want to create an Interface. Do I have to create an Interface for each class? I was tempted to create one interface and than inherit on all the classes. This involves making all the projects classes partial and change the classes name.......But i think is not the best solution. Any suggestion?

A: 

Using interface depends how you want to arrange the classes. Interface allows some sort of plug and play behaviour. So, if you need a single interface, this will mean that you shall have a single set of interfaces accross all the classes implementing the interface. In such a case, your classes PersonDataProvider, JobDataProvider etc. will have the same set of methods. If you feel, they need to be different and still be available through a single provider facade, you can think of using a facade pattern.

The facade will have interfaces for individual provider and the provider classes will implement them.

Kangkan
+4  A: 

I think you are approaching this incorrectly.

When you make an interface, you're making a contract for those classes. Think of it as "my class will act as a IMyInterface".

If all of your classes have a common usage scenario, then a single, common interface may be appropriate (IDataProvider, given the class names..?).

Reed Copsey
Each of my class is responsible for retrieving a certain 'type' of data from the database. such as the PersonDataProvider will retrieve the person details the JobDataProvider the Job details and so on.What do you mean for common usage scenario???All are used to retrieve the data for the application ...is this common scenario?
GigaPr
You can use IDataProvider<T>, where T is Job, Person, etc...
Reed Copsey
A: 

First off, I'm assuming there are standard method calls across all your xDataProvider classes. For example, instead of a SelectPerson method, you have a Select method on the PersonDataProvider class. If not, you have some work to do to make this a valid exercise.

Within Visual Studio, there is an Extract Interface refactoring option. Right-click in a xDataProvider class and choose Refactor - Extract Interface. Now name it (IDataProvider, for example) and choose the methods / properties you want in your interface, click OK and your done with this class.

Then just implement this IDataProvider interface in your other xDataProvider classes. Assuming you've already implemented similar methods in all you DataProvider classes, you won't have to write any more code (beyond the : IDataProvider).

Austin Salonen
I see your point but if lets take in consideration the following scenarioclass 1public Person Get(int id){}class2 public Job Get(int id, DateTime date)class3 public Address Get(int id, srting postcode)All the classes implementin IDataProvider will have to implement methods not neededIsn it?
GigaPr
I would say that you would have a standard `Get(int id)` method in your `IDataProvider` interface.
Austin Salonen
+3  A: 

You don't inherit an Interface you implement it. There's no need to make a class partial to add an interface to it.

An interface is a contract that the class subscribes to saying that it will honour the methods described in the interface and will implement them appropriately. For your scenario you'd create a single interface and implement it in your classes, you can then pass the instances of the various accessor classes as instances of the interface.

For example:

public interface IDataProvider
{
    void LoadData();
}

The data providers would then look as follows:

public class MyDataProvder1 : IDataProvider
{
    // Some methods

    // Must implement LoadData
    public void LoadData()
    {
        // Do something
    }
}

public class MyDataProvder2 : IDataProvider
{
    // Some methods

    // Must implement LoadData
    public void LoadData()
    {
        // Do something
    }
}

You can then pass the objects as IDataProvider as follows:

IDataProvider DataProviderA = new MyDataProvider1();
IDataProvider DataProviderB = new MyDataProvider2();

// Call function that expects an IDataProvider

DoSomething(DataProviderA);
DoSomething(DataProviderB);

...

public void DoSomething(IDataProvider DataProvider)
{
    DataProvider.LoadData();
}

Hopefully that clears it up for you.

Lazarus
Seems to me that with your approach all the classes have to implement all the methods....i do not want it. I want to define all the methods in the interface but allow to each class to implement the one relevant to its purpose. retrieving person data, job data...
GigaPr
The LoadData method in the example can do different things within each class. For returning different data types I think you'd need to resort to Generics, something to read up on, you'll be glad you did.
Lazarus
yes but your solution works just for methods without any parameter.Other way each method will implement methods not needed.
GigaPr