我有以下名为IAccountService的界面。我也有完全相同的产品和包装等接口。
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);
}
我创建了一些通用方法,但我想知道我是否也可以创建一个通用接口。如果是这样,我将如何调用它,如何更改上面的内容使其成为通用的。目前我使用这个界面如下:
public class AccountService : BaseService, IAccountService
更新1
感谢所有建议。我仍有问题的一件事是AccountDetail和AccountSummary类。我想也许这些应该是子类。但是,我怎么能处理那些命名呢?我必须取类名,追加Detail然后在界面中使用它。有可能吗?
更新2
以下是详细信息和摘要类的示例:
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; }
}
上述类用于报告。我想他们可能不应该成为模型库的一部分。也许他们应该在另一个地方。
关于ref评论。引用是因为在我的控制器中我调用了以下方法:
_account.ShowDetails(ref runTime);
ShowDetails的输出是一个详细信息列表,runTime引用随着运行报告所花费的时间而更新。
答案 0 :(得分:5)
你总是可以这样做:
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>
对于Detail / Summary方法,我会把它们分成一个单独的位置(可能是某种mapper类)。
答案 1 :(得分:4)
您可以按照制作泛型类的方式创建通用接口:
public interface IRepository<T> {
void AddOrUpdate(T item);
bool TableExists { get; }
IEnumerable<T> All { get; }
...
IEnumerable<Detail<T>> GetDetails(string runTime);
...
}
答案 2 :(得分:2)
这应该适合你:
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>
请注意,您需要3种类型,或者具有单独的IAccountService接口来处理摘要和详细信息方法本身。
答案 3 :(得分:1)
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);
}
并像这样使用它:
public class AccountService : BaseService, IService<Account>
答案 4 :(得分:1)
public interface IAccountService<T>
{
...
SomeDataType<T> SomeMethod(...);
...
}
答案 5 :(得分:1)
您可以这样定义:
public interface IService<T>
{
void AddOrUpdate(T entity);
T Get(T entity);
// etc
}
并像这样使用它:
public class AccountService : IService<Account>
{
void AddOrUpdate(Account entity);
Account Get(Account entity);
// etc
}
答案 6 :(得分:1)
这样的东西?
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);
}
有几点需要注意:
ValidateNoProducts
如果此界面的Product
版本中也存在IService<>
,我似乎对此感到怀疑。总而言之,虽然可以创建一个通用接口,但您需要问自己的一个重要问题是,这确实是正确的抽象。其中一些方法可能直观地更具特定类型,属于不同类型的服务。
为此,您可以使用大多数这些方法创建AccountService
,然后让您的{{1}}和其他实现它的服务也添加自己的方法。这似乎是一种更可行的抽象,而不是试图将方形钉固定在一个带有不正确抽象的圆孔中。