Skip to content

Instantly share code, notes, and snippets.

View jasonmccay's full-sized avatar
🏠
Working from home

Jason McCay jasonmccay

🏠
Working from home
View GitHub Profile
@jasonmccay
jasonmccay / gist:1145512
Created August 15, 2011 00:39
An example mongoid.yml (YAML) file to configure MongoDB replica sets with MongoHQ.
defaults: &defaults
autocreate_indexes: true
max_retries_on_connection_failure: 3
allow_dynamic_fields: false
development:
host: localhost
database: taq_development
namespace mongohq_csharp.Models.DTO
{
public class DeveloperDTO
{
public BsonObjectId id { get; set; }
public string name { get; set; }
public string languages { get; set; }
public string company { get; set; }
}
}
using System.Collections.Generic;
using mongohq_csharp.Models.DTO;
namespace mongohq_csharp.Models
{
public class DeveloperModel
{
public IList<DeveloperDTO> developers { get; set; }
}
}
@jasonmccay
jasonmccay / gist:1273326
Created October 9, 2011 05:05
Adding DeveloperModel.cs to the Models directory.
using System.Collections.Generic;
using mongohq_csharp.Models.DTO;
namespace mongohq_csharp.Models
{
public class DeveloperModel
{
public IList<DeveloperDTO> developers { get; set; }
}
}
@jasonmccay
jasonmccay / web.config
Created October 9, 2011 05:07
Set up a connection string in the web.config file of the project.
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="MongoHQ"
connectionString="your mongo uri"/>
</connectionStrings>
@jasonmccay
jasonmccay / gist:1273333
Created October 9, 2011 05:12
Declare a private MongoDatabase object in our DeveloperController
public class DeveloperController : Controller
{
readonly MongoDatabase mongo_db;
public DeveloperController()
{
mongo_db = retreive_mongohq_db();
}
}
@jasonmccay
jasonmccay / gist:1273337
Created October 9, 2011 05:15
Get the MongoHQ database with the connection string.
static MongoDatabase retreive_mongohq_db()
{
return MongoServer.Create(
ConfigurationManager.ConnectionStrings["MongoHQ"].ConnectionString)
.GetDatabase("t2");
}
@jasonmccay
jasonmccay / gist:1273340
Created October 9, 2011 05:17
Create your Developer Controller
using System.Linq;
using System.Web.Mvc;
using System.Configuration;
using MongoDB.Driver;
using mongohq_csharp.Models;
using mongohq_csharp.Models.DTO;
namespace mongohq_csharp.Controllers
{
public class DeveloperController : Controller
@jasonmccay
jasonmccay / gist:1273351
Created October 9, 2011 05:29
Creating a view for the Developer Listing.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<mongohq_csharp.Models.DeveloperModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Developers</h2>
@jasonmccay
jasonmccay / gist:1273352
Created October 9, 2011 05:32
Add Index method to the Controller
public ActionResult Index()
{
var model = new DeveloperModel();
var developers_collection = mongo_db.GetCollection("developers").FindAll().AsEnumerable();
model.developers = (from developer in developers_collection
select new DeveloperDTO
{
id = developer["_id"].AsObjectId,
name = developer["name"].AsString,