Skip to content

Instantly share code, notes, and snippets.

View mahizsas's full-sized avatar

MAHIZ mahizsas

View GitHub Profile
@mahizsas
mahizsas / MongoDbContext.cs
Last active August 29, 2015 14:13 — forked from khalidsalomao/MongoDbContext.cs
MongoDB Context
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
namespace SimpleMongoDb
{
public class MongoDbContext
{
@mahizsas
mahizsas / CountryDB.cs
Last active August 29, 2015 14:13 — forked from wcadap/CountryDB.cs
Sample DB class with MongoDB persistence
using MongoDB.Driver;
using System;
namespace mvcMongoDB.Models
{
public class CountryDB
{
//For Best practice, Please put this in the web.config. This is only for demo purpose.
//====================================================
public String connectionString = "mongodb://localhost";
@mahizsas
mahizsas / PaginatedList
Last active August 29, 2015 14:13 — forked from lurumad/PaginatedList
Paged list with C#
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
namespace Luru.Collections
{
public class PaginatedList<T>
{
public int PageIndex { get; set; }
@mahizsas
mahizsas / TempDataDocument
Last active February 8, 2021 08:57 — forked from lurumad/TempDataDocument
MongoDB custom serialization for anonymous types
public class TempDataDocument
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("key")]
public string Key { get; set; }
[BsonElement("value")]
[BsonSerializer(typeof(CustomSerializer))]
public object Value { get; set; }
}
@mahizsas
mahizsas / fake-httpcontext.cs
Last active August 29, 2015 14:13 — forked from graylikeme/gist:1242578
Fake HttpContext
public static class MvcMockHelpers
{
public static HttpContextBase FakeHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
@mahizsas
mahizsas / BetterDefaultModelBinder.cs
Last active August 29, 2015 14:13 — forked from feanz/BetterDefaultModelBinder.cs
Model binder for non empty or null List
public class BetterDefaultModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var model = base.CreateModel(controllerContext, bindingContext, modelType);
if (model == null || model is IEnumerable)
return model;
foreach (var property in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
@mahizsas
mahizsas / MockHttpContextFixture.cs
Last active August 29, 2015 14:13 — forked from feanz/MockHttpContextFixture.cs
Mock httpcontext
public class HttpContextBuilder
{
private Mock<HttpContextBase> _context;
private Mock<HttpRequestBase> _request;
private Mock<HttpResponseBase> _response;
private Mock<HttpSessionStateBase> _session;
private Mock<HttpServerUtilityBase> _server;
private Mock<IPrincipal> _user;
private Mock<IIdentity> _identity;
public HttpContextBase FakeHttpContext() {
var context = new Mock<HttpContextBase>();
var files = new Mock<HttpFileCollectionBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
request.Setup(req => req.ApplicationPath).Returns("~/");
@mahizsas
mahizsas / Crc32.cs
Last active August 29, 2015 14:13 — forked from i-e-b/Crc32.cs
using System;
using System.Text;
namespace System.Security.Cryptography {
public static class StringExtensions
{
public static int CRC32(this string src)
{
return (int)Crc32.Compute(Encoding.UTF8.GetBytes(src));
}
@mahizsas
mahizsas / SqlHelper.cs
Last active August 29, 2015 14:13 — forked from i-e-b/SqlHelper.cs
using System;
namespace System.Data.SqlClient {
public class SqlHelper {
readonly string connectionString;
public SqlHelper(string connectionString) { this.connectionString = connectionString; }
public string StringCommand (string commandString, Action<SqlParameterCollection> bindingCommand) {
using (var connection = new SqlConnection(connectionString)) {