Skip to content

Instantly share code, notes, and snippets.

View chadmichel's full-sized avatar
😀
Working

Chad Michel chadmichel

😀
Working
View GitHub Profile
@chadmichel
chadmichel / gist:7139443
Created October 24, 2013 15:37
Web Api - make routing include Actions
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// ADD ACTION TO routeTemplate BELOW!!!
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
@chadmichel
chadmichel / gist:7413695
Created November 11, 2013 14:08
Super Basic MEF Example
public interface IHello
{
string Message();
}
[Export(typeof(IHello))]
public class Hello : IHello
{
public string Message()
{
@chadmichel
chadmichel / CustomerManager.cs
Created November 12, 2013 15:14
MEF Unit Test Mocking
public class CustomerManager : ICustomerManager
{
[Import]
public ICustomerAccessor CustomerAccessor { get; set; }
public void Upgrade(string customerId)
{
// To upgrade a customer we must first find the customer.
var customer = CustomerAccessor.Find(customerId);
// Set Pro equal to true.
@chadmichel
chadmichel / gist:7434071
Created November 12, 2013 16:32
Transaction Scope and unit tests. Set transaction scope before unit tests. Don't commit and all your DB changes will rollback, great for accessor unit tests.
public class MyTests
{
TransactionScope _testTransactionScope;
#region transaction scope
[TestInitialize]
public void TestInitialize()
{
@chadmichel
chadmichel / AccessorFactory.cs
Created November 12, 2013 16:58
Unit Test example and basic dependency injection
public class AccessorFactory
{
public virtual T Create<T>()
where T : class
{
if (typeof(T).Name == "CustomerAccessor")
return new CustomerAccessor() as T;
return null;
}
@chadmichel
chadmichel / gist:7452220
Created November 13, 2013 16:44
MSMQ Send Message
// Create a transaction because we are using a transactional queue.
using (var trn = new MessageQueueTransaction())
{
try
{
// Create queue object
using (var queue = new MessageQueue(@".\private$\testqueue"))
{
queue.Formatter = new XmlMessageFormatter();
@chadmichel
chadmichel / gist:7452237
Created November 13, 2013 16:45
MSMQ listen for message
public partial class Service1 : ServiceBase
{
private MessageQueue _queue;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
@chadmichel
chadmichel / gist:7530429
Created November 18, 2013 16:09
AWS Queue - Write
var request = new Amazon.SQS.Model.SendMessageRequest();
request.QueueUrl = url;
request.MessageBody = "a simple body";
using (var client = new Amazon.SQS.AmazonSQSClient(key, secret))
{
client.SendMessage(request);
}
@chadmichel
chadmichel / gist:7530437
Created November 18, 2013 16:10
AWS Queue - Read
var request = new Amazon.SQS.Model.ReceiveMessageRequest();
request.QueueUrl = url;
using (var client = new Amazon.SQS.AmazonSQSClient(key, secret))
{
var response = client.ReceiveMessage(request);
}
@chadmichel
chadmichel / gist:7546372
Created November 19, 2013 14:44
Azure Queue - Write
class Program
{
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("myqueue");