Hi all,
I'm not sure if RejectChanges() is the right method to accomplish this so if not please help. Well, this is my scenario.
1) We have an entity and validator.
2) We create a new entity, attach validator and persist to the database.
3) We change a field that would cause the validation to fail, attempt save and it fails.
4) We change the same field again so that when saving the validation will fail.
5) We then call RejectChanges().
6) The entity goes back to !IsDirty.
7) BUT, the last bad value is still in the entity field ... so the entity should actually be dirty.
Am I missing something?
Some test code to explain... I'll try to attatch it as well.
namespace LSSF.CoreTest.BMTests
{
[TestFixture]
public class Bug
{
private const string STR_ValidationFailed = "validation failed";
private class UserValidator : ValidatorBase
{
public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
{
UserEntity lUserEntity = (UserEntity) involvedEntity;
if (lUserEntity.UserName.Length > 20)
{
lUserEntity.SetEntityFieldError("UserName", "must be 20 chars or less", true);
lUserEntity.SetEntityError(STR_ValidationFailed);
throw new ORMEntityValidationException(STR_ValidationFailed, involvedEntity);
}
}
}
[Test]
public void T010_M_ValidationError()
{
// UserName varchar(30), not null
string lUserName = "N" + DateTime.Now.Ticks.ToString();
// Create and save a new entity
UserEntity lUserEntity = new UserEntity(Guid.NewGuid());
lUserEntity.UserName = lUserName;
lUserEntity.Secret = "hash";
lUserEntity.Validator = new UserValidator();
bool lResult = TestHelper.GetInstance().DAA.SaveEntity(lUserEntity, true);
Assert.IsTrue(lResult);
Assert.IsFalse(lUserEntity.IsDirty);
Console.WriteLine("Created user : {0}", lUserEntity.UserName);
// Validation failure on username first time
try
{
lUserEntity.UserName = new string('a', 30);
TestHelper.GetInstance().DAA.SaveEntity(lUserEntity, true);
}
catch (ORMEntityValidationException ex)
{
Assert.AreEqual(STR_ValidationFailed, ex.Message);
}
catch (Exception ex)
{
Assert.Fail("Should have gotten a ORMEntityValicationException here but was an {0} with message {1}.", ex.GetType(), ex.Message);
}
Assert.IsTrue(lUserEntity.IsDirty);
// Validation failure on username the second time
try
{
lUserEntity.UserName = new string('b', 30);
TestHelper.GetInstance().DAA.SaveEntity(lUserEntity, true);
}
catch (ORMEntityValidationException ex)
{
Assert.AreEqual(STR_ValidationFailed, ex.Message);
}
catch (Exception ex)
{
Assert.Fail("Should have gotten a ORMEntityValicationException here but was an {0} with message {1}.", ex.GetType(), ex.Message);
}
Assert.IsTrue(lUserEntity.IsDirty);
// Reject the changes
lUserEntity.RejectChanges();
Assert.IsFalse(lUserEntity.IsDirty);
Assert.AreEqual(lUserName, lUserEntity.UserName, "But the username is still the old rejected value!");
Console.WriteLine("Validation errors as expected, UserName back to normal.");
}
}
}
Attachments
Filename |
File size |
Added on |
Approval |
Bug.cs
|
2,690 |
19-Mar-2007 01:54.04 |
Approved |