NEventStore RavenDB - Obtain Commit Data - ravendb

I'm working through a basic example of working with NEventStore with RavenDB and I've hit a stumbling block.
I've managed to succesfully commit my events to the RavenDB datasource, but now I need to pull them back out, to replay them (CQRS - ES) pattern.
I can bring back the Collection of EventMesssage objects by usng the NEventStore IStoreEvents Unterface like this :-
public IEnumerable<EventMessage> GetEvents(Guid aggregateRootId)
{
using (var stream = _store.OpenStream(aggregateRootId, 0))
{
return stream.CommittedEvents;
}
}
In each EventMessage, I can access the Header & the Body Properties. In the body property is the actual event that i have submitted, which i want to pull out and inspect.
I'm not sure if what I'm trying to achieve is correct.
Should I be using the Raven IDocumentStore class to retrieve the event objects or should this be done via NEventstore Stream.

At basic level what you're doing is correct: it's the same code shown in the quick-start NEventstore sample code.
If you work in a more DDD context, probably you should emit events from your Aggregates using the CommonDomain library that is embedded in NEventStore, and retrieve them from IRepository Interface.
You could find some example in the web.
One of them is a my training project: https://github.com/williamverdolini/CQRS-ES-Todos (some notes about that). I'm not using RavenDB and I'm still learnig...but could be useful ;-)

Related

Scoped properties for Application Insights

I would like to know if there is an elegant way to add scoped properties to Application Insights, something similar to Serilog:
var yearEnricher = new PropertyEnricher("Year", year);
using (LogContext.PushProperties(yearEnricher))
{
// ...
}
In the previous example every log created within the using block will have the property Year stamped on it.
I figured out how to do this when I want the property to be present within the whole request pipeline:
var requestTelemetry = context.Features.Get<RequestTelemetry>();
requestTelemetry?.Properties.Add(propertyName, propertyValue.ToString());
Sometimes I want to create a logging scope in code that is not related to the web context so it doesn't make sense to rely on the IHttpContextAccessor. I acknowledge I could leverage OperationTelemetry and TelemetryClient.StartOperation to achieve my goal but it is cumbersome as I've to implement a few properties in which I've no interest (such as Name, Success, Duration...).
Is there a better way than relying on OperationTelemetry?
If you don't want to use OperationTelemetry, you might want to look into implementing your own ITelemetryInitializer (see documentation here).
It should be fairly easy to implement a stack-like global structure to hold the properties you want to push, and pop the stack on your Dispose method.
Note that you'll probably need to utilize CallContext in order for your stacks to be thread safe.

Update Running workflow with dynamicUpdateMap

I have a workflow running and i'm trying to update it dynamically. It is a Flowchart and i'm trying to change the Next property of a FlowStep.
The problem is that when loading WorkflowApplication.Load(workflowApplicationInstance, map); the instance with the map, i got the error:
In order for an implementation map to be directly applied to a workflow instance, the root of the definition must not have any public/imported children or public/imported delegates.
i tried saving the map to file and to database, because i saw in other examples, the map is saved with extension file.map not file.xaml of file.xml. Anyway it was useless, it's still not loading.
Solved that. The problem was when calling PrepareForUpdate and CreateUpdateMap methods, from their API, i was calling them with ActivityBuilder parameter and it should have been Activity. So having the ActivityBuilder of a workflow you can obtain the activity of it like this:
ActivityBuilder workflowDefinition;
Activity flowcharWorkflow = workflowDefinition.Implementation as Flowchart();
if your workflow definition has a root of flowchart.

Posting a Task to the Web Consoles Execution(Management) Context

In the apache brooklyn web interface we would like to display some content for the sytsem managers. The content is too long to be served as a simple sensor value.
Our idea was to create a task and write the content into the output stream of the task, and then offer the REST based URL to the managers like this:
/v1/activities/{task}/stream/stdout (Of course the link masked with some nice text)
The stream and task is created like this:
LOG.info("{} Creating Activity for ClusterReport Feed", this);
activity = Tasks.builder().
displayName("clusterReportFeed").
description("Output for the Cluster Report Feed").
body(new Runnable() {
#Override
public void run() {
//DO NOTHING
}
}).
parallel(true).
build();
LOG.info("{} Task Created with Id: " + activity.getId(), this);
Entities.submit(server, activity).getUnchecked();
The task seems to be created and the interraction works perfectly fine.
However when I want to access the tasks output stream from my browser using a prepared URL I get the error that the task does not exist.
Our idea is that we are not in the right Management/Execution Context. The Web page is running in an other context compared to the entities and their sensors. How can we put a task so that it's visible for the web consoles context also.
Is it possible to write the content into a file and then offer it for download via Jetty(brooklyns web server)? That would be a much simpler way.
Many tasks in Brooklyn default to being transient - i.e. they are deleted shortly after they complete (things like effector invocations are by default non-transient).
You can mark your task as non-transient using the code below in your use of the task builder:
.tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG)
However, note that (as of Brooklyn version 0.9.0) tasks are kept in-memory using soft references. This means the stdout of the task will likely be lost at some point in the future, when that memory is needed for other in-memory objects.
For your use-case, would it make sense to have this as an effector result perhaps?
Or could you write to an object store such as S3 instead? The S3-approach would seem best to me.
For writing it to a file, care must be taken when used with Brooklyn high-availability. Would you write to a shared volume?
If you do write to a file, then you'd need to provide a web-extension so that people can access the contents of that file. As of Brooklyn 0.9.0, you can add your own WARs in code when calling BrooklynLauncher (which calls BrooklynWebServer).

How to ensure that NHibernate is inside a transaction when saving, updating or deleting?

I'd like to ensure, that when I'm persisting any data to the database, using Fluent NHibernate, the operations are executed inside a transaction. Is there any way of checking that a transaction is active via an interceptor? Or any other eventing mechanism?
More specifically, I'm using the System.Transaction.TransactionScope for transaction management, and just want to stop myself from not using it.
If you had one place in your code that built your session, you could start the transaction there and fix the problem at a stroke.
I haven't tried this, but I think you could create a listener implementing IFlushEventListener. Something like:
public void OnFlush(FlushEvent #event)
{
if (!#event.Session.Transaction.IsActive)
{
throw new Exception("Flushing session without an active transaction!");
}
}
It's not clear to me (and Google didn't help) exactly when OnFlush is called. There also may be an implicit transaction that could set IsActive to true.
If you had been using Spring.Net for your transaction handling, you could use an anonymous inner object to ensure that your DAOs/ServiceLayer Objects are always exposed with a TransactionAdvice around their service methods.
See the spring documentation for an example.
To what end? NHProf will give you warnings if you're not executing inside a transaction. Generally you should be developing with this tool open anyway...

NHibernate Optimistic Concurrency

I'm investigating optimistic concurrency in NHibernate. I have a scenario that is very similar to what is being described here:
http://weblogs.asp.net/stefansedich/archive/2008/10/01/set-the-value-of-a-version-column-in-nhibernate-manually.aspx
Would you recommend going with the proposed solution in this blog post?
Thanks
The blog suggests using an interceptor to re-load the current version number from the database in order to perform a manual version check with the version passed in through the entity from a DTO object. This would certainly work, but as described in the article, it adds an extra DB hit to load the current version number.
The better solution, which seems pretty obvious since it's actually what's described in the documentation for "Application version checking" as described and quoted in that blog entry. That is, perform the version check on the initially loaded entity using the DTO's version. More specifically, using the code from the article (changes to the article's code are bold):
public void Update(MyDTO dto) {
// Select the item.
var item = this.repository.SelectById(dto.Id);
// Verify there hasn't been a concurrent change
if(item.Version != dto.Version)
{
throw new StaleObjectStateException();
}
// Map values from DTO to model.
item.Name = dto.Name;
item.Version = dto.Version;
// Call update
this.repository.Update(item);
}