0

这是我的 DAL 的一部分。

数据上下文接口:

public interface IDataContextFactory
{
    System.Data.Linq.DataContext Context { get; }
    void SaveAll();
}

这是我的数据上下文类,其中包含生成的类:

partial class InternetRailwayTicketSalesDataContext: DataContext, IDataContextFactory
{
    public System.Data.Linq.DataContext Context
    {
        get { return this; }
    }

    public void SaveAll()
    {
        this.SubmitChanges();
    } 
}

这是我的存储库界面:

public interface IRepository<T> where T : class
{
    /// <summary>
    /// Return all instances of type T.
    /// </summary>
    /// <returns></returns>
    IEnumerable<T> GetAll();
}

这是我的存储库接口实现:

public class Repository<T> : IRepository<T> where T : class
{
    protected IDataContextFactory _dataContextFactory;

    /// <summary>
    /// Return all instances of type T.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<T> GetAll()
    {
        return GetTable;
    }

    private System.Data.Linq.Table<T> GetTable
    {
        get { return _dataContextFactory.Context.GetTable<T>(); }
    }
}

这是具体存储库类的接口:

interface IPasswayRepository
{ 
    bool IsPasswayExists(int id);
}

最后是一个具体的存储库类实现:

class PasswayRepository:Repository<Passway>, IPasswayRepository
{
    public PasswayRepository(IDataContextFactory context)
        : base(context)    
    {                    
    }

    public bool IsPasswayExists(int id)
    {
        if (GetAll().Where(pass => pass.Id == id && pass.IsDeleted == false).Count() > 0)
            return true;
        else
            return false;           
    }   
}

你能给我一个如何测试 IsPasswayExists 方法的例子吗?(如果您愿意,可以使用任何模拟框架)。

4

0 回答 0