This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SqlDataHandler : IDisposable | |
{ | |
private string _connectionString; | |
private SqlConnection _sqlConnection; | |
public SqlDataHandler() | |
{ | |
_connectionString = ConfigurationManager.AppSettings["connectionString"]; | |
_sqlConnection = new SqlConnection(_connectionString); | |
_sqlConnection.Open(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ClosureExample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Action counterIncrementAction = CounterIncrementAction(); | |
counterIncrementAction(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id" : ObjectId("59ce6b34f48f171624840b05"), | |
"name" : "Nikola", | |
"blog" : "rubikscode.net", | |
"age" : 30, | |
"location" : "Beograd" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization.Attributes; | |
namespace MongoDb | |
{ | |
/// <summary> | |
/// Class used in business logic to represent user. | |
/// </summary> | |
public class User | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Class used to access Mongo DB. | |
/// </summary> | |
public class UsersRepository | |
{ | |
private IMongoClient _client; | |
private IMongoDatabase _database; | |
private IMongoCollection<User> _usersCollection; | |
public UsersRepository(string connectionString) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task InsertUser(User user) | |
{ | |
await _usersCollection.InsertOneAsync(user); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<List<User>> GetAllUsers() | |
{ | |
return await _usersCollection.Find(new BsonDocument()).ToListAsync(); | |
} | |
public async Task<List<User>> GetUsersByField(string fieldName, string fieldValue) | |
{ | |
var filter = Builders<User>.Filter.Eq(fieldName, fieldValue); | |
var result = await _usersCollection.Find(filter).ToListAsync(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<bool> UpdateUser(ObjectId id, string udateFieldName, string updateFieldValue) | |
{ | |
var filter = Builders<User>.Filter.Eq("_id", id); | |
var update = Builders<User>.Update.Set(udateFieldName, updateFieldValue); | |
var result = await _usersCollection.UpdateOneAsync(filter, update); | |
return result.ModifiedCount != 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<bool> DeleteUserById(ObjectId id) | |
{ | |
var filter = Builders<User>.Filter.Eq("_id", id); | |
var result = await _usersCollection.DeleteOneAsync(filter); | |
return result.DeletedCount != 0; | |
} | |
public async Task<long> DeleteAllUsers() | |
{ | |
var filter = new BsonDocument(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var users = await _mongoDbRepo.GetUsersByField("name", "Nikola"); | |
var user = users.FirstOrDefault(); | |
var result = await _mongoDbRepo.UpdateUser(user.Id, "address", "test address"); |