Documentation Coverage Report
Current view: top level - Cqrs/Events - SqlEventStoreDataContext.cs Hit Total Coverage
Version: 2.2 Artefacts: 7 21 33.3 %
Date: 2018-08-07 15:04:50

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="Chinchilla Software Limited">
       4             : // //   Copyright Chinchilla Software Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections.Generic;
      11             : using System.Data.Linq;
      12             : using System.Data.Linq.Mapping;
      13             : 
      14             : namespace Cqrs.Events
      15             : {
      16             :         /// <summary>
      17             :         /// A custom <see cref="DataContext"/> that supports specifying a table name.
      18             :         /// </summary>
      19             :         public class SqlEventStoreDataContext : DataContext
      20           1 :         {
      21             :                 /// <summary>
      22             :                 /// Instantiates a new instance of the <see cref="SqlEventStoreDataContext"/> class by referencing a file source.
      23             :                 /// </summary>
      24             :                 /// <param name="fileOrServerOrConnection">
      25             :                 /// This argument can be any one of the following: 
      26             :                 /// The name of a file where a SQL Server Express database resides. 
      27             :                 /// The name of a server where a database is present. In this case the provider uses the default database for a user. 
      28             :                 /// A complete connection string. LINQ to SQL just passes the string to the provider without modification.
      29             :                 /// </param>
      30           1 :                 public SqlEventStoreDataContext(string fileOrServerOrConnection)
      31             :                         : base(fileOrServerOrConnection) { }
      32             : 
      33             :                 /// <summary>
      34             :                 /// Instantiates a new instance of the <see cref="SqlEventStoreDataContext"/> class by referencing a connection.
      35             :                 /// </summary>
      36             :                 /// <param name="connection">The connection used by the .NET Framework.</param>
      37           1 :                 public SqlEventStoreDataContext(System.Data.IDbConnection connection)
      38             :                         : base(connection) { }
      39             : 
      40             :                 private SqlEventStoreDataContext(CustomMappingSource sharedMappingSource, System.Data.IDbConnection connection)
      41             :                         : base(connection, sharedMappingSource) { }
      42             : 
      43             :                 private SqlEventStoreDataContext(CustomMappingSource sharedMappingSource, string fileOrServerOrConnection)
      44             :                         : base(fileOrServerOrConnection, sharedMappingSource) { }
      45             : 
      46             :                 /// <summary>
      47             :                 /// Instantiates a new instance of the <see cref="SqlEventStoreDataContext"/> class by referencing a file source.
      48             :                 /// </summary>
      49             :                 /// <param name="tableName">The name of the table.</param>
      50             :                 /// <param name="fileOrServerOrConnection">
      51             :                 /// This argument can be any one of the following: 
      52             :                 /// The name of a file where a SQL Server Express database resides. 
      53             :                 /// The name of a server where a database is present. In this case the provider uses the default database for a user. 
      54             :                 /// A complete connection string. LINQ to SQL just passes the string to the provider without modification.
      55             :                 /// </param>
      56           1 :                 public static SqlEventStoreDataContext New<TTable>(string tableName, string fileOrServerOrConnection)
      57             :                 {
      58             :                         return New(typeof(TTable), tableName, fileOrServerOrConnection);
      59             :                 }
      60             : 
      61             :                 /// <summary>
      62             :                 /// Instantiates a new instance of the <see cref="SqlEventStoreDataContext"/> class by referencing a connection.
      63             :                 /// </summary>
      64             :                 /// <param name="tableName">The name of the table.</param>
      65             :                 /// <param name="connection">The connection used by the .NET Framework.</param>
      66           1 :                 public static SqlEventStoreDataContext New<TTable>(string tableName, System.Data.IDbConnection connection)
      67             :                 {
      68             :                         return New(typeof(TTable), tableName, connection);
      69             :                 }
      70             : 
      71             :                 private static CustomMappingSource New(Type rowType, string tableName)
      72             :                 {
      73             :                         CustomMappingSource sharedMappingSource = new CustomMappingSource();
      74             :                         sharedMappingSource.SetCustomTableName(rowType, tableName);
      75             :                         return sharedMappingSource;
      76             :                 }
      77             : 
      78             :                 /// <summary>
      79             :                 /// Instantiates a new instance of the <see cref="SqlEventStoreDataContext"/> class by referencing a file source.
      80             :                 /// </summary>
      81             :                 /// <param name="rowType">The <see cref="Type"/> of the entity the table hold data for.</param>
      82             :                 /// <param name="tableName">The name of the table.</param>
      83             :                 /// <param name="fileOrServerOrConnection">
      84             :                 /// This argument can be any one of the following: 
      85             :                 /// The name of a file where a SQL Server Express database resides. 
      86             :                 /// The name of a server where a database is present. In this case the provider uses the default database for a user. 
      87             :                 /// A complete connection string. LINQ to SQL just passes the string to the provider without modification.
      88             :                 /// </param>
      89           1 :                 public static SqlEventStoreDataContext New(Type rowType, string tableName, string fileOrServerOrConnection)
      90             :                 {
      91             :                         return new SqlEventStoreDataContext(New(rowType, tableName), fileOrServerOrConnection);
      92             :                 }
      93             : 
      94             :                 /// <summary>
      95             :                 /// Instantiates a new instance of the <see cref="SqlEventStoreDataContext"/> class by referencing a connection.
      96             :                 /// </summary>
      97             :                 /// <param name="rowType">The <see cref="Type"/> of the entity the table hold data for.</param>
      98             :                 /// <param name="tableName">The name of the table.</param>
      99             :                 /// <param name="connection">The connection used by the .NET Framework.</param>
     100           1 :                 public static SqlEventStoreDataContext New(Type rowType, string tableName, System.Data.IDbConnection connection)
     101             :                 {
     102             :                         return new SqlEventStoreDataContext(New(rowType, tableName), connection);
     103             :                 }
     104             :         }
     105             : 
     106             :         internal class CustomMappingSource : MappingSource
     107             :         {
     108             :                 readonly AttributeMappingSource _mapping = new AttributeMappingSource();
     109             : 
     110             :                 readonly Dictionary<Type, string> _customTableNames = new Dictionary<Type, string>();
     111             : 
     112           0 :                 public virtual void SetCustomTableName(Type rowType, string tableName)
     113             :                 {
     114             :                         if (string.IsNullOrEmpty(tableName))
     115             :                                 throw new ArgumentNullException("tableName");
     116             : 
     117             :                         _customTableNames[rowType] = tableName;
     118             :                 }
     119             : 
     120           0 :                 public virtual void SetCustomTableName<T>(string tableName)
     121             :                 {
     122             :                         SetCustomTableName(typeof(T), tableName);
     123             :                 }
     124             : 
     125           0 :                 protected override MetaModel CreateModel(Type dataContextType)
     126             :                 {
     127             :                         MetaModel oldmodel = _mapping.GetModel(dataContextType);
     128             :                         CustomMetaModel newmodel = new CustomMetaModel(oldmodel, _customTableNames);
     129             :                         return newmodel;
     130             :                 }
     131             :         }
     132             : 
     133             :         internal class CustomMetaModel : MetaModel
     134             :         {
     135             :                 readonly MetaModel _orgmodel;
     136             : 
     137             :                 readonly Dictionary<Type, MetaTable> _customtables = new Dictionary<Type, MetaTable>();
     138             :                 readonly Dictionary<Type, string> _tableNames;
     139             : 
     140           0 :                 public CustomMetaModel(MetaModel orgmodel, Dictionary<Type, string> tableNames)
     141             :                 {
     142             :                         _orgmodel = orgmodel;
     143             :                         _tableNames = tableNames;
     144             :                 }
     145             : 
     146           0 :                 public override MetaType GetMetaType(Type type)
     147             :                 {
     148             :                         MetaTable metaTable;
     149             :                         if (_customtables.TryGetValue(type, out metaTable))
     150             :                                 return metaTable.RowType;
     151             :                         return _orgmodel.GetMetaType(type);
     152             :                 }
     153           0 :                 public override MetaTable GetTable(Type rowType)
     154             :                 {
     155             :                         MetaTable customMetaTable;
     156             :                         if (_customtables.TryGetValue(rowType, out customMetaTable))
     157             :                                 return customMetaTable;
     158             : 
     159             :                         if (_tableNames.ContainsKey(rowType))
     160             :                         {
     161             :                                 MetaTable orgtable = _orgmodel.GetTable(rowType);
     162             :                                 MetaType orgrowtype = orgtable.RowType;
     163             :                                 CustomMetaType newRowType = new CustomMetaType(orgrowtype, this);
     164             :                                 _customtables.Add(rowType, new CustomMetaTable(orgtable, this, newRowType, _tableNames[rowType]));
     165             :                                 newRowType.MetaTable = _customtables[rowType];
     166             :                                 return newRowType.MetaTable;
     167             :                         }
     168             : 
     169             :                         return _orgmodel.GetTable(rowType);
     170             :                 }
     171             : 
     172             :                 #region MetaModel Forwards
     173             : 
     174             :                 public override Type ContextType { get { return _orgmodel.ContextType; } }
     175             :                 public override string DatabaseName { get { return _orgmodel.DatabaseName; } }
     176             :                 public override MappingSource MappingSource { get { return _orgmodel.MappingSource; } }
     177             :                 public override Type ProviderType { get { return _orgmodel.ProviderType; } }
     178           0 :                 public override MetaFunction GetFunction(System.Reflection.MethodInfo method) { return _orgmodel.GetFunction(method); }
     179           0 :                 public override IEnumerable<MetaFunction> GetFunctions() { return _orgmodel.GetFunctions(); }
     180           0 :                 public override IEnumerable<MetaTable> GetTables() { return _orgmodel.GetTables(); }
     181             : 
     182             :                 #endregion
     183             :         }
     184             : 
     185             :         internal class CustomMetaTable : MetaTable
     186             :         {
     187             :                 readonly MetaTable _orgtable;
     188             :                 readonly MetaModel _metamodel;
     189             :                 readonly MetaType _rowtype;
     190             :                 readonly string _tableName;
     191             : 
     192           0 :                 public CustomMetaTable(MetaTable orgtable, MetaModel metamodel, MetaType rowtype, string tableName)
     193             :                 {
     194             :                         _orgtable = orgtable;
     195             :                         _metamodel = metamodel;
     196             :                         _rowtype = rowtype;
     197             :                         _tableName = tableName;
     198             :                 }
     199             : 
     200             :                 public override MetaModel Model { get { return _metamodel; } }
     201             :                 public override MetaType RowType { get { return _rowtype; } }
     202             :                 public override string TableName { get { return _tableName; } }
     203             : 
     204             :                 #region MetaTable Forwards
     205             : 
     206             :                 public override System.Reflection.MethodInfo DeleteMethod { get { return _orgtable.DeleteMethod; } }
     207             :                 public override System.Reflection.MethodInfo InsertMethod { get { return _orgtable.InsertMethod; } }
     208             :                 public override System.Reflection.MethodInfo UpdateMethod { get { return _orgtable.UpdateMethod; } }
     209             : 
     210             :                 #endregion
     211             :         }
     212             : 
     213             :         internal class CustomMetaType : MetaType
     214             :         {
     215             :                 readonly MetaType _orgtype;
     216             :                 readonly MetaModel _metamodel;
     217             : 
     218             :                 public MetaTable MetaTable { get; set; }
     219             : 
     220           0 :                 public CustomMetaType(MetaType orgtype, MetaModel metamodel)
     221             :                 {
     222             :                         _orgtype = orgtype;
     223             :                         _metamodel = metamodel;
     224             :                 }
     225             : 
     226             :                 public override MetaTable Table { get { return MetaTable; } }
     227             :                 public override MetaModel Model { get { return _metamodel; } }
     228             : 
     229             :                 #region MetaType Forwards
     230             : 
     231             :                 public override System.Collections.ObjectModel.ReadOnlyCollection<MetaAssociation> Associations { get { return _orgtype.Associations; } }
     232             :                 public override bool CanInstantiate { get { return _orgtype.CanInstantiate; } }
     233             :                 public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> DataMembers { get { return _orgtype.DataMembers; } }
     234             :                 public override MetaDataMember DBGeneratedIdentityMember { get { return _orgtype.DBGeneratedIdentityMember; } }
     235             :                 public override System.Collections.ObjectModel.ReadOnlyCollection<MetaType> DerivedTypes { get { return _orgtype.DerivedTypes; } }
     236             :                 public override MetaDataMember Discriminator { get { return _orgtype.Discriminator; } }
     237             :                 public override bool HasAnyLoadMethod { get { return _orgtype.HasAnyLoadMethod; } }
     238             :                 public override bool HasAnyValidateMethod { get { return _orgtype.HasAnyValidateMethod; } }
     239             :                 public override bool HasInheritance { get { return _orgtype.HasInheritance; } }
     240             :                 public override bool HasInheritanceCode { get { return _orgtype.HasInheritanceCode; } }
     241             :                 public override bool HasUpdateCheck { get { return _orgtype.HasUpdateCheck; } }
     242             :                 public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> IdentityMembers { get { return _orgtype.IdentityMembers; } }
     243             :                 public override MetaType InheritanceBase { get { return _orgtype.InheritanceBase; } }
     244             :                 public override object InheritanceCode { get { return _orgtype.InheritanceCode; } }
     245             :                 public override MetaType InheritanceDefault { get { return _orgtype.InheritanceDefault; } }
     246             :                 public override MetaType InheritanceRoot { get { return _orgtype.InheritanceRoot; } }
     247             :                 public override System.Collections.ObjectModel.ReadOnlyCollection<MetaType> InheritanceTypes { get { return _orgtype.InheritanceTypes; } }
     248             :                 public override bool IsEntity { get { return _orgtype.IsEntity; } }
     249             :                 public override bool IsInheritanceDefault { get { return _orgtype.IsInheritanceDefault; } }
     250             :                 public override string Name { get { return _orgtype.Name; } }
     251             :                 public override System.Reflection.MethodInfo OnLoadedMethod { get { return _orgtype.OnLoadedMethod; } }
     252             :                 public override System.Reflection.MethodInfo OnValidateMethod { get { return _orgtype.OnValidateMethod; } }
     253             :                 public override System.Collections.ObjectModel.ReadOnlyCollection<MetaDataMember> PersistentDataMembers { get { return _orgtype.PersistentDataMembers; } }
     254             :                 public override Type Type { get { return _orgtype.Type; } }
     255             :                 public override MetaDataMember VersionMember { get { return _orgtype.VersionMember; } }
     256           0 :                 public override MetaDataMember GetDataMember(System.Reflection.MemberInfo member) { return _orgtype.GetDataMember(member); }
     257           0 :                 public override MetaType GetInheritanceType(Type type) { return _orgtype.GetInheritanceType(type); }
     258           0 :                 public override MetaType GetTypeForInheritanceCode(object code) { return _orgtype.GetTypeForInheritanceCode(code); }
     259             : 
     260             :                 #endregion
     261             :         }
     262             : 
     263             : }

Generated by: LCOV version 1.12