Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a generic interface

Tags:

c#

I have the following interface called 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

Thanks for all the suggestions. The one thing I still have a problem with is AccountDetail and AccountSummary classes. 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. IS that possible?

Update 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. 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.

like image 361
Samantha J T Star Avatar asked Dec 06 '22 17:12

Samantha J T Star


2 Answers

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).

like image 85
Justin Niessner Avatar answered Dec 26 '22 18:12

Justin Niessner


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);
    ...
}
like image 34
SLaks Avatar answered Dec 26 '22 18:12

SLaks