I am trying to use NHibernate Envers to log an additional field "user". I have followed several code examples that seem to vary a bit when it comes to syntax, probably because some of them are a bit out of date. However I can't get it to work.
I'm getting this exception:
Only one property may have the attribute [RevisionNumber]!
My Custom Revision Entity:
public class CustomRevisionEntity
{
public virtual int Id { get; set; }
public virtual DateTime RevisionTimestamp { get; set; }
public virtual Guid UserIdentityId { get; set; }
public override bool Equals(object obj)
{
if (this == obj) return true;
var revisionEntity = obj as CustomRevisionEntity;
if (revisionEntity == null) return false;
var that = revisionEntity;
if (Id != that.Id) return false;
return RevisionTimestamp == that.RevisionTimestamp;
}
public override int GetHashCode()
{
var result = Id;
result = 31 * result + (int)(((ulong)RevisionTimestamp.Ticks) ^ (((ulong)RevisionTimestamp.Ticks) >> 32));
return result;
}
}
My IRevisionListener:
public class RevInfoListener : IRevisionListener
{
public void NewRevision(object revisionEntity)
{
var casted = revisionEntity as CustomRevisionEntity;
if (casted != null)
{
casted.UserIdentityId = Guid.NewGuid(); // TODO
}
}
}
First I use mapping by code to map the entity:
_modelMapper.Class<CustomRevisionEntity>(entity =>
{
entity.Property(x => x.Id);
entity.Property(x => x.RevisionTimestamp);
entity.Property(x => x.UserIdentityId);
});
Then I configure Envers and NHibernate
var enversConf = new FluentConfiguration();
enversConf.SetRevisionEntity<CustomRevisionEntity>(x => x.Id, x => x.RevisionTimestamp, new RevInfoListener());
enversConf.Audit<OrganizationEntity>().Exclude(x => x.Version);
configuration.IntegrateWithEnvers(enversConf); // This is the nh-configuration
The last line gives me the exception:
Only one property may have the attribute [RevisionNumber]!
Anyone have any ideas? Myself I would speculate that the default revision entity is still used somehow and when I try to register my custom revision entity this happens.
The error message occurred because the Id property was being mapped twice.
In our mapping class we had this
_modelMapper.BeforeMapClass += (modelInspector, type, classCustomizer) => classCustomizer.Id(type.GetProperty("Id"), (idMapper) =>
{
idMapper.Access(Accessor.Property);
idMapper.Generator(Generators.GuidComb);
});
Then we tried mapping Id again as a property of the CustomRevisionEntity
The final mapping:
_modelMapper.Class<CustomRevisionEntity>(entity =>
{
entity.Id<int>(x => x.Id, mapper => mapper.Generator(Generators.Identity));
entity.Property(x => x.RevisionDate);
entity.Property(x => x.UserIdentityId);
});
Related
I have WebAPI (.NET Core) and use FluentValidator to validate model, including updating.
I use PATCH verb and have the following method:
public IActionResult Update(int id, [FromBody] JsonPatchDocument<TollUpdateAPI> jsonPatchDocument)
{
also, I have a validator class:
public class TollUpdateFluentValidator : AbstractValidator<TollUpdateAPI>
{
public TollUpdateFluentValidator ()
{
RuleFor(d => d.Date)
.NotNull().WithMessage("Date is required");
RuleFor(d => d.DriverId)
.GreaterThan(0).WithMessage("Invalid DriverId");
RuleFor(d => d.Amount)
.NotNull().WithMessage("Amount is required");
RuleFor(d => d.Amount)
.GreaterThanOrEqualTo(0).WithMessage("Invalid Amount");
}
}
and map this validator in Startup class:
services.AddTransient<IValidator<TollUpdateAPI>, TollUpdateFluentValidator>();
but it does not work. How to write valid FluentValidator for my task?
You will need to trigger the validation manually.
Your action method will be somthing like this:
public IActionResult Update(int id, [FromBody] JsonPatchDocument<TollUpdateAPI> jsonPatchDocument)
{
// Load your db entity
var myDbEntity = myService.LoadEntityFromDb(id);
// Copy/Map data to the entity to patch using AutoMapper for example
var entityToPatch = myMapper.Map<TollUpdateAPI>(myDbEntity);
// Apply the patch to the entity to patch
jsonPatchDocument.ApplyTo(entityToPatch);
// Trigger validation manually
var validationResult = new TollUpdateFluentValidator().Validate(entityToPatch);
if (!validationResult.IsValid)
{
// Add validation errors to ModelState
foreach (var error in validationResult.Errors)
{
ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
}
// Patch failed, return 422 result
return UnprocessableEntity(ModelState);
}
// Map the patch to the dbEntity
myMapper.Map(entityToPatch, myDbEntity);
myService.SaveChangesToDb();
// So far so good, patch done
return NoContent();
}
You can utilize a custom rule builder for this. It might not be the most elegant way of handling it but at least the validation logic is where you expect it to be.
Say you have the following request model:
public class CarRequestModel
{
public string Make { get; set; }
public string Model { get; set; }
public decimal EngineDisplacement { get; set; }
}
Your Validator class can inherit from the AbstractValidator of JsonPatchDocument instead of the concrete request model type.
The fluent validator, on the other hand, provides us with decent extension points such as the Custom rule.
Combining these two ideas you can create something like this:
public class Validator : AbstractValidator<JsonPatchDocument<CarRequestModel>>
{
public Validator()
{
RuleForEach(x => x.Operations)
.Custom(HandleInternalPropertyValidation);
}
private void HandleInternalPropertyValidation(JsonPatchOperation property, CustomContext context)
{
void AddFailureForPropertyIf<T>(
Expression<Func<T, object>> propertySelector,
JsonPatchOperationType operation,
Func<JsonPatchOperation, bool> predicate, string errorMessage)
{
var propertyName = (propertySelector.Body as MemberExpression)?.Member.Name;
if (propertyName is null)
throw new ArgumentException("Property selector must be of type MemberExpression");
if (!property.Path.ToLowerInvariant().Contains(propertyName.ToLowerInvariant()) ||
property.Operation != operation) return;
if (predicate(property)) context.AddFailure(propertyName, errorMessage);
}
AddFailureForPropertyIf<CarRequestModel>(x => x.Make, JsonPatchOperationType.remove,
x => true, "Car Make cannot be removed.");
AddFailureForPropertyIf<CarRequestModel>(x => x.EngineDisplacement, JsonPatchOperationType.replace,
x => (decimal) x.Value < 12m, "Engine displacement must be less than 12l.");
}
}
In some cases, it might be tedious to write down all the actions that are not allowed from the domain perspective but are defined in the JsonPatch RFC.
This problem could be eased by defining none but rules which would define the set of operations that are valid from the perspective of your domain.
Realization bellow allow use IValidator<Model> inside IValidator<JsonPatchDocument<Model>>, but you need create model with valid properties values.
public class ModelValidator : AbstractValidator<JsonPatchDocument<Model>>
{
public override ValidationResult Validate(ValidationContext<JsonPatchDocument<Model>> context)
{
return _validator.Validate(GetRequestToValidate(context));
}
public override Task<ValidationResult> ValidateAsync(ValidationContext<JsonPatchDocument<Model>> context, CancellationToken cancellation = default)
{
return _validator.ValidateAsync(GetRequestToValidate(context), cancellation);
}
private static Model GetRequestToValidate(ValidationContext<JsonPatchDocument<Model>> context)
{
var validModel = new Model()
{
Name = nameof(Model.Name),
Url = nameof(Model.Url)
};
context.InstanceToValidate.ApplyTo(validModel);
return validModel;
}
private class Validator : AbstractValidator<Model>
{
/// <inheritdoc />
public Validator()
{
RuleFor(r => r.Name).NotEmpty();
RuleFor(r => r.Url).NotEmpty();
}
}
private static readonly Validator _validator = new();
}
You may try the below generic validator - it validates only updated properties:
public class JsonPatchDocumentValidator<T> : AbstractValidator<JsonPatchDocument<T>> where T: class, new()
{
private readonly IValidator<T> _validator;
public JsonPatchDocumentValidator(IValidator<T> validator)
{
_validator = validator;
}
private static string NormalizePropertyName(string propertyName)
{
if (propertyName[0] == '/')
{
propertyName = propertyName.Substring(1);
}
return char.ToUpper(propertyName[0]) + propertyName.Substring(1);
}
// apply path to the model
private static T ApplyPath(JsonPatchDocument<T> patchDocument)
{
var model = new T();
patchDocument.ApplyTo(model);
return model;
}
// returns only updated properties
private static string[] CollectUpdatedProperties(JsonPatchDocument<T> patchDocument)
=> patchDocument.Operations.Select(t => NormalizePropertyName(t.path)).Distinct().ToArray();
public override ValidationResult Validate(ValidationContext<JsonPatchDocument<T>> context)
{
return _validator.Validate(ApplyPath(context.InstanceToValidate),
o => o.IncludeProperties(CollectUpdatedProperties(context.InstanceToValidate)));
}
public override async Task<ValidationResult> ValidateAsync(ValidationContext<JsonPatchDocument<T>> context, CancellationToken cancellation = new CancellationToken())
{
return await _validator.ValidateAsync(ApplyPath(context.InstanceToValidate),
o => o.IncludeProperties(CollectUpdatedProperties(context.InstanceToValidate)), cancellation);
}
}
it has to be registered manually:
builder.Services.AddScoped<IValidator<JsonPatchDocument<TollUpdateAPI>>, JsonPatchDocumentValidator<TollUpdateAPI>>();
Update: I'm now convinced that the problem lies in the fact that Document is configured as non lazy. The problem is that I don't control the base class and that means I can't change the base props to virtual...
After reading the docs, I'm under the assumption that I should be able to have a non lazy class with a lazy property. Is this possible? Here's the code I'm using for mapping my class:
public class DocumentoMapping : ClassMap<Documento> {
public DocumentoMapping()
{
Setup();
}
private void Setup()
{
Table("Documentos");
Not.LazyLoad();
Id(doc => doc.Id, "IdDocumentos")
.GeneratedBy.Identity()
.Default(0);
Map(doc => doc.NomeDocumento)
.Not.Nullable();
Map(doc => doc.Descricao);
Map(doc => doc.Bytes, "Documento")
.CustomSqlType("image")
.CustomType<Byte[]>()
.LazyLoad()
.Length(2000000000);
Component(doc => doc.Acao,
accao =>
{
accao.Map(a => a.Login);
accao.Map(a => a.Data);
accao.Map(a => a.UserAD)
.CustomSqlType("int")
.CustomType<ADs>();
})
.Not.LazyLoad();
Map(doc => doc.IdPedidoAssistencia)
.Column("IdPats")
.Not.LazyLoad();
}
}
And here's the code for my class:
public class Documento : Entity, IHasAssignedId<int>{
public virtual Byte[] Bytes { get; private set; }
public Documento()
{
NomeDocumento = Descricao = "";
Acao = new Acao("none", DateTime.Now, ADs.Sranet);
}
public Documento(string nomeDocumento, string descricao, Acao acao)
{
Contract.Requires(!String.IsNullOrEmpty(nomeDocumento));
Contract.Requires(!String.IsNullOrEmpty(descricao));
Contract.Requires(acao != null);
Contract.Ensures(!String.IsNullOrEmpty(NomeDocumento));
Contract.Ensures(!String.IsNullOrEmpty(Descricao));
Contract.Ensures(Acao != null);
NomeDocumento = nomeDocumento;
Descricao = descricao;
Acao = acao;
}
[DomainSignature]
public String NomeDocumento { get; private set; }
[DomainSignature]
public String Descricao { get; private set; }
[DomainSignature]
public Acao Acao { get; private set; }
internal Int32 IdPedidoAssistencia { get; set; }
internal static Documento CriaNovo(String nomeDocumento, String descricao, Byte[] bytes, Acao acao)
{
Contract.Requires(!String.IsNullOrEmpty(nomeDocumento));
Contract.Requires(!String.IsNullOrEmpty(descricao));
Contract.Requires(bytes != null);
Contract.Requires(acao != null);
var documento = new Documento(nomeDocumento, descricao, acao) { Bytes = bytes };
return documento;
}
public void ModificaBytes(Byte[] bytes)
{
Contract.Requires(bytes != null);
Bytes = bytes;
}
public void SetAssignedIdTo(int assignedId)
{
Id = assignedId;
}
[ContractInvariantMethod]
private void Invariants()
{
Contract.Invariant(NomeDocumento != null);
Contract.Invariant(Descricao != null);
Contract.Invariant(Acao != null);
}
}
Base classes are the just for the basic stuff, ie, setting Id and injecting base code for instance comparison. At first sight, I can't really see anything wrong with this code. I mean, the property is virtual, the mapping says it should be virtual, so why does loading it with Get forces a complete load of the properties? For instance, this code:
var d = sess.Get(148);
Ends up generating sql for loading all the properties on the table. Did I get this wrong?
thanks!
Yes, it's confirmed: in order to have lazy load properties on a class, the class will also need to be lazy.
How do I transform an Enum value into a String Value using QueryOver and AliasToBean? I have the following but get an error when trying to transform the Enum:
SomeDTO someDTO = null;
SomeReferenceAlias someReferenceAlias = null;
var jobs = query
.JoinAlias(x => x.SomeReference, () => someReferenceAlias, JoinType.InnerJoin)
.SelectList(list => list
.Select(p => p.SomeStatusEnum).WithAlias(() => someDTO.SomeStatus)//problem here
.Select(p => someReferenceAlias.Name).WithAlias(() => someDTO.Name)
)
.TransformUsing(Transformers.AliasToBean<SomeDTO>())
.Take(100)
.List<SomeDTO>();
Assuming your enum is stored as int in your DB, I would try a string readonly property to a custom string type :
public enum SomeStatus {up=1,right=2,down=3,left=4}
public class SomeStatusNhString : NHibernate.Type.AbstractStringType
{
public SomeStatusNhString()
: base(new StringSqlType())
{
}
public SomeStatusNhString(StringSqlType sqlType)
: base(sqlType)
{
}
public override string Name
{
get { return "SomeStatusNhString"; }
}
public override object Get(System.Data.IDataReader rs, int index)
{
var x = base.Get(rs, index);
return ((SomeStatus)int.Parse((string)x)).ToString();
}
}
And then your mapping
public virtual String StatusAsString{ get; set; }
<property name="StatusAsString" column="YOUR_COLUMN" not-null="true" insert="false" update="false" type="YourNameSpace.SomeStatusNhString, YourDll" access="property"></property>
Hope this can help
In a simple test scenario which can be setup using the following:
public class TestObj
{
public string Id { get; set; }
public string Name { get; set; }
}
public class Summary
{
public string MyId { get; set; }
public string MyName { get; set; }
}
public class TestObjs_Summary : AbstractIndexCreationTask<TestObj, Summary>
{
public TestObjs_Summary()
{
Map = docs => docs.Select(d => new { MyId = d.Id, MyName = d.Name });
Store(x => x.MyId, FieldStorage.Yes);
Store(x => x.MyName, FieldStorage.Yes);
}
}
static IDocumentStore Setup()
{
var store = new DocumentStore() { Url="http://localhost:8080" };
store.Initialize();
IndexCreation.CreateIndexes(MethodInfo.GetCurrentMethod().DeclaringType.Assembly, store);
using (var session = store.OpenSession())
{
session.Store(new TestObj { Name = "Doc1" });
session.Store(new TestObj { Name = "Doc2" });
session.SaveChanges();
}
return store;
}
I can run a simple synchronous query against the index and get the expected results (2 rows output of type Summary):
using (var session = store.OpenSession())
{
var q = session.Query<Summary>("TestObjs/Summary").AsProjection<Summary>();
Dump("Sync:", q.ToList());
}
However, if I try the same thing using an asynchronous query:
using (var session = store.OpenAsyncSession())
{
var q = session.Query<Summary>("TestObjs/Summary").AsProjection<Summary>();
q.ToListAsync().ContinueWith(t => Dump("Async:", t.Result));
}
I get an InvalidCastException:
InnerException: System.InvalidCastException
Message=Unable to cast object of type 'TestObj' to type 'Summary'.
Source=Raven.Client.Lightweight
StackTrace:
at Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity[T](String id, RavenJObject documentFound, RavenJObject metadata) in c:\Builds\RavenDB-Unstable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:line 416
at Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity[T](String key, RavenJObject document, RavenJObject metadata) in c:\Builds\RavenDB-Unstable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:line 340
at Raven.Client.Document.SessionOperations.QueryOperation.Deserialize[T](RavenJObject result) in c:\Builds\RavenDB-Unstable\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:line 130
...
I suspect this is a bug, but as a RavenDB newbie, I first wanted to rule out the possibility I have screwed something up here. Can anyone see why this code would be failing?
(Note: this was run on Build 721 and on 701 and both produce the same results)
Thanks for any assistance you can provide.
It looks like a bug. I don't see anything wrong in your code. I suggest you create an issue here: http://issues.hibernatingrhinos.com
I get an exception: Unrecognised method call in epression a.B.Count() when I run:
var query = session.QueryOver<A>()
.Where(a => a.B.Count() > 0)
.List();
The following code works:
var query1 = session.QueryOver<A>().List();
var query2 = query1.Where(a => a.B.Count() > 0);
Any ideas? Thanks.
Edit:
Here is my mappings. I'm using NHibernate 3.1.0.4000:
Models:
public class A
{
public virtual int Id { get; private set; }
public virtual ICollection<B> Bs { get; set; }
}
public class B
{
public virtual int Id { get; private set; }
}
Mappings:
public class AMappings : ClassMap<A>
{
public AMappings()
{
Id(x => x.Id);
HasMany(x => x.Bs).LazyLoad();
}
}
public class BMappings : ClassMap<B>
{
public BMappings()
{
Id(x => x.Id);
}
}
Rest of my code:
class Program
{
static void Main(string[] args)
{
// Create connection string
string connectionString = new System.Data.SqlClient.SqlConnectionStringBuilder()
{
DataSource = #".\r2",
InitialCatalog = "TestNHibernateMappings",
IntegratedSecurity = true
}.ConnectionString;
// Create SessionFactory
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration
.MsSql2008.ConnectionString(connectionString)
.ShowSql())
.Mappings(m => m.FluentMappings
.Add(typeof(AMappings))
.Add(typeof(BMappings)))
.ExposeConfiguration(BuildSchema)
.BuildConfiguration()
.BuildSessionFactory();
// Test
var session = sessionFactory.OpenSession();
// This line works OK
var query1 = session.Query<A>()
.Where(a => a.Bs.Count() > 0);
// This line throws exception: Unrecognised method call in epression a.Bs.Count()
var query2 = session.QueryOver<A>()
.Where(a => a.Bs.Count() > 0);
}
static void BuildSchema(Configuration cfg)
{
new SchemaExport(cfg).Create(false, true);
}
}
QueryOver is not LINQ.
Your second code snippet works because it's retrieving ALL THE RECORDS and using LINQ-to-objects in memory.
What you should do is:
session.Query<A>()
.Where(a => a.B.Count() > 0)
.ToList();
or better yet:
session.Query<A>()
.Where(a => a.B.Any())
.ToList();
Query is an extension method, you need to add using NHibernate.Linq;