C# 我应该使用数据库模拟进行单元测试吗

C# 我应该使用数据库模拟进行单元测试吗,c#,unit-testing,C#,Unit Testing,我目前正在为一个应用程序编写各种单元测试 现在,我确实有测试来检查代码是否正常工作,确实如此。但我是否也应该模拟,例如: 数据库不可用 数据库查询返回null 执行数据库查询需要很长时间 我知道这不是一个编码问题,但我想对它有一些一般性的想法 如果需要,我想到了以下方法: SettingsUnavailableMock.Setup(x => x.PageRepository.All()).Throws(new Exception()); SettingsUnavailableMock.

我目前正在为一个应用程序编写各种单元测试

现在,我确实有测试来检查代码是否正常工作,确实如此。但我是否也应该模拟,例如:

  • 数据库不可用
  • 数据库查询返回null
  • 执行数据库查询需要很长时间
我知道这不是一个编码问题,但我想对它有一些一般性的想法

如果需要,我想到了以下方法:

SettingsUnavailableMock.Setup(x => x.PageRepository.All()).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Get(It.IsAny<int>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Get(It.IsAny<string>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Refresh(It.IsAny<Page>())).Throws(new Exception());
SettingsUnavailableMock.Setup(x => x.PageRepository.Save()).Throws(new Exception());
settingsunavaailablemock.Setup(x=>x.PageRepository.All()).Throws(newexception());
settingsunavaailablemock.Setup(x=>x.PageRepository.Get(It.IsAny()).Throws(newexception());
settingsunavaailablemock.Setup(x=>x.PageRepository.Get(It.IsAny()).Throws(newexception());
settingsunavaailablemock.Setup(x=>x.PageRepository.Refresh(It.IsAny()).Throws(newexception());
SettingsUnavailableMock.Setup(x=>x.PageRepository.Save()).Throws(新异常());
当然,为所有存储库添加


然后在我的测试类中,我可以选择我想要使用的Mock。

这实际上取决于与数据库交互的代码在做什么

单元测试背后的理念是单独测试一个类。它的所有外部依赖项都应该被模拟

但是,您也可以检查类是否正确使用其依赖项。这将是一个互动测试

最后,如果一切正常,你要检查整个系统是否能协同工作。这是一个集成测试。

请注意,有些库允许您更轻松地执行集成测试,例如

公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共接口存储库
{
列出LoadProducts();
}
公共类ProductRepository:IPProductRepository
{
公共列表加载产品()
{
//返回产品列表的数据库代码
返回新列表();
}
}
公共类存储统计生成器
{
专用只读IPProductRepository\u存储库;
公共存储统计生成器(IPProductRepository存储库)
{
_存储库=存储库;
}
public int computeNumber of Products()
{
var products=_repository.LoadProducts();
退货。盘点;
}
}
给定以下类,您可能需要测试不同类型的东西

[TestFixture]
public class StorageStatisticsGeneratorTests
{
    private Mock<IProductRepository> _productRepository;
    private StorageStatisticsGenerator _statisticGenerator;

    [SetUp]
    public void Setup()
    {
        _productRepository = new Mock<IProductRepository>();
        _statisticGenerator = new StorageStatisticsGenerator(_productRepository.Object);
    }


    // In this test we test if the statistic generator works correctly
    // This is a UNIT TEST
    [Test]
    public void ComputeNumberOfProducts_Should_Returns_TheCorrectCount()
    {
        // Arrange
        _productRepository.Setup(p => p.LoadProducts()).Returns(new List<Product>
        {
            new Product(), new Product(), new Product()
        });

        // Act
        int result = _statisticGenerator.ComputeNumberOfProducts();

        // Assert
        Assert.AreEqual(3, result);
    }


    // In this test we test if the statistic generator use the repository as expected
    // This is an INTERACTION TEST, you could check corner case using "real life data"
    [Test]
    public void ComputeNumberOfProducts_Should_Use_The_Product_Repository()
    {
        // Arrange
        _productRepository.Setup(p => p.LoadProducts()).Returns(new List<Product>
        {
            new Product()
        });

        // Act
        _statisticGenerator.ComputeNumberOfProducts();

        // Assert
        _productRepository.Verify(p => p.LoadProducts());
    }



    // In this test we use the real repository this is an INTEGRATION TEST
    // You can flag this kind of slow test to run only during the night for instabce
    [Test, Category("Nightly")]
    public void ComputeNumberOfProducts_Should_Correctly_Integrate_With_ProductRepository()
    {
        // Arrange
        _statisticGenerator = new StorageStatisticsGenerator(new ProductRepository());

        // Act
        _statisticGenerator.ComputeNumberOfProducts();

        // Assert
        _productRepository.Verify(p => p.LoadProducts());
    }
}
[TestFixture]
公共类StorageStatisticsGeneratorTests
{
私有模拟存储库;
专用存储统计生成器\u统计生成器;
[设置]
公共作废设置()
{
_productRepository=new Mock();
_statisticGenerator=新的StorageStatisticsGenerator(\u productRepository.Object);
}
//在本测试中,我们测试统计生成器是否正常工作
//这是一个单元测试
[测试]
public void computeNumber of Products\u应\u返回\u正确计数()
{
//安排
_productRepository.Setup(p=>p.LoadProducts()).Returns(新列表
{
新产品(),新产品(),新产品()
});
//表演
int result=_statisticGenerator.ComputeNumberOfProducts();
//断言
断言。等于(3,结果);
}
//在本测试中,我们测试统计生成器是否按预期使用存储库
//这是一个交互测试,您可以使用“真实数据”检查角落案例
[测试]
public void ComputeNumber of Products\u应\u使用\u Product\u存储库()
{
//安排
_productRepository.Setup(p=>p.LoadProducts()).Returns(新列表
{
新产品()
});
//表演
_statisticGenerator.ComputeNumber of Products();
//断言
_验证(p=>p.LoadProducts());
}
//在这个测试中,我们使用真实的存储库,这是一个集成测试
//您可以将此类慢速测试标记为仅在instabce的夜间运行
[测试,类别(“夜间”)]
public void computeNumber of Products\u应\u正确\u与\u ProductRepository()集成
{
//安排
_statisticGenerator=新存储StatisticsGenerator(新产品存储库());
//表演
_statisticGenerator.ComputeNumber of Products();
//断言
_验证(p=>p.LoadProducts());
}
}

如果你想知道更多,你可以阅读

理想情况下,你应该测试以上所有内容;然而,这取决于你的情况。就我个人而言,我总是测试我能测试的一切

需要很长时间的查询是非常现实的。 数据库不可用也是非常现实的

返回null的查询我不太确定;然而,如果这是一个现实的场景,那么无论如何都要对它进行存根和测试

更新-根据评论,我认为这将是一个好的补充

public interface IRepository<T> where T : IRepositoryEntry, new()
{
    event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    event EventHandler<RepositoryOperationEventArgs> DeleteEvent;
    IList<String> PrimaryKeys { get; }

    void Insert(T Entry);
    void Update(T Entry);
    void Delete(Predicate<T> predicate);
    bool Exists(Predicate<T> predicate);
    T Retrieve(Predicate<T> predicate);

    IEnumerable<T> RetrieveAll();

}

public interface IRepositoryEntry
{
    IList<String> GetPrimaryKeys();
}

public class OracleRepository
{

    const string ConnectionString = "*"

    public static IDbConnection GetIDbConnection()
    {
        IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
        return connection;
    }
    public IDbConnection GetConnection()
    {
        IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
        return connection;
    }

}

public  class OracleRepository<T> : OracleRepository, IDisposable, IRepository<T> where T : RepositoryEntryBase, IRepositoryEntry, new()
{


    /// <summary>
    /// Gets all property names from a type.
    /// </summary>
    /// <returns>IEnumerable of strings</returns>
    static IEnumerable<String> GetEntryPropertyNames(Type type)
    {
        foreach (var propInfo in type.GetProperties())
            yield return propInfo.Name;
    }

    public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

    #region Properties
    public IList<String> PrimaryKeys
    {
        get
        {
            return primaryKeys.AsReadOnly();
        }
        private set
        {
            primaryKeys = new List<String>(value);
        }
    }
    public IList<String> Properties { get; private set; }

    public String InsertText { get; private set; }
    public String UpdateText { get; private set; }
    public String DeleteText { get; private set; }
    public String SelectText { get; private set; }
    #endregion

    #region Fields
    IDbConnection connection;
    IDbTransaction transaction;
    List<String> primaryKeys;
    #endregion

    #region Constructors
    public OracleRepository()
    {
        PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
        Properties = new List<String>(GetEntryPropertyNames(typeof(T))).AsReadOnly();
        InsertText = GenerateInsertText();
        UpdateText = GenerateUpdateText();
        SelectText = GenerateSelectText();
        DeleteText = GenerateDeleteText();
        connection = GetConnection();
    }
    #endregion

    #region Interface Implementations
    public void Insert(T Entry)
    {
        Insert(connection, Entry);
    }
    public void Update(T Entry)
    {
        Update(connection, Entry);
    }
    public void Delete(Predicate<T> predicate)
    {
        Delete(connection, predicate);
    }

    public T Retrieve(Predicate<T> predicate)
    {
        return Retrieve(connection, predicate);
    }
    public bool Exists( Predicate<T> predicate)
    {

        return Exists(connection, predicate);
    }

    public IEnumerable<T> RetrieveAll()
    {
        return RetrieveAll(connection);
    }

    public void Dispose()
    {
        if (transaction != null)
            transaction.Dispose();
        connection.Dispose();
    }
    #endregion

    #region Public Methods

    public void StartTransaction()
    {
        if (transaction != null)
            throw new InvalidOperationException("Transaction is already set. Please Rollback or commit transaction");
        transaction = connection.BeginTransaction();
    }
    public void CommitTransaction()
    {
        transaction.Commit();
        transaction.Dispose();
        transaction = null;
    }
    public void RollbackTransaction()
    {

        transaction.Rollback();
        transaction.Dispose();
        transaction = null;
    }


    public void Insert(IDbConnection connection, T Entry)
    {
        Type type = typeof(T);
        List<Object> args = new List<Object>();
        for (int i = 0; i < Properties.Count; i++)
            args.Add(type.GetProperty(Properties[i]).GetValue(Entry));
        connection.NonQuery(InsertText, args.ToArray());
        if (InsertEvent != null)
            InsertEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });
    }

    public void Update(IDbConnection connection, T Entry)
    {
        Type type = typeof(T);
        List<Object> args = new List<Object>();

        foreach (var propertyName in Properties.Where(p => !PrimaryKeys.Any(k => k == p)))
            args.Add(type.GetProperty(propertyName).GetValue(Entry));
        foreach (var PropertyName in PrimaryKeys)
            args.Add(type.GetProperty(PropertyName).GetValue(Entry));

        connection.NonQuery(UpdateText, args.ToArray());
        if (UpdateEvent != null)
            UpdateEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });

    }


    public void Delete(IDbConnection connection, Predicate<T> predicate)
    {
        var entryList = RetrieveAll(connection).Where(new Func<T, bool>(predicate));
        Type type = typeof(T);

        foreach(var entry in entryList)
        {
            List<Object> args = new List<Object>();
            foreach (var PropertyName in PrimaryKeys)
                args.Add(type.GetProperty(PropertyName).GetValue(entry));
            connection.NonQuery(DeleteText, args.ToArray());
            if (DeleteEvent != null)
                DeleteEvent(this, new OracleRepositoryOperationEventArgs() { Entry = null, Transaction = (transaction != null) });

        }
    }

    public T Retrieve(IDbConnection connection, Predicate<T> predicate)
    {
        return RetrieveAll(connection).FirstOrDefault(new Func<T, bool>(predicate));
    }
    public bool Exists(IDbConnection connection, Predicate<T> predicate)
    {
        return RetrieveAll(connection).Any(new Func<T, bool>(predicate));
    }

    public IEnumerable<T> RetrieveAll(IDbConnection connection)
    {
        List<T> collection = new List<T>();
        var result = connection.Query(SelectText);
        foreach (var row in result.Tuples)
            collection.Add(RepositoryEntryBase.FromPlexQueryResultTuple(new T(), row) as T);
        return collection;
    }
    #endregion

    #region Private Methods

    String GenerateInsertText()
    {
        String statement = "INSERT INTO {0}({1}) VALUES ({2})";
        //Do first entry here becasse its unique input.
        String columnNames = Properties.First();

        String delimiter = ", ";
        String bph = ":a"; 

        String placeHolders = bph + 0;

        //Start @ 1 since first entry is already done
        for (int i = 1; i < Properties.Count; i++)
        {
            columnNames += delimiter + Properties[i];
            placeHolders += delimiter + bph + i;
        }

        statement = String.Format(statement, typeof(T).Name, columnNames, placeHolders);
        return statement;
    }

    String GenerateUpdateText()
    {
        String bph = ":a"; 
        String cvpTemplate = "{0} = {1}";
        String statement = "UPDATE {0} SET {1} WHERE {2}";

        //Can only set Cols that are not a primary Keys, Get those Columns
        var Settables = Properties.Where(p => !PrimaryKeys.Any(k => k == p)).ToList();

        String cvp = String.Format(cvpTemplate, Settables.First() , bph + 0 );
        String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + Settables.Count);

        //These are the values to be set | Start @ 1 since first entry is done above.
        for (int i = 1; i < Settables.Count; i++) 
            cvp += ", " + String.Format(cvpTemplate, Settables[i], bph + i);

        //This creates the conditions under which the values are set. | Start @ 1 since first entry is done above.
        for (int i = Settables.Count + 1; i < Properties.Count; i++)
            condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i - Settables.Count], bph + i);

        statement = String.Format(statement, typeof(T).Name, cvp, condition);
        return statement;
    }

    String GenerateDeleteText()
    {
        String bph = ":a";
        String cvpTemplate = "{0} = {1}";
        String statement = "DELETE FROM {0} WHERE {1}";
        String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + 0);

        for (int i =1; i < PrimaryKeys.Count; i++)
            condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i], bph + i);

        statement = String.Format(statement, typeof(T).Name, condition);
        return statement;
    }

    String GenerateSelectText()
    {
        String statement = "SELECT * FROM {0}";
        statement = String.Format(statement, typeof(T).Name);
        return statement;
    }
    #endregion
}
公共接口IRepository,其中T:IRepositoryEntry,new()
{
事件处理程序插入事件;
事件处理程序UpdateEvent;
事件处理程序DeleteEvent;
IList主密钥{get;}
无效插入(T条目);
无效更新(T条目);
无效删除(谓词);
bool存在(谓词);
T检索(谓词);
IEnumerable RetrieveAll();
}
公共接口IRepositoryEntry
{
IList GetPrimaryKeys();
}
公共类或高级公寓
{
常量字符串ConnectionString=“*”
公共静态IDbConnection GetIDbConnection()
{
IDbConnection=newOracleConnection(ConnectionString).OpenConnection();
回路连接;
}
公共IDbConnection GetConnection()
{
IDbConnection=newOracleConnection(ConnectionString).OpenConnection();
回路连接;
}
}
公共类OracleRepository:OracleRepository、IDisposable、IRepository whe
public interface IRepository<T> where T : IRepositoryEntry, new()
{
    event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    event EventHandler<RepositoryOperationEventArgs> DeleteEvent;
    IList<String> PrimaryKeys { get; }

    void Insert(T Entry);
    void Update(T Entry);
    void Delete(Predicate<T> predicate);
    bool Exists(Predicate<T> predicate);
    T Retrieve(Predicate<T> predicate);

    IEnumerable<T> RetrieveAll();

}

public interface IRepositoryEntry
{
    IList<String> GetPrimaryKeys();
}

public class OracleRepository
{

    const string ConnectionString = "*"

    public static IDbConnection GetIDbConnection()
    {
        IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
        return connection;
    }
    public IDbConnection GetConnection()
    {
        IDbConnection connection = new OracleConnection(ConnectionString).OpenConnection();
        return connection;
    }

}

public  class OracleRepository<T> : OracleRepository, IDisposable, IRepository<T> where T : RepositoryEntryBase, IRepositoryEntry, new()
{


    /// <summary>
    /// Gets all property names from a type.
    /// </summary>
    /// <returns>IEnumerable of strings</returns>
    static IEnumerable<String> GetEntryPropertyNames(Type type)
    {
        foreach (var propInfo in type.GetProperties())
            yield return propInfo.Name;
    }

    public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

    #region Properties
    public IList<String> PrimaryKeys
    {
        get
        {
            return primaryKeys.AsReadOnly();
        }
        private set
        {
            primaryKeys = new List<String>(value);
        }
    }
    public IList<String> Properties { get; private set; }

    public String InsertText { get; private set; }
    public String UpdateText { get; private set; }
    public String DeleteText { get; private set; }
    public String SelectText { get; private set; }
    #endregion

    #region Fields
    IDbConnection connection;
    IDbTransaction transaction;
    List<String> primaryKeys;
    #endregion

    #region Constructors
    public OracleRepository()
    {
        PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
        Properties = new List<String>(GetEntryPropertyNames(typeof(T))).AsReadOnly();
        InsertText = GenerateInsertText();
        UpdateText = GenerateUpdateText();
        SelectText = GenerateSelectText();
        DeleteText = GenerateDeleteText();
        connection = GetConnection();
    }
    #endregion

    #region Interface Implementations
    public void Insert(T Entry)
    {
        Insert(connection, Entry);
    }
    public void Update(T Entry)
    {
        Update(connection, Entry);
    }
    public void Delete(Predicate<T> predicate)
    {
        Delete(connection, predicate);
    }

    public T Retrieve(Predicate<T> predicate)
    {
        return Retrieve(connection, predicate);
    }
    public bool Exists( Predicate<T> predicate)
    {

        return Exists(connection, predicate);
    }

    public IEnumerable<T> RetrieveAll()
    {
        return RetrieveAll(connection);
    }

    public void Dispose()
    {
        if (transaction != null)
            transaction.Dispose();
        connection.Dispose();
    }
    #endregion

    #region Public Methods

    public void StartTransaction()
    {
        if (transaction != null)
            throw new InvalidOperationException("Transaction is already set. Please Rollback or commit transaction");
        transaction = connection.BeginTransaction();
    }
    public void CommitTransaction()
    {
        transaction.Commit();
        transaction.Dispose();
        transaction = null;
    }
    public void RollbackTransaction()
    {

        transaction.Rollback();
        transaction.Dispose();
        transaction = null;
    }


    public void Insert(IDbConnection connection, T Entry)
    {
        Type type = typeof(T);
        List<Object> args = new List<Object>();
        for (int i = 0; i < Properties.Count; i++)
            args.Add(type.GetProperty(Properties[i]).GetValue(Entry));
        connection.NonQuery(InsertText, args.ToArray());
        if (InsertEvent != null)
            InsertEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });
    }

    public void Update(IDbConnection connection, T Entry)
    {
        Type type = typeof(T);
        List<Object> args = new List<Object>();

        foreach (var propertyName in Properties.Where(p => !PrimaryKeys.Any(k => k == p)))
            args.Add(type.GetProperty(propertyName).GetValue(Entry));
        foreach (var PropertyName in PrimaryKeys)
            args.Add(type.GetProperty(PropertyName).GetValue(Entry));

        connection.NonQuery(UpdateText, args.ToArray());
        if (UpdateEvent != null)
            UpdateEvent(this, new OracleRepositoryOperationEventArgs() { Entry = Entry, Transaction = (transaction != null) });

    }


    public void Delete(IDbConnection connection, Predicate<T> predicate)
    {
        var entryList = RetrieveAll(connection).Where(new Func<T, bool>(predicate));
        Type type = typeof(T);

        foreach(var entry in entryList)
        {
            List<Object> args = new List<Object>();
            foreach (var PropertyName in PrimaryKeys)
                args.Add(type.GetProperty(PropertyName).GetValue(entry));
            connection.NonQuery(DeleteText, args.ToArray());
            if (DeleteEvent != null)
                DeleteEvent(this, new OracleRepositoryOperationEventArgs() { Entry = null, Transaction = (transaction != null) });

        }
    }

    public T Retrieve(IDbConnection connection, Predicate<T> predicate)
    {
        return RetrieveAll(connection).FirstOrDefault(new Func<T, bool>(predicate));
    }
    public bool Exists(IDbConnection connection, Predicate<T> predicate)
    {
        return RetrieveAll(connection).Any(new Func<T, bool>(predicate));
    }

    public IEnumerable<T> RetrieveAll(IDbConnection connection)
    {
        List<T> collection = new List<T>();
        var result = connection.Query(SelectText);
        foreach (var row in result.Tuples)
            collection.Add(RepositoryEntryBase.FromPlexQueryResultTuple(new T(), row) as T);
        return collection;
    }
    #endregion

    #region Private Methods

    String GenerateInsertText()
    {
        String statement = "INSERT INTO {0}({1}) VALUES ({2})";
        //Do first entry here becasse its unique input.
        String columnNames = Properties.First();

        String delimiter = ", ";
        String bph = ":a"; 

        String placeHolders = bph + 0;

        //Start @ 1 since first entry is already done
        for (int i = 1; i < Properties.Count; i++)
        {
            columnNames += delimiter + Properties[i];
            placeHolders += delimiter + bph + i;
        }

        statement = String.Format(statement, typeof(T).Name, columnNames, placeHolders);
        return statement;
    }

    String GenerateUpdateText()
    {
        String bph = ":a"; 
        String cvpTemplate = "{0} = {1}";
        String statement = "UPDATE {0} SET {1} WHERE {2}";

        //Can only set Cols that are not a primary Keys, Get those Columns
        var Settables = Properties.Where(p => !PrimaryKeys.Any(k => k == p)).ToList();

        String cvp = String.Format(cvpTemplate, Settables.First() , bph + 0 );
        String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + Settables.Count);

        //These are the values to be set | Start @ 1 since first entry is done above.
        for (int i = 1; i < Settables.Count; i++) 
            cvp += ", " + String.Format(cvpTemplate, Settables[i], bph + i);

        //This creates the conditions under which the values are set. | Start @ 1 since first entry is done above.
        for (int i = Settables.Count + 1; i < Properties.Count; i++)
            condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i - Settables.Count], bph + i);

        statement = String.Format(statement, typeof(T).Name, cvp, condition);
        return statement;
    }

    String GenerateDeleteText()
    {
        String bph = ":a";
        String cvpTemplate = "{0} = {1}";
        String statement = "DELETE FROM {0} WHERE {1}";
        String condition = String.Format(cvpTemplate, PrimaryKeys.First(), bph + 0);

        for (int i =1; i < PrimaryKeys.Count; i++)
            condition += ", " + String.Format(cvpTemplate, PrimaryKeys[i], bph + i);

        statement = String.Format(statement, typeof(T).Name, condition);
        return statement;
    }

    String GenerateSelectText()
    {
        String statement = "SELECT * FROM {0}";
        statement = String.Format(statement, typeof(T).Name);
        return statement;
    }
    #endregion
}
public class APPS : RepositoryEntryBase, IRepositoryEntry
{
    public int APP_ID { get; set; }
    public string AUTH_KEY { get; set; }
    public string TITLE { get; set; }
    public string DESCRIPTION { get; set; }
    public int IS_CLIENT_CUSTOM_APP { get; set; }


    public APPS() : base() {
        primaryKeys.Add("APP_ID");
    }
    public APPS(PlexQueryResultTuple plexTuple) : base(plexTuple) { }
}

public class RepositoryEntryBase
{

    public static RepositoryEntryBase FromPlexQueryResultTuple( RepositoryEntryBase reb, PlexQueryResultTuple plexTuple)
    {

        if (plexTuple.parent == null)
            throw new NotSupportedException("This Operation is Not supported by this PlexTuple.");

        Type type = reb.GetType();
        var pInfo = type.GetProperties();
        PlexQueryResult result = plexTuple.parent;

        foreach (var p in pInfo)
        {
            int index = result.Tuples.IndexOf(plexTuple);

            if (result[p.Name, index] == null)
                continue;

            var conversationType = Nullable.GetUnderlyingType(p.PropertyType) ?? p.PropertyType;
            object value = Convert.ChangeType(result[p.Name, index], (result[p.Name, index] != null)?conversationType: p.PropertyType);
            p.SetValue(reb, value);
        }
        return reb;
    }

    protected IList<String> primaryKeys;

    public RepositoryEntryBase() 
    {
        primaryKeys = new List<String>();
    }
    public RepositoryEntryBase(PlexQueryResultTuple plexTuple) : this()
    {
        FromPlexQueryResultTuple(this, plexTuple);
    }


    public IList<String> GetPrimaryKeys()
    {
        return primaryKeys;
    }
}
public class InMemoryRepository<T> : IRepository<T> where T :  IRepositoryEntry, new()
{
    //RepositoryEntryBase,
    public event EventHandler<RepositoryOperationEventArgs> InsertEvent;
    public event EventHandler<RepositoryOperationEventArgs> UpdateEvent;
    public event EventHandler<RepositoryOperationEventArgs> DeleteEvent;

    public IList<String> PrimaryKeys { get; protected set; }
    List<T> data;
    public InMemoryRepository() {
        PrimaryKeys = new List<String>(new T().GetPrimaryKeys());
        data = new List<T>();
    }

    public void  Insert(T Entry){
        if(Get(Entry) != null)
            throw new Exception("Duplicate Entry - Identical Key already exists");
        data.Add(Entry);
        if (InsertEvent != null)
            InsertEvent(this, new RepositoryOperationEventArgs() { Entry = Entry });
    }

    public void Update(T Entry){
        var obj = Get(Entry);
        if (obj == null)
            throw new Exception("Object does not exist");
        obj = Entry;
        if (UpdateEvent != null)
            UpdateEvent(this, new RepositoryOperationEventArgs() { Entry = obj });
    }

    public void Delete(Predicate<T> predicate)
    {
        data.RemoveAll(predicate);
        if (DeleteEvent != null)
            DeleteEvent(this, new RepositoryOperationEventArgs() { Entry = null });
    }

    public bool Exists(Predicate<T> predicate)
    {
        return data.Exists(predicate);
    }

    public T Retrieve(Predicate<T> predicate)
    {
        return data.FirstOrDefault(new Func<T, bool>(predicate));
    }

    public IEnumerable<T> RetrieveAll()
    {
        return data.ToArray();
    }

    T Get(T Entry)
    {
        //Returns Entry based on Identical PrimaryKeys
        Type entryType = typeof(T);
        var KeyPropertyInfo = entryType.GetProperties().Where(p => PrimaryKeys.Any(p2 => p2 == p.Name));
        foreach (var v in data)
        {
            //Assume the objects are identical by default to prevent false positives.
            Boolean AlreadyExists = true;
            foreach (var property in KeyPropertyInfo)
                if (!property.GetValue(v).Equals(property.GetValue(Entry)))
                    AlreadyExists = false;
            if (AlreadyExists)
                return v;
        }
        return default(T);
    }
}