Skip to content

Instantly share code, notes, and snippets.

@saintc0d3r
Created November 16, 2013 14:34
Show Gist options
  • Save saintc0d3r/7500784 to your computer and use it in GitHub Desktop.
Save saintc0d3r/7500784 to your computer and use it in GitHub Desktop.
[C#][AWS][SimpleDb][MsTest] A sample of AWS SimpeDb's Domain Repository.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="AWSAccessKey" value="AAOipiapsoif23oi34lk6"/>
<add key="AWSSecretKey" value="LKKUkjjkLIupoiYuhTYiuGyg"/>
<add key="AWSRegion" value="ap-southeast-1" />
</appSettings>
</configuration>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using Amazon.SimpleDB.Model;
using GuestBook.Domain.Models;
namespace GuestBook.Repository.Aws.SimpleDb
{
class GuestBookEntryRepository : SimpleDbRepositoryBase, IGuestBookEntryRepository
{
public const string DOMAIN_NAME = "GuestBookDemo";
public GuestBookEntryRepository() : base(DOMAIN_NAME) { }
public void Add(GuestBookEntry entity)
{
SimpleDb.PutAttributes(new PutAttributesRequest
{
DomainName = DOMAIN_NAME,
ItemName = entity.Id.ToString(CultureInfo.InvariantCulture),
// TODO: Refactor this one to a method
Attributes = new List<ReplaceableAttribute>
{
new ReplaceableAttribute{ Name = "GuestName", Value = entity.GuestName},
new ReplaceableAttribute{ Name="Comment", Value = entity.Comment},
new ReplaceableAttribute{ Name="PhotoUrl", Value = entity.PhotoUrl},
new ReplaceableAttribute{ Name = "CreateDate", Value = entity.CreateDate.ToString(CultureInfo.InvariantCulture)},
new ReplaceableAttribute{ Name = "ModifiedDate", Value = entity.CreateDate.ToString(CultureInfo.InvariantCulture)},
new ReplaceableAttribute{ Name = "ModifiedBy", Value = entity.ModifiedBy.ToString(CultureInfo.InvariantCulture)}
}
});
}
public GuestBookEntry FindByItemId(long itemId)
{
var response = SimpleDb.GetAttributes(new GetAttributesRequest
{
DomainName = DOMAIN_NAME,
ItemName = itemId.ToString()
});
if ((response.HttpStatusCode == HttpStatusCode.OK) && (response.Attributes.Count > 0))
{
// TODO: Refactor this one to a method
return new GuestBookEntry
{
Id = itemId,
GuestName = response.Attributes.GetValue("GuestName"),
Comment = response.Attributes.GetValue("Comment"),
PhotoUrl = response.Attributes.GetValue("PhotoUrl"),
CreateDate = DateTime.Parse(response.Attributes.GetValue("CreateDate")),
ModifiedDate = DateTime.Parse(response.Attributes.GetValue("ModifiedDate")),
ModifiedBy = uint.Parse(response.Attributes.GetValue("ModifiedBy"))
};
}
return null;
}
public void Remove(GuestBookEntry guestBookEntry)
{
SimpleDb.DeleteAttributes(new DeleteAttributesRequest
{
DomainName = DOMAIN_NAME,
ItemName = guestBookEntry.Id.ToString(CultureInfo.InvariantCulture)
});
}
}
}
using GuestBook.Domain.Models;
using GuestBook.Repository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xtremecode.Infrastructure.Library.Di;
namespace Demo.SimpleDb.Test
{
[TestClass]
public class GuestBookEntryRepositoryTest
{
private readonly IGuestBookEntryRepository _guestBookEntryRepository = DependencyInjector.Get<IGuestBookEntryRepository>();
private GuestBookEntry _model;
[TestMethod]
public void Can_add_new_record()
{
// Arrange
_model = new GuestBookEntry { GuestName = "Eva Huang", Comment = "Hi, my name is Eva.", PhotoUrl = string.Empty };
// Act
_guestBookEntryRepository.Add(_model);
// Assert
var insertedModel = _guestBookEntryRepository.FindByItemId(_model.Id);
Assert.IsNotNull(insertedModel);
}
[TestCleanup]
public void Teardown()
{
_guestBookEntryRepository.Remove(_model);
}
}
}
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Amazon;
using Amazon.SimpleDB.Model;
namespace GuestBook.Repository.Aws.SimpleDb
{
static class SimpleDbHelpers
{
public static string GetValue(this List<Attribute> attributes, string attributeName )
{
var result = attributes.FirstOrDefault(attribute => attribute.Name.Equals(attributeName));
return result != null ? result.Value : string.Empty;
}
public static RegionEndpoint GetRegionEndpointFromConfig()
{
var awsRegionSettings = ConfigurationManager.AppSettings["AWSRegion"];
switch (awsRegionSettings)
{
case "ap-southeast-1":
return RegionEndpoint.APSoutheast1;
case "ap-southeast-2":
return RegionEndpoint.APSoutheast2;
case "ap-northeast-1":
return RegionEndpoint.APNortheast1;
case "us-east-1":
return RegionEndpoint.USEast1;
case "us-west-1":
return RegionEndpoint.USWest1;
case "us-west-2":
return RegionEndpoint.USWest2;
case "eu-west-1":
return RegionEndpoint.EUWest1;
case "sa-east-1":
return RegionEndpoint.SAEast1;
}
return RegionEndpoint.APSoutheast1;
}
}
}
using System.Linq;
using System.Net;
using Amazon;
using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
namespace GuestBook.Repository.Aws.SimpleDb
{
abstract class SimpleDbRepositoryBase
{
private readonly string _domainName;
protected SimpleDbRepositoryBase(RegionEndpoint regionEndpoint, string domainName)
{
_domainName = domainName;
SimpleDb = AWSClientFactory.CreateAmazonSimpleDBClient(regionEndpoint);
initialiseDomain();
}
protected SimpleDbRepositoryBase(string domainName) :this( SimpleDbHelpers.GetRegionEndpointFromConfig(), domainName) { }
private void initialiseDomain()
{
// Check the required domain whether it exists or not
var listDomainsResponse = SimpleDb.ListDomains();
if ((listDomainsResponse.HttpStatusCode == HttpStatusCode.OK) &&
(!listDomainsResponse.DomainNames.Any(domainName => domainName.Equals(_domainName))))
{
// Create "GuestBookDemo" domain
SimpleDb.CreateDomain(new CreateDomainRequest { DomainName = _domainName });
}
}
protected IAmazonSimpleDB SimpleDb { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment