Exemplo n.º 1
0
        public static async Task Insert <T>(string collectionName, T value, NoSQLType type = NoSQLType.MongoDB, IClientSessionHandle clientSession = null, INoSQLDbConnection connection = null) where T : MongDBBaseModel
        {
            if (connection != null)
            {
                var collection = connection.Database.GetCollection <T>(collectionName);
                await collection.InsertOneAsync(value);
            }
            else
            {
                switch (type)
                {
                case NoSQLType.MongoDB:
                    using (INoSQLDbConnection conn = new MongoSqlConnection(NoConnectionString, NoSqlDatabase))
                    {
                        var collection = connection.Database.GetCollection <T>(collectionName);
                        await collection.InsertOneAsync(value);
                    }
                    break;

                default:
                    throw new Exception("Wrong type");
                }
            }
        }
Exemplo n.º 2
0
        public static INoSQLDbConnection GetNoSQLConnection(string connectionString = "", string database = "", NoSQLType type = NoSQLType.MongoDB)
        {
            if ("".Equals(NoConnectionString))
            {
                connectionString = NoConnectionString;
            }

            if ("".Equals(database))
            {
                database = NoSqlDatabase;
            }

            switch (type)
            {
            case NoSQLType.MongoDB:
                return(new MongoSqlConnection(connectionString, database));

            default:
                throw new Exception("Wrong type");
            }
        }
Exemplo n.º 3
0
        public static async Task <IEnumerable <T> > Query <T>(string collectionName, Func <T, bool> query, NoSQLType type = NoSQLType.MongoDB, IClientSessionHandle clientSession = null, INoSQLDbConnection connection = null)
        {
            if (connection != null)
            {
                var collection = connection.Database.GetCollection <T>(collectionName);
                return(await((IMongoQueryable <T>)(collection.AsQueryable().Where(query))).ToListAsync());
            }
            else
            {
                switch (type)
                {
                case NoSQLType.MongoDB:
                    using (INoSQLDbConnection conn = new MongoSqlConnection(NoConnectionString, NoSqlDatabase))
                    {
                        var collection = conn.Database.GetCollection <T>(collectionName);
                        return(await((IMongoQueryable <T>)(collection.AsQueryable().Where(query))).ToListAsync());
                    }

                default:
                    throw new Exception("Wrong type");
                }
            }
        }