Lasse Edsvik
Lasse Edsvik

Reputation: 9298

Interface for 4 interfaces?

I'm having a quite large database app and trying to be using the repository pattern.

I have 4 interfaces: IProductRepository, IFileRepository .... , and I want to have an combined interface for all 4 which I want to act like an API to other parts of the application.

Must I have a class and type in all the methods of the 4 interfaces, and add interface to that class? Sounds like lots of work. Since each of the 4 have lots of methods I wouldn't like to type them all once again.

Or how do I solve this in the most clean and nice way?

Upvotes: 2

Views: 147

Answers (5)

ChrisWue
ChrisWue

Reputation: 19020

One other option is to use a tool like Dynamic Proxy and mixin your repository implementations. This would relieve you of the work having to create the manual forwarding implementation.

Example:

public class Product 
{
}
public interface IProductRepository
{
    void Save(Product p);
}
public class ProductRepository : IProductRepository
{
    public void Save(Product p)
    {
        Console.WriteLine("Saved Product");
    }
}

public class File 
{
}
public interface IFileRepository
{
    void Save(File f);
}
public class FileRepository : IFileRepository
{
    public void Save(File f)
    {
        Console.WriteLine("Saved File");
    }
}

public class Repository 
{
}

Create the mixin:

var options = new ProxyGenerationOptions();
options.AddMixinInstance(new ProductRepository());
options.AddMixinInstance(new FileRepository());
var generator = new ProxyGenerator();
var proxy = (Repository)generator.CreateClassProxy(typeof(Repository), options);
(proxy as IProductRepository).Save(new Product());
(proxy as IFileRepository).Save(new File());

Somehow I have the feeling you should also be able to combine your interfaces and proxy on that. Something along those lines:

public interface IFullRepository : IProductRepository, IFileRepository
{
}

var proxy = (IFullRepository)generator.???

I haven't found a way to make it work - maybe ask the Dynamic Proxy guys.

Upvotes: 0

Euphoric
Euphoric

Reputation: 12849

Yes. This is possible downside to using interfaces. Only good way to make this bearable is to use some kind of code generation tool. Something like T4.

Upvotes: 1

Dave
Dave

Reputation: 3621

What about implementing a generic IRepository that each of the classes can use?

Upvotes: 0

Kostassoid
Kostassoid

Reputation: 1898

Yes, there's no other way, as far as I know. But tools like Resharper make it all a lot easier.

Upvotes: 1

Padu Merloti
Padu Merloti

Reputation: 3229

From your description, it sounds like a Facade should fit the bill.

http://www.dofactory.com/Patterns/PatternFacade.aspx

Upvotes: 4

Related Questions