分离接口并使其更通用



我有一个接口来定义我的记录模型

public interface IStockItem
{
    string Code { get; set; }
    string Description { get; set; }
    decimal FreeStock { get; set; }
}

最好把动作放到另一个界面吗?

public interface IStockExport
{
    IEnumerable<IStockItem> GetAll();
    IEnumerable<IStockItem> GetStockByCode(string code);
    decimal GetFreeStock(string code);
}
public interface IStockImport
{
    void CreateItem<IStockItem>;
}

是否有更好的方法来做到这一点,使其更通用?所以我可以与其他记录模型共享操作接口?

其他记录模型是SalesOrder, Customer, Address。

总体思想是一个ImportExport程序,它将通过API在许多不同的帐户包中创建Export销售订单。

这是一种常见的模式,称为Repository Pattern .

如果你想沿着这条路走,你应该创建一个基本接口,Repository<T>,例如:

public interface IRepository<T>
{
    void Insert(T entity);
    void Delete(T entity);
    IEnumerable<T> SearchFor(Func<T, bool> predicate);
    IEnumerable<T> GetAll();
    T GetById(int id);
}

你可以让你的IStockItem实现一个IEntity接口,这样它就可以为GetById()提供一个ID,例如:

public interface IEntity
{
    int ID { get; }
}

然后,您将通过声明实现类来实现数据类型(如StockItem)的存储库。它可能像这样开始:

public class Repository<T> : IRepository<T> where T : class, IEntity
{
    protected Table<T> DataTable;
    public Repository(DataContext dataContext)
    {
        DataTable = dataContext.GetTable<T>();
    }
    ...

想要获取库存项目的存储库的代码可能看起来像这样:

using (var dataContext = new StockItemDataContext())
{
    var StockItemRepository = new Repository<IStockItem>(dataContext);
    ...

对于您想要的,这可能有点过分,但这是一般的方法。

详情请参阅这篇优秀的博客文章。

下面是您如何开始为您的案例实现此模式的方法:

public interface IRepository<T>
{
    void Insert(T entity);
    void Delete(T entity);
    IEnumerable<T> SearchFor(Func<T, bool> predicate);
    IEnumerable<T> GetAll();
    T GetByCode(string code);
}
public interface IStockItem: IEntity
{
    string Description { get; set; }
    decimal FreeStock { get; set; }
}
public sealed class StockItem: IStockItem
{
    public string Code { get; set; }
    public string Description { get; set; }
    public decimal FreeStock { get; set; }
}
public interface IEntity
{
    string Code { get; }
}
public sealed class MyLowLevelDataAccess
{
    public StockItem FindStockItem(string code)
    {
        return null; // Call your API here.
    }
    public void DeleteStockItem(string code)
    {
        // Call your API here.
    }
    public void InsertStockItem(StockItem item)
    {
        // Call your API here.
    }
    public IEnumerable<StockItem> FindAllItems()
    {
        return FindItemsMatching(x => true);
    }
    public IEnumerable<StockItem> FindItemsMatching(Func<StockItem, bool> predicate)
    {
        return null; // Call your API here and return all items matching the predicate.
    }
}
public sealed class StockRepository: IRepository<StockItem>
{
    private readonly MyLowLevelDataAccess _dataAccess;
    public StockRepository(MyLowLevelDataAccess dataAccess)
    {
        _dataAccess = dataAccess;
    }
    public void Insert(StockItem entity)
    {
        _dataAccess.InsertStockItem(entity);
    }
    public void Delete(StockItem entity)
    {
        _dataAccess.DeleteStockItem(entity.Code);
    }
    public IEnumerable<StockItem> SearchFor(Func<StockItem, bool> predicate)
    {
        return _dataAccess.FindItemsMatching(predicate);
    }
    public IEnumerable<StockItem> GetAll()
    {
        return _dataAccess.FindAllItems();
    }
    public StockItem GetByCode(string code)
    {
        return _dataAccess.FindStockItem(code);
    }
}

您也可以使用通用接口:

public interface IRecordExport<T> where T : IRecordBase
{
  IEnumerable<T> GetAll();
  IEnumerable<T> GetOneByCode(string code);
  decimal GetFree(string code);
}
public interface IRecordImport<T> where T : IRecordBase
{
  void CreateItem<T>();
}

可以,但可能没有必要。基于方法的类的接口最好用于与实现相关的多态性。

在您的情况下,似乎您想要的是能够共享公共功能(基于IStockExport接口),但也提供多态创建机制(基于IStockImport)。

我建议你为IStockExport实现一个抽象基类,它可以继承所有不同类型的IStockItem(由于公共接口),然后派生类应该实现IStockExport,因为每个Create<IStockItem>()实现将是不同的,但由于共同的行为(总是返回IStockItem对象),可以以相同的方式使用。

最新更新