Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Validation

  • Loading branch information...
commit 459bc1c9a5307ff16205d3dc3f166560ccabddef 1 parent 9a70fc0
@codeimpossible authored
Showing with 21 additions and 4 deletions.
  1. +19 −2 MassiveRecord.cs
  2. +2 −2 README.md
View
21 MassiveRecord.cs
@@ -1,5 +1,6 @@
namespace MassiveRecord {
using System;
+ using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
@@ -11,20 +12,24 @@ public enum FilterType { BeforeSave, BeforeDelete }
public class MassiveContextBase : DynamicModel {
private IDictionary<FilterType, List<Action<dynamic>>> bound_filters = null;
+ private IDictionary<String, Func<dynamic, bool>> bound_validators = null;
public MassiveContextBase( MassiveRecord.DynamicTable.ISettings config ) :
this( config.ConnectionString ) {
TableName = config.Table;
PrimaryKeyField = config.PrimaryKey;
bound_filters = config.Filters;
+ bound_validators = config.Validators;
}
public MassiveContextBase( string connectionStringName,
- IDictionary<FilterType, List<Action<dynamic>>> filters = null ) :
+ IDictionary<FilterType, List<Action<dynamic>>> filters = null,
+ IDictionary<String, Func<dynamic, bool>> validators = null ) :
base( connectionStringName ) {
bound_filters = filters ?? new Dictionary<FilterType, List<Action<dynamic>>> {
{ FilterType.BeforeSave, new List<Action<dynamic>>() },
{ FilterType.BeforeDelete, new List<Action<dynamic>>() }
};
+ bound_validators = validators ?? new Dictionary<String, Func<dynamic, bool>>();
}
public void RegisterFilter( FilterType type, Action<dynamic> filter ) {
@@ -41,6 +46,10 @@ public class MassiveContextBase : DynamicModel {
return true;
}
+ public override void Validate( dynamic item ) {
+ Errors = Errors.Concat( bound_validators.Where( v => v.Value(item) ).Select( v => v.Key )).ToList();
+ }
+
public override bool TryInvokeMember( System.Dynamic.InvokeMemberBinder binder, object[] args, out object result ) {
if( binder.Name.ToLower().StartsWith( "findby" ) ) {
var where = new StringBuilder();
@@ -86,12 +95,18 @@ public interface ISettings {
ISettings BeforeSave( Action<dynamic> filter );
ISettings BeforeDelete( Action<dynamic> filter );
- IDictionary<FilterType, List<Action<dynamic>>> Filters { get; }
+ IDictionary<FilterType, List<Action<dynamic>>> Filters { get; }
+ IDictionary<String, Func<dynamic, bool>> Validators { get; set; }
}
// configurator implements them all we just cast it around
public class DynamicTableConfigurator : IWhenAskedFor, IUse, ISettings {
private IDictionary<FilterType, List<Action<dynamic>>> filters = new Dictionary<FilterType, List<Action<dynamic>>>();
+
+ public DynamicTableConfigurator() {
+ Validators = new Dictionary<String, Func<dynamic, bool>>();
+ }
+
public IUse WhenAskedFor( string table ) { Type = table; return this; }
public ISettings Use( Action<ISettings> use ) {
@@ -106,6 +121,8 @@ public class DynamicTableConfigurator : IWhenAskedFor, IUse, ISettings {
public IDictionary<FilterType, List<Action<dynamic>>> Filters { get { return filters; } }
+ public IDictionary<String, Func<dynamic, bool>> Validators { get; set; }
+
public ISettings BeforeSave( Action<dynamic> filter ) {
return AddFilter( FilterType.BeforeSave, filter );
}
View
4 README.md
@@ -47,9 +47,9 @@ What if you wanted to specify a configuration that MassiveRecord should use ever
s.PrimaryKey = "ContactID";
s.Table = "Person.Contact";
}));
-
+
// in your controller or other class file do the following
- var usersTable = DynamicTable.Create("Users");
+ var usersTable = DynamicTable.Create("Users");
var users = usersTable.FindByEmail("myuser@test.com"); // BOOM!
```
Please sign in to comment.
Something went wrong with that request. Please try again.