Skip to content

Commit

Permalink
breeze.net - more validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraband committed Mar 11, 2014
1 parent 10f260e commit dbf53a4
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Breeze.Client/Intellisense/breeze.intellisense.js
@@ -1,4 +1,4 @@
// Generated on: Sun Mar 09 2014 15:53:05 GMT-0700 (Pacific Daylight Time)
// Generated on: Mon Mar 10 2014 18:49:21 GMT-0700 (Pacific Daylight Time)

intellisense.annotate(breeze.core, {

Expand Down
2 changes: 1 addition & 1 deletion Breeze.Client/Scripts/breeze.intellisense.js
@@ -1,4 +1,4 @@
// Generated on: Sun Mar 09 2014 15:53:05 GMT-0700 (Pacific Daylight Time)
// Generated on: Mon Mar 10 2014 18:49:21 GMT-0700 (Pacific Daylight Time)

intellisense.annotate(breeze.core, {

Expand Down
2 changes: 1 addition & 1 deletion Breeze.NetClient/DataProperty.cs
Expand Up @@ -51,7 +51,7 @@ public DataProperty(DataProperty dp)
MaxLength = jNode.Get<int?>("maxLength");
EnumTypeName = jNode.Get<String>("enumType");
IsScalar = jNode.Get<bool>("isScalar", true);
_validators = new ValidatorCollection(jNode.GetJNodeArray("validators").Select(jn => Validator.FindOrCreate(jn)));
_validators = new ValidatorCollection(jNode.GetJNodeArray("validators"));
}

JNode IJsonSerializable.ToJNode(Object config) {
Expand Down
2 changes: 1 addition & 1 deletion Breeze.NetClient/EntityType.cs
Expand Up @@ -23,7 +23,7 @@ public class EntityType : StructuralType, IJsonSerializable {
}
jNode.GetJNodeArray("dataProperties").Select(jn => new DataProperty(jn)).ForEach(dp => AddDataProperty(dp));
jNode.GetJNodeArray("navigationProperties").Select(jn => new NavigationProperty(jn)).ForEach(np => AddNavigationProperty(np));
jNode.GetJNodeArray("validators").Select(jn => Validator.FindOrCreate(jn)).ToList();
_validators = new ValidatorCollection(jNode.GetJNodeArray("validators"));
// custom
}

Expand Down
34 changes: 21 additions & 13 deletions Breeze.NetClient/MetadataStore.cs
Expand Up @@ -67,6 +67,22 @@ public class MetadataStore : IJsonSerializable {
}
}

public IEnumerable<Exception> Errors {
get { return _errors.ToList(); }
}

#endregion

#region Public methods

public static void __Reset() {
lock (__lock) {
var x = __instance._probedAssemblies;
__instance = new MetadataStore();
__instance.ProbeAssemblies(x.ToArray());
}
}

public bool ProbeAssemblies(params Assembly[] assembliesToProbe) {
lock (_structuralTypes) {
var assemblies = assembliesToProbe.Except(_probedAssemblies).ToList();
Expand All @@ -88,18 +104,6 @@ public class MetadataStore : IJsonSerializable {
_typeDiscoveryActions.Add(Tuple.Create(type, action));
}

#endregion

#region Public methods

public static void __Reset() {
lock (__lock) {
var x = __instance._probedAssemblies;
__instance = new MetadataStore();
__instance.ProbeAssemblies(x.ToArray());
}
}

public async Task<DataService> FetchMetadata(DataService dataService) {
String serviceName;

Expand Down Expand Up @@ -330,7 +334,9 @@ public class MetadataStore : IJsonSerializable {
var vrName = jNode.Get<String>("name");
Type vrType;
if (!_validatorMap.TryGetValue(vrName, out vrType)) {
throw new Exception("Unable to create a validator for " + vrName);
var e = new Exception("Unable to create a validator for " + vrName);
_errors.Add(e);
return null;
}
// Deserialize the object
var vr = (Validator)jNode.ToObject(vrType, true);
Expand Down Expand Up @@ -630,6 +636,8 @@ private class TypePair {
private Dictionary<JNode, Validator> _validatorJNodeCache = new Dictionary<JNode, Validator>();
private Dictionary<String, Type> _validatorMap = new Dictionary<string, Type>();

private List<Exception> _errors = new List<Exception>();

#endregion

}
Expand Down
2 changes: 1 addition & 1 deletion Breeze.NetClient/NavigationProperty.cs
Expand Up @@ -31,7 +31,7 @@ public NavigationProperty(NavigationProperty np)
EntityTypeName = jNode.Get<String>("entityTypeName");
IsScalar = jNode.Get<bool>("isScalar", true);
AssociationName = jNode.Get<String>("associationName");
_validators = new ValidatorCollection(jNode.GetJNodeArray("validators").Select(jn => Validator.FindOrCreate(jn)));
_validators = new ValidatorCollection(jNode.GetJNodeArray("validators"));
_foreignKeyNames.AddRange(jNode.GetArray<String>("foreignKeyNames"));
_invForeignKeyNames.AddRange(jNode.GetArray<String>("invForeignKeyNames"));
// custom
Expand Down
6 changes: 5 additions & 1 deletion Breeze.NetClient/Validator.cs
Expand Up @@ -196,7 +196,11 @@ public class ValidatorCollection : SetCollection<Validator> {

public ValidatorCollection() : base() { }
public ValidatorCollection(IEnumerable<Validator> validators) : base(validators) { }

public ValidatorCollection(IEnumerable<JNode> jNodes) {
jNodes.Select(jn => Validator.FindOrCreate(jn))
.Where(v => v != null)
.ForEach(v => this.Add(v));
}

public override void Add(Validator item) {
item = item.Intern();
Expand Down
7 changes: 7 additions & 0 deletions Breeze.NetClient/__Notes.txt
Expand Up @@ -6,6 +6,13 @@

- EntityAspect EntityManager & EntityGroup properties are set if an entity was ever attached.
This is not cleared when an entity is detached. This assumption removes a lot of special purpose logic
- MetadataStore.Instance is a singleton - This is different than breeze.net because
1) We want to allow entities and complexObjects be created and manipulated before they are attached to an EntityManager. Once
we allow this we need somewhere for these new objects to get the metadata they need. For example, a detached
entity needs a definition for EntityKey that depends on a metadataStore.
2) Each clrType can only have a single representation per the .NET runtime. We need to associate each
clrType with a single EntityType or ComplexType. If we allowed a different version of the EntityType/Complex
type per EntityManager then we wouldn't have any easy way of working with detached entities
- Only the MetadataStore and immutable types are ThreadSafe. Any property returned by the MetadataStore
must therefore also be either immutable or thread safe itself. (all StructuralTypes are effectively immutable)

Expand Down
32 changes: 18 additions & 14 deletions Test_NetClient/ValidationTests.cs
Expand Up @@ -205,20 +205,24 @@ public class ValidationTests {
Assert.IsTrue(ve.Key != null);
}

//[TestMethod]
//public async Task CustomPropertyValidator() {
// await _emTask;

// var custType = MetadataStore.Instance.GetEntityType(typeof(Customer));
// var countryProp = custType.GetDataProperty("Country");
// countryProp.Validators.Add(new CountryIsUsValidator());
// var cust = new Customer();
// var valErrors = cust.EntityAspect.ValidationErrors;
// Assert.IsTrue(valErrors.Count == 0);
// cust.Country = "Germany";
// _em1.AttachEntity(cust);
// // MetadataStore.Instance.
//}
[TestMethod]
public async Task CustomPropertyValidator() {
await _emTask;

var custType = MetadataStore.Instance.GetEntityType(typeof(Customer));
var countryProp = custType.GetDataProperty("Country");
try {
countryProp.Validators.Add(new CountryIsUsValidator());
var cust = new Customer();
var valErrors = cust.EntityAspect.ValidationErrors;
Assert.IsTrue(valErrors.Count == 0);
cust.Country = "Germany";
_em1.AttachEntity(cust);
// MetadataStore.Instance.
} finally {
countryProp.Validators.Remove(new CountryIsUsValidator());
}
}

public class CountryIsUsValidator : Validator {
public CountryIsUsValidator()
Expand Down

0 comments on commit dbf53a4

Please sign in to comment.