开发者

C# - Interface split?

I have a class that have a different Add method than the other classes and therefor cant implement the same interface... Should I split the current interface so it can use it too or should I just create another interface for it?

UPDATE:

public interface IProductRepository<T, T2>
    where T : class
    where T2 : class
{
    void Add(T model, int categoryId);
    void Edit(T model, int id);
    void Delete(int id);

    T2 Get(int id);
}

As you see then the interface above have an Add method which requires a categoryId.

My Category class is the same as above but without the categoryId parameter in the A开发者_如何学编程dd method. Should I just create a new interface for the Category class?


C# allows you to explicitly implement an interface method:

public class A : IFoo, IBar{
    void IFoo.DoSomething() {
        //DoSomething();
    }
    int IBar.DoSomething() {
        return -1;
    }
}

This should allow you to implement any interface, even if it conflicts with method signatures that you already have defined. Which method gets called will depend on which Type it is being called on. For example:

((IFoo) new A()).DoSomething(); // ... may do something different than ...
((IBar) new A()).DoSomething(); // ... would do.


Think about what is for it.

Maybe your other class don't seems to be like to functionality of this interface rather than doing same action (adding). Then you can create other interface for this new (new other) type of classes or leave this class alone without abstract interface.

Maybe you want do some refactoring on other class to get definition of Add() smillar in interface.

If you can't refactor other class and this class seems to have this interface. Then you create other interface or in old interface new Function like Add_Extended() for adding functionality with other arguemnt list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜