-
-
Save codeimpossible/1571835 to your computer and use it in GitHub Desktop.
Massive Fluently rough draft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Dynamic; | |
using System.Linq; | |
using Massive; | |
using Massive.Expressions; | |
public class Test | |
{ | |
public void TestMethod( ) | |
{ | |
MassiveFluently.Defaults(m => m.PrimaryKeyField = "Id"); | |
var items = MassiveFluently.ConnectTo("MyConnection").ProductsTable.all(); | |
var items2 = (All)MassiveFluently.ConnectTo("SomeConnection").Products; | |
} | |
} | |
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 DynamicModel ConnectTo(string connectionStringName) | |
{ | |
return new MassiveContextBase(connectionStringName); | |
} | |
public static DynamicModel GetContext(string tableName, string primaryKeyColumn) | |
{ | |
return new MassiveContextBase(_connectionStringName, tableName, primaryKeyColumn); | |
} | |
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