Permalink
Browse files

First attempt at SqlRepository using PetaPoco

  • Loading branch information...
1 parent 75ec8d5 commit 12c155fa2d77fddb31282a3a0214af2abb51530f @ChrisFulstow ChrisFulstow committed Apr 24, 2011
@@ -1,8 +1,4 @@
using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-using System.Web;
using NBlog.Web.Application.Service;
using NBlog.Web.Application.Storage;
using NBlog.Web.Application.Storage.Json;
@@ -1,4 +1,5 @@
using System;
+using PetaPoco;
namespace NBlog.Web.Application.Service.Entity
{
@@ -1,5 +1,4 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
@@ -8,13 +7,11 @@ namespace NBlog.Web.Application.Storage
{
public interface IRepository
{
- TEntity Single<TEntity, TKey>(TKey key);
- void Save<TEntity>(TEntity item);
-
- IQueryable<TEntity> All<TEntity>();
-
- bool Exists<TEntity>(string key);
- bool Exists<TEntity, TKey>(TKey key);
+ TEntity Single<TEntity, TKey>(TKey key) where TEntity : new();
+ IEnumerable<TEntity> All<TEntity>() where TEntity : new();
+ bool Exists<TEntity>(string key) where TEntity : class, new();
+ bool Exists<TEntity, TKey>(TKey key) where TEntity : new();
+ void Save<TEntity>(TEntity item) where TEntity : class, new();
void Delete<TEntity, TKey>(TKey key);
}
}
@@ -1,44 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
-using System.Linq;
-using System.Linq.Expressions;
-using System.Web;
-using AppLimit.CloudComputing.SharpBox;
-using AppLimit.CloudComputing.SharpBox.DropBox;
-using Ionic.Zip;
using NBlog.Web.Application.Service.Entity;
using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
namespace NBlog.Web.Application.Storage.Json
{
public class JsonRepository : IRepository
{
+ private readonly RepositoryKeys _keys;
private readonly string _dataPath;
- private readonly Dictionary<Type, Func<object, string>> _keys = new Dictionary<Type, Func<object, string>>();
- public JsonRepository(string dataPath)
+ public JsonRepository(RepositoryKeys keys, string dataPath)
{
+ _keys = keys;
_dataPath = dataPath;
-
- // todo: make this an external configuration
- RegisterKey<Entry>(e => e.Slug);
- RegisterKey<Config>(e => e.Site);
- RegisterKey<User>(e => e.Username);
}
public string DataPath { get { return _dataPath; } }
- public TEntity Single<TEntity>(string key)
+ public TEntity Single<TEntity>(string key) where TEntity : new()
{
return Single<TEntity, string>(key);
}
- public TEntity Single<TEntity, TKey>(TKey key)
+ public TEntity Single<TEntity, TKey>(TKey key) where TEntity : new()
{
var filename = key.ToString();
var recordPath = Path.Combine(_dataPath, typeof(TEntity).Name, filename + ".json");
@@ -48,7 +37,7 @@ public TEntity Single<TEntity>(string key)
}
- public IQueryable<TEntity> All<TEntity>()
+ public IEnumerable<TEntity> All<TEntity>() where TEntity : new()
{
var folderPath = Path.Combine(_dataPath, typeof(TEntity).Name);
var filePaths = Directory.GetFiles(folderPath, "*.json", SearchOption.TopDirectoryOnly);
@@ -61,28 +50,28 @@ public IQueryable<TEntity> All<TEntity>()
list.Add(entity);
}
- return list.AsQueryable();
+ return list;
}
- public void Save<TEntity>(TEntity item)
+ public void Save<TEntity>(TEntity item) where TEntity : class, new()
{
var json = JsonConvert.SerializeObject(item, Formatting.Indented);
var folderPath = GetEntityPath<TEntity>();
if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); }
- var filename = GetKeyValue(item);
+ var filename = _keys.GetKeyValue(item);
var recordPath = Path.Combine(folderPath, filename + ".json");
File.WriteAllText(recordPath, json);
}
- public bool Exists<TEntity>(string key)
+ public bool Exists<TEntity>(string key) where TEntity : class, new()
{
return Exists<TEntity, string>(key);
}
- public bool Exists<TEntity, TKey>(TKey key)
+ public bool Exists<TEntity, TKey>(TKey key) where TEntity : new()
{
var folderPath = GetEntityPath<TEntity>();
var recordPath = Path.Combine(folderPath, key + ".json");
@@ -105,16 +94,6 @@ public void DeleteAll<TEntity>()
File.Delete(recordPath);
}
- private void RegisterKey<T>(Func<T, string> key)
- {
- _keys.Add(typeof(T), f => key((T)f));
- }
-
-
- private string GetKeyValue<T>(T item)
- {
- return _keys[typeof(T)](item);
- }
// todo: need a TEntity + TKey version of this too that does Path.Combine(folderPath, key + ".json");
private string GetEntityPath<TEntity>()
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+using System.Reflection;
+using System.Linq.Expressions;
+
+namespace NBlog.Web.Application.Storage
+{
+ public class RepositoryKeys
+ {
+ private readonly Dictionary<Type, Expression<Func<object, object>>> _keys = new Dictionary<Type, Expression<Func<object, object>>>();
+
+ public void Add<T>(Expression<Func<T, object>> expression)
+ {
+ // use a conversion expression:
+
+ Expression<Func<object, T>> convert = myObj => (T)myObj;
+
+ var param = Expression.Parameter(typeof(object), "obj"); // don't need name?
+ var body = Expression.Invoke(expression, Expression.Invoke(convert, param));
+ var lambda = Expression.Lambda<Func<object, object>>(body, param);
+
+
+ //var objectParam = new[] { Expression.Parameter(typeof(object), expression.Parameters[0].Name) };
+ //var convertedExpression = Expression.Lambda<Func<object, object>>(expression.Body, objectParam);
+
+
+ _keys.Add(typeof(T), lambda);
+ }
+
+
+ public static Expression<Func<TModel, TToProperty>> Cast<TModel, TFromProperty, TToProperty>(Expression<Func<TModel, TFromProperty>> expression)
+ {
+ Expression converted = Expression.Convert(expression.Body, typeof(TToProperty));
+
+ return Expression.Lambda<Func<TModel, TToProperty>>(converted, expression.Parameters);
+ }
+
+
+ public object GetKeyValue<T>(T item)
+ {
+ var getValue = _keys[typeof(T)].Compile();
+ return getValue(item);
+ }
+
+ public string GetKeyName<T>(T item)
+ {
+ return GetKeyName<T>();
+ }
+
+ public string GetKeyName<T>()
+ {
+ var expression = _keys[typeof(T)];
+
+ var conversionBody = (InvocationExpression)expression.Body;
+ var conversionExpression = (Expression<Func<T, object>>)conversionBody.Expression;
+ var body = (MemberExpression)conversionExpression.Body;
+ var memberName = body.Member.Name;
+ return memberName;
+ }
+
+ //private static string GetMemberName<T>(Expression<Func<object, object>> expression)
+ //{
+ // var conversionBody = (InvocationExpression)expression.Body;
+ // var conversionExpression = (Expression<Func<T, object>>)conversionBody.Expression;
+ // var body = (MemberExpression)conversionExpression.Body;
+ // var memberName = body.Member.Name;
+ // return memberName;
+ //}
+ }
+}
Oops, something went wrong.

0 comments on commit 12c155f

Please sign in to comment.