Skip to content

Instantly share code, notes, and snippets.

@jbubriski
Forked from codeimpossible/MassiveFluently.cs
Created January 6, 2012 18:45
Show Gist options
  • Save jbubriski/1571866 to your computer and use it in GitHub Desktop.
Save jbubriski/1571866 to your computer and use it in GitHub Desktop.
Massive Fluently rough draft
using System;
using System.Dynamic;
using System.Linq;
using Massive;
using Massive.Expressions;
public class Test
{
public void TestMethod()
{
// You can change the default PrimaryKeyField
MassiveFluently.Defaults(m => m.PrimaryKeyField = "Id");
var items = MassiveFluently.ConnectTo("MyConnection").ProductsTable.all();
var items2 = (All)MassiveFluently.ConnectTo("SomeConnection").Products;
// After you set the connection string statically, you shouldn't need to specify it.
MassiveFluently._connectionStringName = "MyConnection";
var items3 = MassiveFluently.From().ProductsTable.all();
}
}
namespace Massive.Expressions
{
public class All { }
public class First { }
public class Last { }
}
namespace Massive
{
///<example>
/// MassiveFluently.Defaults( m => m.PrimaryKey = "Id" );
///
/// new MassiveFluently()
/// .ConnectTo("MyConnection")
/// .ProductsTable
/// .all();
///
/// OR
///
/// var items = (All) new MassiveFluently().ConnectTo("SomeConnection").Products;
///</example>
public class MassiveFluently : DynamicObject
{
private static Action<DynamicModel> _defaultSettings = (m) => m.PrimaryKeyField = "Id";
internal static string _connectionStringName = null;
private MassiveFluently(string connectionStringName)
{
_connectionStringName = connectionStringName;
}
public static void Defaults(Action<DynamicModel> setDefaults)
{
_defaultSettings = setDefaults;
}
public static dynamic ConnectTo(string connectionStringName)
{
return new MassiveContextBase(connectionStringName);
}
public static dynamic GetContext(string tableName, string primaryKeyColumn)
{
return new MassiveContextBase(_connectionStringName, tableName, primaryKeyColumn);
}
public static dynamic From()
{
return new MassiveContextBase(_connectionStringName);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var table = binder.Name;
if (binder.Name.EndsWith("Table"))
{
table = binder.Name.Replace("Table", "");
}
result = new MassiveContextBase(table);
return true;
}
public class MassiveContextBase : DynamicModel
{
internal MassiveContextBase()
: base(MassiveFluently._connectionStringName)
{
MassiveFluently._defaultSettings(this);
}
internal MassiveContextBase(string tableName)
: this()
{
TableName = tableName;
}
internal MassiveContextBase(string connectionStringName, string tableName, string primaryKeyField) :
base(connectionStringName)
{
TableName = tableName;
PrimaryKeyField = primaryKeyField;
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.Type == typeof(Massive.Expressions.All))
{
result = this.All();
}
if (binder.Type == typeof(Massive.Expressions.First))
{
result = this.All(limit: 1);
}
if (binder.Type == typeof(Massive.Expressions.Last))
{
result = this.All(limit: 1, orderBy: PrimaryKeyField + " DESC").First();
}
return base.TryConvert(binder, out result);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment