N层(3层架构)窗口中的SqlDependency构成应用程序。 [英] SqlDependency in a N-Tier (3 layer architecture) windows form application.

查看:95
本文介绍了N层(3层架构)窗口中的SqlDependency构成应用程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我解决有关SqlDependency的问题,我已经在google上找到了很多关于我具体问题的信息。



我目前编写的程序是Windows窗体中的ADO.Net应用程序,它实现了3层架构,并使用SQL Server 2008作为后端数据库。因此,我有一个检索数据的数据库层,一个保存数据的Business层,以及一个将数据放到表单上的表示层。下面的代码位于数据库层中,并处理基于用户名从User.Messages表中检索消息列表。

I am hoping some one can help with my problem regarding SqlDependency, to which I have found plenty of information on google but nothing regarding my specific problem.

The program I am currently writing is an ADO.Net application in windows forms and implements 3 layer architecture, plus uses SQL Server 2008 as the back end data base. Therefore I have a database layer which retrieves the data, a Business layer which holds the data, and a presentation layer which puts the data onto the form. The below code below sits in the data base layer, and deals with retrieving a list of messages from the User.Messages table based on the "username".

public class MessagesDB
{
    public static List<Messages> GetMessageList(string UserName)
    {
        List<Messages> messageList = new List<Messages>();
        SqlConnection connection = ESM_Demand_ProformaDB.GetConection();
        string selectStatement =
            "SELECT MessageID, UserName, MessageTitle, Date, Frome," +
            "MessageText, ESMNo " +
            "FROM Users.Message " +
            "ORDER BY Date ";
        SqlCommand selectCommand = new SqlCommand(selectStatement, connection);
        selectCommand.Parameters.AddWithValue("@UserName", UserName);
        try
        {
            connection.Open();
            SqlDataReader reader = selectCommand.ExecuteReader();
            while (reader.Read())
            {
                Messages mesages = new Messages();
                mesages.MesageID = (Int64)reader["MessageID"];
                mesages.UserName = reader["UserName"].ToString();
                mesages.MessageTitle = reader["MessageTitle"].ToString();
                mesages.Date = (DateTime)reader["Frome"];
                mesages.From = reader["Frome"].ToString();
                mesages.MessageText = reader["MessageText"].ToString();
                mesages.ESMNo = (Int64)reader["ESMNo"];
                messageList.Add(mesages);
            }
            reader.Close();
        }
        catch (SqlException ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
        }
        return messageList;
    }
}



上面的代码在与表示层中的事件相结合时工作正常,例如在加载表单或按下按钮。但是我不会实现SqlDependency,因此当我的开始页面加载时,它会获得该用户的消息列表,然后当收到新消息时弹出消息说你有新消息并重新加载消息列表。我在互联网上看到的所有方法都在与从数据库中检索数据的方法相同的类中具有On-Change事件处理程序。因此,很容易将SqlDependency的on change事件处理程序设置为与下面相同的类中的Onchange事件处理程序委托。


The code above works fine when coupled to an event in the presentation layer such as on loading a form or pushing a button. But I won’t to implement SqlDependency , hence when my start page loads it gets a list of messages for that user, then when a new message is received as message pops up saying you have a new message and re-loads the list of messages. All the methods I have seen on the internet have the On-Change event handler in the same class as the method that retrieves the data from the data base. Hence it’s easy to set the on change event handler of the SqlDependency to the Onchange event handler delegate in the same class as below.

private void dependency_OnDataChangeDelagate(object sender, SqlNotificationEventArgs e)
{
   //do somthing
   //and reset sqldependency
}

//Code in database method 
SqlDependency dependency = new SqlDependency(sqlCmd);
dependency.OnChange += new OnChangeEventHandler(dependency_OnDataChangedDelegate);



但你怎么做当您的On-Change Delegate位于您的表示层时,用于检索消息列表的public static位于数据库层中。



如果有人可以指向我正确的方向,这将是伟大的,也希望我解释正确,并希望代码有所帮助。


But how do you do this when you On-Change Delegate is in your presentation layer, and the public static to retrieve the message list is in the database layer.

If anyone can point me in the right direction that would be great, Also hope I explained that right, and hope the code helps.

推荐答案

如果您有一个表示层,请使用它,并且不要试图让它做什么不是它的目的。如果你的PL很厚,你应该实现一个消息传递功能来通知PL,并要求它刷新数据。如果它很薄,你应该在PL侧进行预定回调以检查是否有新消息,必要时结束刷新。
If you have a presentation layer, use it as such, and don''t try to make it do what''s not it''s purpose. If your PL is thick, you should implement a messaging feature to notify the PL, and ask it to refresh the data. If it is thin, you should do scheduled callback on PL side to check if there is any new message, end refresh if necessary.


这篇关于N层(3层架构)窗口中的SqlDependency构成应用程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆