English 中文(简体)
NHibernate ID geneator when using NSERBus
原标题:NHibernate ID generator when using NServiceBus

CRUD的多数使用NHibernate s guid.comb ID generator。 有了这一优势,我就能够在我通畅访问数据库之前查阅该数据库,并围绕与使用正常指南有关的指数分散问题。

When we introduce messaging it raises a few questions:

  1. Since we are sending commands to make changes to our domain layer, we don t actually have access to the "new" domain object in the UI. Often (in the case of a web application) we need it s identifier to redirect to another page. One solution would be to pass the id as part of the command (e.g. Guid.NewGuid()) but then we lose the sequential Guids that NHibernate provides.
  2. If we instead use an identity strategy we remove the index issue but now have no easy way of determining the id from the UI, other than subscribing to an event or executing he command synchronously, both of which are not ideal in a web application.

So I m curious what strategy other NServiceBus developers are taking. Performing some operation on an existing entity is not really a problem since we can just send a request using ajax to dispatch the command, and notify the user that everything was successful (probably). Since the page they are on already has the updated information this is enough.

However when we create a new instance of a domain object (via a command) we often need to redirect the user to a page that then retrieves the newly created entity from our database. Of course this entity may not have been saved yet (as we are processing our commands async) and we typically need an Id to perform this redirect.

最佳回答

This is the code that nhibernate uses to generate a comb.

private Guid Generate()
    {
        byte[] guidArray = Guid.NewGuid().ToByteArray();

        DateTime baseDate = new DateTime(1900, 1, 1);
        DateTime now = DateTime.Now;

        // Get the days and milliseconds which will be used to build the byte string 
        TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
        TimeSpan msecs = now.TimeOfDay;

        // Convert to a byte array 
        // SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333 
        byte[] daysArray = BitConverter.GetBytes(days.Days);
        byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));

        // Reverse the bytes to match SQL Servers ordering 
        Array.Reverse(daysArray);
        Array.Reverse(msecsArray);

        // Copy the bytes into the guid 
        Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
        Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);

        return new Guid(guidArray);
    }

或许你们可以使用:

问题回答

暂无回答




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签