Can i create a common interface

I have the following IAccountService interface. I also have the same interfaces for the 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 have created some common methods, but I am wondering if I can create a common interface. If so, how would I call it and how could I change above to make it general. I am currently using this interface as follows:

 public class AccountService : BaseService, IAccountService 

Update 1

Thanks for all the suggestions. The only thing I came across was the AccountDetail and AccountSummary classes. I think it should be subclasses. But how can I handle this name? I would need to take the class name, add the part, and then use it in the interface. Is it possible?

Update 2

Here is an example of detailed 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 think that they probably should not be part of the model repository. Maybe they should be somewhere else.

Regarding comments ref. There is a link because in my controller I call the following method:

 _account.ShowDetails(ref runTime); 

The result of ShowDetails is a list of details, and the runTime link is updated to reflect the time it takes to run the report.

+4
source share
7 answers

Something like that?

 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 notes:

  • It seems that there are three different types that need to be generalized, so there are three corresponding types of parameters. If the last two are incorrect, continue and update or let me know, and I can help with this.
  • I renamed the parameters to "entity" as a more general term. You can choose which name makes sense to you.
  • Are the last two method names universal enough for this? ValidateNoProducts seems suspicious to me if it also exists in the Product version of this interface.

In general, although you can make one common interface, one important question that you need to ask yourself is that it is really an abstraction. Some of these methods may be more intuitively specific to a particular type and belong to a different type of service.

To this end, you can create an IService<> with most of these methods, and then your AccountService and other services that implement it also add their own methods. This would seem to be a more viable abstraction, rather than an attempt to insert a square anchor into a round hole with incorrect abstractions.

+1
source

You can 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 into a separate place (perhaps some sort of matching class).

+5
source

You can create a generic interface just like you do a generic class:

 public interface IRepository<T> { void AddOrUpdate(T item); bool TableExists { get; } IEnumerable<T> All { get; } ... IEnumerable<Detail<T>> GetDetails(string runTime); ... } 
+4
source

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 will need 3 types or have a separate IAccountService interface that handles the summary and details methods themselves.

+2
source
 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 as follows:

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

You can define it like this:

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

And use it like this:

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

Source: https://habr.com/ru/post/1387204/


All Articles