More MongoDB Migration

This commit is contained in:
Thomas Hounsell 2015-08-05 12:28:16 +01:00
parent 8bb592fbaf
commit c7a1f85ca3
9 changed files with 156 additions and 59 deletions

View File

@ -1,32 +0,0 @@
using System.Configuration;
namespace BuildFeed
{
public static class DatabaseConfig
{
public static string Host { get; private set; }
public static int Port { get; private set; }
public static long Database { get; private set; }
static DatabaseConfig()
{
Host = ConfigurationManager.AppSettings["data:ServerHost"];
int port;
bool success = int.TryParse(ConfigurationManager.AppSettings["data:ServerPort"], out port);
if (!success)
{
port = 6379; // redis default port
}
Port = port;
long db;
success = long.TryParse(ConfigurationManager.AppSettings["data:ServerDB"], out db);
if (!success)
{
db = 0; // redis default db
}
Database = db;
}
}
}

View File

@ -0,0 +1,26 @@
using System.Configuration;
namespace BuildFeed
{
internal static class MongoConfig
{
public static string Host { get; private set; }
public static int Port { get; private set; }
public static string Database { get; private set; }
static MongoConfig()
{
Host = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["data:MongoHost"]) ? ConfigurationManager.AppSettings["data:MongoHost"] : "localhost";
int _port;
bool success = int.TryParse(ConfigurationManager.AppSettings["data:MongoPort"], out _port);
if (!success)
{
_port = 27017; // mongo default port
}
Port = _port;
Database = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["data:MongoDB"]) ? ConfigurationManager.AppSettings["data:MongoDB"] : "MongoAuth";
}
}
}

View File

@ -190,7 +190,7 @@
<Compile Include="Code\DateTimeModelBinder.cs" /> <Compile Include="Code\DateTimeModelBinder.cs" />
<Compile Include="Code\DisplayHelpers.cs" /> <Compile Include="Code\DisplayHelpers.cs" />
<Compile Include="App_Start\BundleConfig.cs" /> <Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\DatabaseConfig.cs" /> <Compile Include="App_Start\MongoConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" /> <Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" /> <Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Areas\admin\adminAreaRegistration.cs" /> <Compile Include="Areas\admin\adminAreaRegistration.cs" />

View File

@ -4,9 +4,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using NServiceKit.DataAnnotations;
using NServiceKit.DesignPatterns.Model;
using NServiceKit.Redis;
using Required = System.ComponentModel.DataAnnotations.RequiredAttribute; using Required = System.ComponentModel.DataAnnotations.RequiredAttribute;
using System.Web.Mvc; using System.Web.Mvc;
@ -15,7 +12,7 @@
namespace BuildFeed.Models namespace BuildFeed.Models
{ {
[DataObject] [DataObject]
public class Build : IHasId<long> public class Build
{ {
[Key] [Key]
[AutoIncrement] [AutoIncrement]
@ -116,7 +113,7 @@ public string FullBuildString
[DataObjectMethod(DataObjectMethodType.Select, true)] [DataObjectMethod(DataObjectMethodType.Select, true)]
public static IEnumerable<Build> Select() public static IEnumerable<Build> Select()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
return client.GetAll(); return client.GetAll();
@ -126,7 +123,7 @@ public static IEnumerable<Build> Select()
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static Build SelectById(long id) public static Build SelectById(long id)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
return client.GetById(id); return client.GetById(id);
@ -136,7 +133,7 @@ public static Build SelectById(long id)
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<Build> SelectInBuildOrder() public static IEnumerable<Build> SelectInBuildOrder()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
return client.GetAll() return client.GetAll()
@ -151,7 +148,7 @@ public static IEnumerable<Build> SelectInBuildOrder()
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<Build> SelectInVersionOrder() public static IEnumerable<Build> SelectInVersionOrder()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
return client.GetAll() return client.GetAll()
@ -166,7 +163,7 @@ public static IEnumerable<Build> SelectInVersionOrder()
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<BuildVersion> SelectBuildVersions() public static IEnumerable<BuildVersion> SelectBuildVersions()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
var results = client.GetAll() var results = client.GetAll()
@ -181,7 +178,7 @@ public static IEnumerable<BuildVersion> SelectBuildVersions()
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<int> SelectBuildYears() public static IEnumerable<int> SelectBuildYears()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
var results = client.GetAll().Where(b => b.BuildTime.HasValue) var results = client.GetAll().Where(b => b.BuildTime.HasValue)
@ -195,7 +192,7 @@ public static IEnumerable<int> SelectBuildYears()
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<string> SelectBuildLabs() public static IEnumerable<string> SelectBuildLabs()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
var results = client.GetAll() var results = client.GetAll()
@ -210,7 +207,7 @@ public static IEnumerable<string> SelectBuildLabs()
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<string> SelectBuildLabs(byte major, byte minor) public static IEnumerable<string> SelectBuildLabs(byte major, byte minor)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
var results = client.GetAll() var results = client.GetAll()
@ -227,7 +224,7 @@ public static IEnumerable<string> SelectBuildLabs(byte major, byte minor)
[DataObjectMethod(DataObjectMethodType.Insert, true)] [DataObjectMethod(DataObjectMethodType.Insert, true)]
public static void Insert(Build item) public static void Insert(Build item)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
item.Id = client.GetNextSequence(); item.Id = client.GetNextSequence();
@ -242,7 +239,7 @@ public static void Update(Build item)
item.Added = old.Added; item.Added = old.Added;
item.Modified = DateTime.Now; item.Modified = DateTime.Now;
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
client.Store(item); client.Store(item);
@ -252,7 +249,7 @@ public static void Update(Build item)
[DataObjectMethod(DataObjectMethodType.Insert, false)] [DataObjectMethod(DataObjectMethodType.Insert, false)]
public static void InsertAll(IEnumerable<Build> items) public static void InsertAll(IEnumerable<Build> items)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
client.StoreAll(items); client.StoreAll(items);
@ -262,7 +259,7 @@ public static void InsertAll(IEnumerable<Build> items)
[DataObjectMethod(DataObjectMethodType.Delete, true)] [DataObjectMethod(DataObjectMethodType.Delete, true)]
public static void DeleteById(long id) public static void DeleteById(long id)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<Build>(); var client = rClient.As<Build>();
client.DeleteById(id); client.DeleteById(id);

View File

@ -31,7 +31,7 @@ public class MetaItem : IHasId<MetaItemKey>
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static IEnumerable<MetaItem> Select() public static IEnumerable<MetaItem> Select()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
return client.GetAll(); return client.GetAll();
@ -41,7 +41,7 @@ public static IEnumerable<MetaItem> Select()
[DataObjectMethod(DataObjectMethodType.Select, true)] [DataObjectMethod(DataObjectMethodType.Select, true)]
public static IEnumerable<MetaItem> SelectByType(MetaType type) public static IEnumerable<MetaItem> SelectByType(MetaType type)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
return from t in client.GetAll() return from t in client.GetAll()
@ -53,7 +53,7 @@ public static IEnumerable<MetaItem> SelectByType(MetaType type)
[DataObjectMethod(DataObjectMethodType.Select, false)] [DataObjectMethod(DataObjectMethodType.Select, false)]
public static MetaItem SelectById(MetaItemKey id) public static MetaItem SelectById(MetaItemKey id)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
return client.GetById(id); return client.GetById(id);
@ -64,7 +64,7 @@ public static MetaItem SelectById(MetaItemKey id)
public static IEnumerable<string> SelectUnusedLabs() public static IEnumerable<string> SelectUnusedLabs()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
var labs = Build.SelectBuildLabs(); var labs = Build.SelectBuildLabs();
@ -83,7 +83,7 @@ where usedLabs.All(ul => ul.Id.Value != l)
public static IEnumerable<string> SelectUnusedVersions() public static IEnumerable<string> SelectUnusedVersions()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
var versions = Build.SelectBuildVersions(); var versions = Build.SelectBuildVersions();
@ -102,7 +102,7 @@ where usedLabs.All(ul => ul.Id.Value != v.ToString())
public static IEnumerable<string> SelectUnusedYears() public static IEnumerable<string> SelectUnusedYears()
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
var years = Build.SelectBuildYears(); var years = Build.SelectBuildYears();
@ -120,7 +120,7 @@ where usedYears.All(ul => ul.Id.Value != y.ToString())
[DataObjectMethod(DataObjectMethodType.Insert, true)] [DataObjectMethod(DataObjectMethodType.Insert, true)]
public static void Insert(MetaItem item) public static void Insert(MetaItem item)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
client.Store(item); client.Store(item);
@ -130,7 +130,7 @@ public static void Insert(MetaItem item)
[DataObjectMethod(DataObjectMethodType.Update, true)] [DataObjectMethod(DataObjectMethodType.Update, true)]
public static void Update(MetaItem item) public static void Update(MetaItem item)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
client.Store(item); client.Store(item);
@ -140,7 +140,7 @@ public static void Update(MetaItem item)
[DataObjectMethod(DataObjectMethodType.Insert, false)] [DataObjectMethod(DataObjectMethodType.Insert, false)]
public static void InsertAll(IEnumerable<MetaItem> items) public static void InsertAll(IEnumerable<MetaItem> items)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
client.StoreAll(items); client.StoreAll(items);
@ -150,7 +150,7 @@ public static void InsertAll(IEnumerable<MetaItem> items)
[DataObjectMethod(DataObjectMethodType.Delete, true)] [DataObjectMethod(DataObjectMethodType.Delete, true)]
public static void DeleteById(long id) public static void DeleteById(long id)
{ {
using (RedisClient rClient = new RedisClient(DatabaseConfig.Host, DatabaseConfig.Port, db: DatabaseConfig.Database)) using (RedisClient rClient = new RedisClient(MongoConfig.Host, MongoConfig.Port, db: MongoConfig.Database))
{ {
var client = rClient.As<MetaItem>(); var client = rClient.As<MetaItem>();
client.DeleteById(id); client.DeleteById(id);

View File

@ -0,0 +1,46 @@
using NServiceKit.DataAnnotations;
using NServiceKit.DesignPatterns.Model;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace RedisMongoMigration.Redis
{
[DataObject]
public class MetaItem : IHasId<MetaItemKey>
{
[Key]
[Index]
public MetaItemKey Id { get; set; }
public string PageContent { get; set; }
public string MetaDescription { get; set; }
}
public struct MetaItemKey
{
public string Value { get; set; }
public MetaType Type { get; set; }
public MetaItemKey(string id)
{
var items = id.Split(':');
Type = (MetaType)Enum.Parse(typeof(MetaType), items[0]);
Value = items[1];
}
public override string ToString()
{
return $"{Type}:{Value}";
}
}
public enum MetaType
{
Lab,
Version,
Source,
Year
}
}

View File

@ -0,0 +1,36 @@
using NServiceKit.DataAnnotations;
using NServiceKit.DesignPatterns.Model;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace RedisMongoMigration.Redis
{
[DataObject]
public class RedisMember : IHasId<Guid>
{
[Key]
[Index]
public Guid Id { get; set; }
[Key]
public string UserName { get; set; }
public byte[] PassHash { get; set; }
public byte[] PassSalt { get; set; }
[Key]
public string EmailAddress { get; set; }
public bool IsApproved { get; set; }
public bool IsLockedOut { get; set; }
public DateTime CreationDate { get; set; }
public DateTime LastActivityDate { get; set; }
public DateTime LastLockoutDate { get; set; }
public DateTime LastLoginDate { get; set; }
public DateTime LockoutWindowStart { get; set; }
public int LockoutWindowAttempts { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using NServiceKit.DataAnnotations;
using NServiceKit.DesignPatterns.Model;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace RedisMongoMigration.Redis
{
[DataObject]
public class RedisRole : IHasId<Guid>
{
[Key]
[Index]
public Guid Id { get; set; }
[Key]
public string RoleName { get; set; }
public Guid[] Users { get; set; }
}
}

View File

@ -63,6 +63,9 @@
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Redis\Build.cs" /> <Compile Include="Redis\Build.cs" />
<Compile Include="Redis\MetaItem.cs" />
<Compile Include="Redis\RedisMember.cs" />
<Compile Include="Redis\RedisRole.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />