Skip to content

Commit

Permalink
- Update to NHibernate 3.1.0
Browse files Browse the repository at this point in the history
- NHibernate helper

git-svn-id: https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk@1662 d7b3437e-3345-0410-94a8-cbd290e69f67
  • Loading branch information
darioquintana committed Mar 11, 2011
1 parent 894ce58 commit 194fb85
Show file tree
Hide file tree
Showing 12 changed files with 1,077 additions and 494 deletions.
Binary file modified src/NHibernate.Validator/lib/Iesi.Collections.dll
Binary file not shown.
Binary file modified src/NHibernate.Validator/lib/NHibernate.ByteCode.Castle.dll
Binary file not shown.
Binary file modified src/NHibernate.Validator/lib/NHibernate.ByteCode.LinFu.dll
Binary file not shown.
Binary file modified src/NHibernate.Validator/lib/NHibernate.dll
Binary file not shown.
1,448 changes: 972 additions & 476 deletions src/NHibernate.Validator/lib/NHibernate.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/NHibernate.Validator/lib/nhibernate-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@
<xs:element ref="one-to-one" />
<xs:element ref="component" />
<xs:element ref="dynamic-component" />
<xs:element ref="properties" />
<xs:element ref="any" />
<xs:element ref="map" />
<xs:element ref="set" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NHibernate.Validator.Cfg;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Event;
using NHibernate.Validator.Util;
using NUnit.Framework;
using System.Collections.Generic;

Expand Down Expand Up @@ -80,13 +81,13 @@ public void NoInitializeAfterFetch()

// This two lines are not needed for tests because is a NH matter
// we use it only to check where the problem really is
Assert.IsFalse(NHibernateUtil.IsInitialized(p.Children));
Assert.IsFalse(NHibernateUtil.IsInitialized(p.Friends));
Assert.IsFalse(NHibernateHelper.IsInitialized(p.Children));
Assert.IsFalse(NHibernateHelper.IsInitialized(p.Friends));

vengine.Validate(p);

Assert.IsFalse(NHibernateUtil.IsInitialized(p.Children));
Assert.IsFalse(NHibernateUtil.IsInitialized(p.Friends));
Assert.IsFalse(NHibernateHelper.IsInitialized(p.Children));
Assert.IsFalse(NHibernateHelper.IsInitialized(p.Friends));
}

// No initialized many-to-one
Expand All @@ -97,23 +98,23 @@ public void NoInitializeAfterFetch()

// This line are not needed for tests because is a NH matter
// we use it only to check where the problem really is
Assert.IsFalse(NHibernateUtil.IsInitialized(p.Parent));
Assert.IsFalse(NHibernateHelper.IsInitialized(p.Parent));

vengine.Validate(p);

Assert.IsFalse(NHibernateUtil.IsInitialized(p.Parent));
Assert.IsFalse(NHibernateHelper.IsInitialized(p.Parent));
}

// No initialized the proxie it self
using (ISession s = OpenSession())
{
Person p = s.Load<Person>(savedId);

Assert.IsFalse(NHibernateUtil.IsInitialized(p));
Assert.IsFalse(NHibernateHelper.IsInitialized(p));

vengine.Validate(p);

Assert.IsFalse(NHibernateUtil.IsInitialized(p));
Assert.IsFalse(NHibernateHelper.IsInitialized(p));
}

CleanUp();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Util;
using NUnit.Framework;

namespace NHibernate.Validator.Tests.Integration
Expand Down Expand Up @@ -30,7 +31,7 @@ public void ReturnTheCorrectType()
}
var ei = new DefaultEntityTypeInspector();
Assert.That(ei.GuessType(proxy), Is.EqualTo(typeof (AnEntity)));
Assert.That(!NHibernateUtil.IsInitialized(proxy), "should not initialize the proxy");
Assert.That(!NHibernateHelper.IsInitialized(proxy), "should not initialize the proxy");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using NHibernate.Validator.Cfg.Loquacious;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Util;
using NUnit.Framework;

namespace NHibernate.Validator.Tests.Integration
Expand Down Expand Up @@ -74,7 +75,7 @@ public void ValidateNotInitializeProxyAtFirstLevel()
var proxy = s.Load<SimpleWithRelation>(savedId);
Assert.That(engine.IsValid(proxy));
Assert.DoesNotThrow(() => engine.AssertValid(proxy));
Assert.That(!NHibernateUtil.IsInitialized(proxy), "should not initialize the proxy");
Assert.That(!NHibernateHelper.IsInitialized(proxy), "should not initialize the proxy");
}

CleanDb();
Expand Down Expand Up @@ -238,7 +239,7 @@ public void ValidateNotInitializeProxyAtDeepLevel()
var proxy = s.Load<Relation>(savedIdRelation);
Assert.That(engine.IsValid(new SimpleWithRelation { Name = "OK", Relation = proxy }));
Assert.DoesNotThrow(() => engine.AssertValid(new SimpleWithRelation {Name = "OK", Relation = proxy}));
Assert.That(!NHibernateUtil.IsInitialized(proxy), "should not initialize the proxy");
Assert.That(!NHibernateHelper.IsInitialized(proxy), "should not initialize the proxy");
}

CleanDb();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,19 @@ private IEnumerable<InvalidValue> GetInvalidValues(object entity, HashSet<object
private IEnumerable<InvalidValue> GetChildrenInvalidValues(object entity, string memberName, HashSet<object> circularityState, ICollection<object> activeTags)
{
return from member in childGetters
where memberName.Equals(member.Name) && NHibernateUtil.IsPropertyInitialized(entity, member.Name)
where memberName.Equals(member.Name) && NHibernateHelper.IsPropertyInitialized(entity, member.Name)
let value = TypeUtils.GetMemberValue(entity, member)
where value != null && (NHibernateUtil.IsInitialized(value) || value is AbstractPersistentCollection)
where value != null && (NHibernateHelper.IsInitialized(value) || value is AbstractPersistentCollection)
from invalidValue in ChildInvalidValues(value, entity, member, circularityState, activeTags)
select invalidValue;
}

private IEnumerable<InvalidValue> GetChildrenInvalidValues(object entity, HashSet<object> circularityState, ICollection<object> activeTags)
{
return from member in childGetters
where NHibernateUtil.IsPropertyInitialized(entity, member.Name)
where NHibernateHelper.IsPropertyInitialized(entity, member.Name)
let value = TypeUtils.GetMemberValue(entity, member)
where value != null && (NHibernateUtil.IsInitialized(value) || value is AbstractPersistentCollection)
where value != null && (NHibernateHelper.IsInitialized(value) || value is AbstractPersistentCollection)
from invalidValue in ChildInvalidValues(value, entity, member, circularityState, activeTags)
select invalidValue;
}
Expand Down Expand Up @@ -328,7 +328,7 @@ private bool IsValidationNeededByTags(ICollection<object> activeTags, ICollectio
private IEnumerable<InvalidValue> ChildInvalidValues(object value, object entity, MemberInfo member, HashSet<object> circularityState, ICollection<object> activeTags)
{
var valueEnum = value as IEnumerable;
if (!NHibernateUtil.IsInitialized(value) && value is AbstractPersistentCollection)
if (!NHibernateHelper.IsInitialized(value) && value is AbstractPersistentCollection)
{
valueEnum = (value as AbstractPersistentCollection).QueuedAdditionIterator;
}
Expand Down Expand Up @@ -690,9 +690,9 @@ private IEnumerable<InvalidValue> MembersInvalidValues(object entity, string mem

return from mtv in membersValidators
let member = mtv.Getter
where NHibernateUtil.IsPropertyInitialized(entity, member.Name)
where NHibernateHelper.IsPropertyInitialized(entity, member.Name)
let value = TypeUtils.GetMemberValue(entity, member)
where IsValidationNeededByTags(activeTags, mtv.ValidatorDef.Tags) && NHibernateUtil.IsInitialized(value)
where IsValidationNeededByTags(activeTags, mtv.ValidatorDef.Tags) && NHibernateHelper.IsInitialized(value)
let validator = mtv.ValidatorDef.Validator
let constraintContext = new ConstraintValidatorContext(member.Name, defaultInterpolator.GetAttributeMessage(validator))
where !validator.IsValid(value, constraintContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="Engine\ReferenceEqualityComparer.cs" />
<Compile Include="Engine\SystemTypeExtensions.cs" />
<Compile Include="Event\ValidatePreCollectionUpdateEventListener.cs" />
<Compile Include="Util\NHibernateHelper.cs" />
<Compile Include="Util\EnumerableExtensions.cs" />
<Compile Include="Constraints\IsNumericAttribute.cs" />
<Compile Include="Constraints\WithinAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using NHibernate.Bytecode;
using NHibernate.Collection;
using NHibernate.Intercept;
using NHibernate.Proxy;

namespace NHibernate.Validator.Util
{
/// <summary>
/// Ported from NHibernate with changes
/// </summary>
public class NHibernateHelper
{
public static bool IsPropertyInitialized(object proxy, string propertyName)
{
object entity;

if (!IsProxyFactoryConfigurated())
{
//if the proxy provider it's not configurated, can't be a proxy neither an instrumented field.
return true;
}

if (proxy.IsProxy())
{
ILazyInitializer li = ((INHibernateProxy)proxy).HibernateLazyInitializer;
if (li.IsUninitialized)
{
return false;
}
else
{
entity = li.GetImplementation();
}
}
else
{
entity = proxy;
}

if (FieldInterceptionHelper.IsInstrumented(entity))
{
IFieldInterceptor interceptor = FieldInterceptionHelper.ExtractFieldInterceptor(entity);
return interceptor == null || interceptor.IsInitializedField(propertyName);
}
else
{
return true;
}
}

public static bool IsInitialized(object proxy)
{
var noProxyFactory = IsProxyFactoryConfigurated();

if (noProxyFactory && proxy.IsProxy())
{
return !((INHibernateProxy)proxy).HibernateLazyInitializer.IsUninitialized;
}
else if (proxy is IPersistentCollection)
{
return ((IPersistentCollection)proxy).WasInitialized;
}
else
{
return true;
}
}

public static bool IsProxyFactoryConfigurated()
{
try
{
var f = NHibernate.Cfg.Environment.BytecodeProvider.ProxyFactoryFactory;
return true;
}
catch (ProxyFactoryFactoryNotConfiguredException)
{
return false;
}
}
}
}

0 comments on commit 194fb85

Please sign in to comment.