LCOV - code coverage report
Current view: top level - Cqrs.Sql/DataStores - SqlDataStore.cs Hit Total Coverage
Test: doc-coverage.info Lines: 2 10 20.0 %
Date: 2017-07-26

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="cdmdotnet Limited">
       4             : // //   Copyright cdmdotnet Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections;
      11             : using System.Collections.Generic;
      12             : using System.Data;
      13             : using System.Data.Linq;
      14             : using System.Linq;
      15             : using System.Linq.Expressions;
      16             : using Cqrs.DataStores;
      17             : 
      18             : namespace Cqrs.Sql.DataStores
      19             : {
      20             :         public class SqlDataStore<TEntity, TDbEntity> : IDataStore<TEntity>
      21             :                 where TDbEntity : class, new()
      22           0 :         {
      23             :                 internal DataContext DataContext { get; private set; }
      24             : 
      25             :                 internal Table<TDbEntity> DataTable { get; private set; }
      26             : 
      27             :                 internal IQueryable<TEntity> EntityQuery { get; private set; }
      28             : 
      29             :                 internal IQueryable<TDbEntity> DbEntityQuery { get; private set; }
      30             : 
      31             :                 internal IExpressionTreeConverter ExpressionConverter { get; set; }
      32             : 
      33           0 :                 public SqlDataStore(IExpressionTreeConverter expressionConverter, DataContext dataContext)
      34             :                 {
      35             :                         ExpressionConverter = expressionConverter;
      36             : 
      37             :                         DataContext = dataContext;
      38             :                         DataTable = DataContext.GetTable<TDbEntity>();
      39             :                         DbEntityQuery = DataTable;
      40             :                         EntityQuery = new List<TEntity>().AsQueryable();
      41             : 
      42             :                         switch (DataContext.Connection.State)
      43             :                         {
      44             :                                 case ConnectionState.Closed:
      45             :                                 case ConnectionState.Broken:
      46             :                                         DataContext.Connection.Open();
      47             :                                         DataContext.Transaction = DataContext.Connection.BeginTransaction();
      48             :                                         break;
      49             :                         }
      50             :                 }
      51             : 
      52             :                 internal SqlDataStore(DataContext dataContext, Table<TDbEntity> dataTable, IQueryable<TDbEntity> dbEntityQuery, IQueryable<TEntity> entityQuery)
      53             :                 {
      54             :                         DataContext = dataContext;
      55             :                         DataTable = dataTable;
      56             :                         DbEntityQuery = dbEntityQuery;
      57             :                         EntityQuery = entityQuery;
      58             :                 }
      59             : 
      60             :                 #region Implementation of IEnumerable
      61             : 
      62             :                 /// <summary>
      63             :                 /// Returns an enumerator that iterates through the collection.
      64             :                 /// </summary>
      65             :                 /// <returns>
      66             :                 /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      67             :                 /// </returns>
      68           1 :                 public IEnumerator<TDbEntity> GetEnumerator()
      69             :                 {
      70             :                         return DataTable.GetEnumerator();
      71             :                 }
      72             : 
      73             :                 /// <summary>
      74             :                 /// Returns an enumerator that iterates through the collection.
      75             :                 /// </summary>
      76             :                 /// <returns>
      77             :                 /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      78             :                 /// </returns>
      79             :                 IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
      80             :                 {
      81             :                         return EntityQuery.GetEnumerator();
      82             :                 }
      83             : 
      84             :                 /// <summary>
      85             :                 /// Returns an enumerator that iterates through a collection.
      86             :                 /// </summary>
      87             :                 /// <returns>
      88             :                 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
      89             :                 /// </returns>
      90             :                 IEnumerator IEnumerable.GetEnumerator()
      91             :                 {
      92             :                         return GetEnumerator();
      93             :                 }
      94             : 
      95             :                 #endregion
      96             : 
      97             :                 #region Implementation of IQueryable
      98             : 
      99             :                 /// <summary>
     100             :                 /// Gets the expression tree that is associated with the instance of <see cref="T:System.Linq.IQueryable"/>.
     101             :                 /// </summary>
     102             :                 /// <returns>
     103             :                 /// The <see cref="T:System.Linq.Expressions.Expression"/> that is associated with this instance of <see cref="T:System.Linq.IQueryable"/>.
     104             :                 /// </returns>
     105             :                 public Expression Expression
     106             :                 {
     107             :                         get { return EntityQuery.Expression; }
     108             :                 }
     109             : 
     110             :                 /// <summary>
     111             :                 /// Gets the type of the element(s) that are returned when the expression tree associated with this instance of <see cref="T:System.Linq.IQueryable"/> is executed.
     112             :                 /// </summary>
     113             :                 /// <returns>
     114             :                 /// A <see cref="T:System.Type"/> that represents the type of the element(s) that are returned when the expression tree associated with this object is executed.
     115             :                 /// </returns>
     116             :                 public Type ElementType
     117             :                 {
     118             :                         get { return EntityQuery.ElementType; }
     119             :                 }
     120             : 
     121             :                 /// <summary>
     122             :                 /// Gets the query provider that is associated with this data source.
     123             :                 /// </summary>
     124             :                 /// <returns>
     125             :                 /// The <see cref="T:System.Linq.IQueryProvider"/> that is associated with this data source.
     126             :                 /// </returns>
     127             :                 public IQueryProvider Provider
     128             :                 {
     129             :                         get { return EntityQuery.Provider; }
     130             :                 }
     131             : 
     132             :                 #endregion
     133             : 
     134             :                 #region Implementation of IDataStore<T>
     135             : 
     136           0 :                 public void Add(TEntity data)
     137             :                 {
     138             :                         var converted = Converters.ConvertTo<TDbEntity>(data);
     139             :                         DataTable.InsertOnSubmit(converted);
     140             :                 }
     141             : 
     142           0 :                 public void Add(IEnumerable<TEntity> data)
     143             :                 {
     144             :                         DataTable.InsertAllOnSubmit(data.Select(x => Converters.ConvertTo<TDbEntity>(x)));
     145             :                 }
     146             : 
     147           0 :                 public void Remove(TEntity data)
     148             :                 {
     149             :                         Destroy(data);
     150             :                 }
     151             : 
     152           0 :                 public void Destroy(TEntity data)
     153             :                 {
     154             :                         var converted = Converters.ConvertTo<TDbEntity>(data);
     155             :                         DataTable.DeleteOnSubmit(converted);
     156             :                 }
     157             : 
     158           0 :                 public void RemoveAll()
     159             :                 {
     160             :                         IList<TDbEntity> all = DataTable.ToList();
     161             :                         DataTable.DeleteAllOnSubmit(all);
     162             :                 }
     163             : 
     164           0 :                 public void Update(TEntity data)
     165             :                 {
     166             :                         var converted = Converters.ConvertTo<TDbEntity>(data);
     167             :                         DataTable.Attach(converted);
     168             :                 }
     169             : 
     170             :                 #endregion
     171             : 
     172             :                 #region Implementation of IDisposable
     173             : 
     174             :                 /// <summary>
     175             :                 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
     176             :                 /// </summary>
     177           1 :                 public void Dispose()
     178             :                 {
     179             :                         try
     180             :                         {
     181             :                                 DataContext.SubmitChanges();
     182             :                                 DataContext.Transaction.Commit();
     183             :                         }
     184             :                         catch
     185             :                         {
     186             :                                 DataContext.Transaction.Rollback();
     187             :                         }
     188             :                         finally
     189             :                         {
     190             :                                 DataContext.Transaction.Dispose();
     191             :                                 DataContext.Dispose();
     192             :                         }
     193             :                 }
     194             : 
     195             :                 #endregion
     196             :         }
     197             : }

Generated by: LCOV version 1.10