简体   繁体   English

我可以创建通用接口吗?

[英]Can I create a generic interface

I have the following interface called IAccountService. 我有以下称为IAccountService的接口。 I also have exactly the same interfaces for Product and Package etc. 我也有完全相同的产品和包装接口等。

public interface IAccountService
    {
        void AddOrUpdate(Account account);
        void Delete(Account account);
        bool DoesTableExist();
        Account Get(Account account);
        Account Get(string pk, string rk);
        IEnumerable<Account> Get();
        IEnumerable<Account> Get(string pk);
        string GetOptions(string pk, string rk);
        IEnumerable<AccountDetail> ShowDetails(ref string runTime);
        IEnumerable<AccountSummary> ShowSummary(ref string runTime);
        void ValidateNoDuplicate(Account account);
        void ValidateNoProducts(Account account);
    }

I created some generic methods but I am wondering can I also create a generic interface. 我创建了一些通用方法,但我想知道我是否也可以创建一个通用接口。 If so then how would I call it and how could I change the above to make it generic. 如果是这样,我将如何调用它,如何更改上面的内容使其成为通用的。 Presently I use this interface as follows: 目前我使用这个界面如下:

public class AccountService : BaseService, IAccountService

Update 1 更新1

Thanks for all the suggestions. 感谢所有的建议。 The one thing I still have a problem with is AccountDetail and AccountSummary classes. 我仍有问题的一件事是AccountDetail和AccountSummary类。 I am thinking maybe these should be sub classes. 我想也许这些应该是子类。 But how then can I handle the naming of those? 但是,我怎么能处理那些命名呢? I would have to take the class name, append Detail and then use that in the interface. 我必须取类名,追加Detail然后在界面中使用它。 IS that possible? 那可能吗?

Update 2 更新2

Here's an example of the detail and summary classes: 以下是详细信息和摘要类的示例:

public class AccountDetail
{
    public string Title { get; set; }
    public string Product { get; set; }
}
public class AccountSummary
{
    public string Title { get; set; }
    public Int32? ProductCount { get; set; }
    public string PartitionKey{ get; set; }
    public string RowKey { get; set; }
    public DateTime? Modified { get; set; }
    public string ModifiedBy { get; set; }
}

The above classes are used for reporting. 上述类用于报告。 I am thinking that they probably should not be part of the model repository. 我想他们可能不应该成为模型库的一部分。 Maybe they should be in another place. 也许他们应该在另一个地方。

Regarding the ref comments. 关于ref评论。 The ref is there because in my controller I call the following method: 引用是因为在我的控制器中我调用以下方法:

_account.ShowDetails(ref runTime);

The output of ShowDetails is a list of details and the runTime reference is updated with the time that it takes to run the report. ShowDetails的输出是一个详细信息列表,runTime引用随运行报告所花费的时间而更新。

You could always do: 你可以随时做:

public interface IService<T>
{
    bool TableExists { get };

    void AddOrUpdate(T item);
    void Delete(T item);
    T Get(T item);
    T Get(string pk, string rk);
    // etc
}

public class AccountService : BaseService, IService<Account>

In the case of the Detail/Summary methods, I would break them out into a separate location (probably some sort of mapper class). 在Detail / Summary方法的情况下,我会将它们分解到一个单独的位置(可能是某种mapper类)。

You can make a generic interface the same way you make a generic class: 您可以像创建泛型类一样创建通用接口:

public interface IRepository<T> {
    void AddOrUpdate(T item);
    bool TableExists { get; }
    IEnumerable<T> All { get; }

    ...
    IEnumerable<Detail<T>> GetDetails(string runTime);
    ...
}

This should work for you: 这应该适合你:

    public interface IService<T, TDetail, TSummary>
    {
        void AddOrUpdate(T account);
        void Delete(T account);
        bool DoesTableExist();
        T Get(T account);
        T Get(string pk, string rk);
        IEnumerable<T> Get();
        IEnumerable<T> Get(string pk);
        string GetOptions(string pk, string rk);
        IEnumerable<TDetail> ShowDetails(ref string runTime);
        IEnumerable<TSummary> ShowSummary(ref string runTime);
        void ValidateNoDuplicate(T account);
        void ValidateNoProducts(T account);
    }

    public class AccountService : BaseService, IService<Account, AccountDetail, AccountSummary>
    public class ProductService : BaseService, IService<Product, ProductDetail, ProductSummary>
    public class PackageService : BaseService, IService<Package, PackageDetail, PackageSummary>

Note that you would need 3 types, or have a seperate IAccountService interface that handles the summary and detail methods itself. 请注意,您需要3种类型,或者具有单独的IAccountService接口来处理摘要和详细信息方法本身。

public interface IService<T>
{
    void AddOrUpdate(T entity);
    void Delete(T entity);
    bool DoesTableExist();
    T Get(T entity);
    T Get(string pk, string rk);
    IEnumerable<T> Get();
    IEnumerable<T> Get(string pk);
    string GetOptions(string pk, string rk);
    IEnumerable<AccountDetail> ShowDetails(ref string runTime); // you will need to redisign this part
    IEnumerable<AccountSummary> ShowSummary(ref string runTime); //same for this method
    void ValidateNoDuplicate(T account);
    void ValidateNoProducts(T account);
}

and use it like this: 并像这样使用它:

public class AccountService : BaseService, IService<Account>
public interface IAccountService<T>
{
...
SomeDataType<T> SomeMethod(...);
...
}

You could define it like so: 您可以像这样定义它:

public interface IService<T>
{
  void AddOrUpdate(T entity);
  T Get(T entity);
  // etc
}

And use it like so: 并像这样使用它:

public class AccountService : IService<Account>
{
  void AddOrUpdate(Account entity);
  Account Get(Account entity);
  // etc
}

Something like this? 像这样的东西?

public interface IService<T, TDetail, TSummary>
{
    void AddOrUpdate(T entity);
    void Delete(T entity);
    bool DoesTableExist();
    T Get(T entity);
    T Get(string pk, string rk);
    IEnumerable<T> Get();
    IEnumerable<T> Get(string pk);
    string GetOptions(string pk, string rk);
    IEnumerable<TDetail> ShowDetails(ref string runTime);
    IEnumerable<TSummary> ShowSummary(ref string runTime);
    void ValidateNoDuplicate(T entity);
    void ValidateNoProducts(T entity);
}

A few things to note: 有几点需要注意:

  1. There appeared to be three distinct types to be generalized, so there are three corresponding type parameters. 似乎有三种不同的类型可以推广,因此有三种相应的类型参数。 If the latter two aren't correct, go ahead and update or let me know and I can help with that. 如果后两者不正确,请继续更新或让我知道,我可以提供帮助。
  2. I renamed parameters to "entity" as a more generic term. 我将参数重命名为“实体”作为更通用的术语。 You can choose whatever name makes sense to you. 您可以选择任何对您有意义的名称。
  3. Are the last two method names generic enough for this? 最后两个方法名称是否足够通用? ValidateNoProducts seems suspect to me if it also exists on the Product version of this interface. 如果此接口的Product版本中也存在ValidateNoProducts ,则对我来说似乎是怀疑的。

All in all, while it's possible to make a single generic interface, one important question you need to ask yourself is if this is indeed the right abstraction. 总而言之,虽然可以创建一个通用接口,但您需要问自己的一个重要问题是,这确实是正确的抽象。 Some of these methods might be intuitively more type-specific and belong on a different type of service. 其中一些方法可能直观地更加特定于类型,并且属于不同类型的服务。

To that end, you can create an IService<> with most of these methods and then have your AccountService and other services which implement it also add methods of their own. 为此,您可以使用大多数这些方法创建一个IService<> ,然后让您的AccountService和其他实现它的服务也添加自己的方法。 That would seem like a more viable abstraction, as opposed to trying to fit a square peg into a round hole with incorrect abstractions. 这似乎是一种更可行的抽象,而不是试图将方形钉固定到具有不正确抽象的圆孔中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以创建通用的接口属性吗? - Can I create Interface Properties as Generic? 我可以创建一种接口类型的通用方法吗? - Can I Create A Generic Method of a Type of Interface? 如何在C#中的通用接口上创建协变扩展方法? - How can I create a covariant extension method on a generic interface in C#? 如何在C#中创建具有任意数量的泛型参数的接口? - How can i create an interface in C# that has an arbitrary number of generic parameters ? 如何约束通用类型以实现通用接口? - How can I constrain a generic type to implement a generic interface? 我可以声明一个接口方法来返回C#中的通用接口吗? - Can I declare an interface method to return generic Interface in C#? 我如何获得通用接口的接口实现 - how can i get the interface implementation of a generic interface 在接口中创建通用属性 - Create generic property in interface 为什么不能从具有通用参数的类型转换为通用接口,该类型是接口中通用参数的子类型? - Why can I not cast to a generic interface from a type with a generic argument which is a subtype of the generic argument in my interface? 如何使用反射来查找开放泛型类型上的哪个开放泛型参数实现泛型接口? - How can I use reflection to find which open generic argument on an open generic type implements a generic interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM