English 中文(简体)
NHibernate在另一个方案向共享的KQ数据库提交新数据时能否发现?
原标题:Can NHibernate detect when another program has written new data to a shared SQLite database?

我有两个C#方案,共享一个单一的SQ数据库。

其中一个方案(请其撰写人)向数据库撰写新数据。 另一项方案(Reader)读作。 两个方案都使用Fluent NHibernate和系统。 资料来源:统计局。

目前,我刚刚在我的阅读者方案上看到一个大的Refreshutton,用户必须点击它,以便获得自上一次复读以来向亚洲开发银行提交的任何新数据。 这种做法是可行的,但很难说出。

是否有任何办法让NHibernate在银行更新时向读者方案发起一场活动,因此我可以自动向用户提交新的数据? (我看见《国家人类发展报告》关于拦截和事件的书,但不清楚这些书是否会做我想要做的事情)

或者,是否对使用NH的更新进行投票?

如果不采用国民保健办法,我就能够采取类似制度的做法。 数据,或通过另一个低级机制?

最佳回答

您可将<代码>System.IO.FileWatcher列入共同档案。 这一点将在更新后告诉你。

问题回答

I think an Interceptor or Event on the writer side would be the right solution for this. In our current project we have done something similar and it works great.
Using this approach you would keep the ability to switch the database below or-mapping and could elaborate your update-event from a simple TableUpdate -trigger which i think should be done within a few hours to a concrete Update-Event containing all changed data.

EDIT:

First of all i would not only track the OnSave but also AfterTransactionCompleted to ensure the Transaction is comitted. This also enables you to collect all objects of the tx. The easiest way to do this is overriding the EmptyInterceptor object like this:

public class EntityInterceptor : EmptyInterceptor
{
    private IList<object> entities = new List<object>();

    public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
    {
        entities.Add(entity);
        return base.OnSave(entity, id, state, propertyNames, types);
    }

    public override void AfterTransactionCompletion(ITransaction tx)
    {
        if (tx.WasCommitted)
        { 
            Console.WriteLine("Data has been inserted. Notify the reader here.");
        }
        entities = new List<object>();
    } 
}

海事组织处理中通信的最平级做法是:网络改造。 有几个例子说明如何做到这一点。 http://www.mctainsh.com/Articles/Csharp/RemoteCallback.aspx#A_simple_example”rel=“nofollow noreferer”>here。 或者,如果你想尝试WCF,见here。 或搜索SO,可在此找到许多好的例子。

根据John Rayner的建议,此处采用FileSystemWatcher的一些样本代码。

     // Watches for writes to database file
    private static FileSystemWatcher _dbFileWatcher;

    /// <summary>
    /// Set up an event handler that is called when SQLite database is written to.
    /// </summary>
    /// <param name="onChangedHandler"></param>
    public static void SetupSqliteDatabaseWatcher(FileSystemEventHandler onChangedHandler)
    {
        // Create a new FileSystemWatcher and set its properties.
        _dbFileWatcher = new FileSystemWatcher
                             {
                                 Path = "<directory containing DB file>",
                                 Filter = "<DB file>.db",
                                 NotifyFilter = NotifyFilters.LastWrite
                             };

        // Add the event handler
        _dbFileWatcher.Changed += onChangedHandler;

        // Begin watching.
        _dbFileWatcher.EnableRaisingEvents = true;
    }

    // Event handler
    private static void OnDbWritten(object source, FileSystemEventArgs e)
    {
        Debug.Print("DB written");
    }


    // Set up the event handler
    SetupSqliteDatabaseWatcher(OnDbWritten);




相关问题
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 ) ....

热门标签