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.
Something like this?
A few things to note:
ValidateNoProductsseems suspect to me if it also exists on theProductversion of this interface.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 yourAccountServiceand other services which implement it also add methods of their own. That would seem like a more viable abstraction, as opposed to trying to fit a square peg into a round hole with incorrect abstractions.