/// <summary> /// Constructor /// </summary> /// <param name="dataSession">Active data session</param> /// <param name="collectionReference">Collection reference</param> protected EntityFrameworkRepositoryBase(IDataSession dataSession, Func <TDbContext, DbSet <TEntity> > collectionReference) { //Eseguo la validazione degli argomenti if (dataSession == null) { throw new ArgumentNullException(nameof(dataSession)); } if (collectionReference == null) { throw new ArgumentNullException(nameof(collectionReference)); } //Tento il cast della sessione generica ad EntityFramework var efSession = dataSession as EntityFrameworkDataSession <TDbContext>; if (efSession == null) { throw new InvalidCastException( $"Specified session of type '{dataSession.GetType().FullName}' cannot be converted to type '{typeof(EntityFrameworkDataSession<TDbContext>).FullName}'."); } //Imposto la proprietà della sessione DataSession = efSession; //Recupero il riferimento alla collezione Collection = collectionReference(DataSession.Context); }
/// <summary> /// Costructor of Mongo Repository Base /// </summary> /// <param name="dataSession">Data Session</param> /// <param name="CollectionName">if null use as collectionName the pluralized class name</param> protected MongoDbRepositoryBase(IDataSession dataSession, string CollectionName = null) { //Validazione argomenti if (dataSession == null) { throw new ArgumentNullException(nameof(dataSession)); } //dataSession. //var options = new TOptions(); //Type type = System.Type.GetType(); //dataSession. //Tento il cast della sessione generica a RemoteApiDataSession if (!(dataSession is MongoDbDataSession <TOptions> currentSession)) { throw new InvalidCastException(string.Format("Specified session of type '{0}' cannot be converted to type '{1}'.", dataSession.GetType().FullName, typeof(MongoDbDataSession <TOptions>).FullName)); } //Imposto la proprietà della sessione DataSession = currentSession; //DataSession = dataSession; //Imposto il nome della Collection if (CollectionName == null) { var pluralize = new PluralizeUtility(); CollectionName = pluralize.Pluralize(typeof(TEntity)); } _CollectionName = CollectionName; }
/// <summary> /// Constructor /// </summary> /// <param name="dataSession">Active data session</param> /// <param name="baseFolder">Base folder for archive</param> protected FileSystemRepositoryBase(IDataSession dataSession, string baseFolder) { //Validazione argomenti if (dataSession == null) { throw new ArgumentNullException(nameof(dataSession)); } if (string.IsNullOrEmpty(baseFolder)) { throw new ArgumentNullException(nameof(baseFolder)); } //Tento il cast della sessione generica a FileSystemDataSession var fileSystemDataSession = dataSession as FileSystemDataSession; if (fileSystemDataSession == null) { throw new InvalidCastException( $"Specified session of type '{dataSession.GetType().FullName}' cannot be converted to type '{typeof(FileSystemDataSession).FullName}'."); } //Imposto la proprietà della sessione DataSession = fileSystemDataSession; //TODO Qui i dati dovrebbero essere prelevati da filesystem, su cartelle specifiche //definite per convenzione. Esempio, su oggetti DataFlow o User, ci dovrebbe //essere una cartella base che funge da contenitore (es. 'F:\FileSystemStorage') //poi tante sottofolder (es. 'DataFlows', 'Users', ecc) e all'interno di esse //un file per ciascuna entità, in formato JSON per un rapido recupero dei dati //I dati, una volta i memoria, dovrebbero essere riscritti sul filesystem solo //nel caso in cui questi cambiano, eventualmente con task asincroni. Valutare //se utilizzare NanoDb per eseguire questo tipo di interazione MockedEntities = new List <TEntity>(); }
/// <summary> /// Constructor /// </summary> /// <param name="dataSession">Active data session</param> protected MongoDbRepositoryBase(IDataSession dataSession) { //Eseguo la validazione degli argomenti if (dataSession == null) { throw new ArgumentNullException(nameof(dataSession)); } //Tento il cast della sessione generica ad MongoDb var mongoSession = dataSession as MongoDbDataSession <TMongoDbOptions>; //Imposto la proprietà della sessione DataSession = mongoSession ?? throw new InvalidCastException( $"Specified session of type '{dataSession.GetType().FullName}' cannot " + $"be converted to type '{typeof(MongoDbDataSession<TMongoDbOptions>).FullName}'."); //Recupero la collezione su cui lavorare Collection = mongoSession.Database.GetCollection <TEntity>(typeof(TEntity).Name); }
/// <summary> /// Convert provided data session in "IMockDataSession" /// </summary> /// <param name="instance">Instance</param> /// <returns>Returns converted data session</returns> public static IMockDataSession AsMockDataSession(this IDataSession instance) { //Arguments validation if (instance == null) { throw new ArgumentNullException(nameof(instance)); } //Tento il cast della sessione generica a MockDataSession IMockDataSession mockSession = instance as IMockDataSession; if (mockSession == null) { throw new InvalidCastException($"Specified data session of type '{instance.GetType().FullName}' " + $"cannot be converted to type '{typeof(IMockDataSession).FullName}'."); } //Returns instance return(mockSession); }
public static IDataSession AddSet <T>(this IDataSession session, params T[] set) where T : class { var mockSession = session as MockDataSession; if (mockSession == null) { throw new Exception(string.Format("Data store must of type MockDataStore. Got type '{0}' instead", session.GetType())); } mockSession .Mock .Setup(x => x.Set <T>()) .Returns(set == null ? new FakeDbSet <T>() : new FakeDbSet <T>(set)); return(mockSession); }