使用钟形图标通知用户

huangapple go评论57阅读模式
英文:

Notify users using bell shaped icon

问题

我正在构建一个应用程序,在这个应用程序中,当数据库插入新记录时,我需要通知用户。我的研究表明,我可以使用SignalR和SQL依赖项来实现这一点。然而,我有一些需要解决的问题。

  1. 表很多,每个表都需要进行通知,因此使用SQL依赖项可能意味着我需要为每个表创建依赖项。
  2. SQL依赖项会有性能问题吗?
  3. 还需要为每个客户端保持未读通知的计数。

我已经多次搜索了,但似乎无法理解这个概念应该是什么样的。我希望有一个简单的逻辑和易理解的代码。任何帮助都将不胜感激。我正在使用ASP.Net Core MVC。

英文:

I am building an application where I have to notify users when new record is inserted in the database. My research has indicated that I can use signalr and sql dependency for that. However, I have some few concerns I need to address.

  1. The tables are many and the notification need to be done for each table, so using sql dependency may mean I may have to create dependency for each table.
  2. Will sqldependency have performance issues?
  3. There is also the need to keep count of unread notifications for each client.

I have googled so many times but i seem not to grasp how the concept should be like. I will like to have a simple logic and understandable codes. any help will be appreciated. I am using ASP.Net core MVC

答案1

得分: 0

我似乎不太理解这个概念应该是什么样的。我想要一个简单的逻辑和易懂的代码。任何帮助都将不胜感激。我正在使用ASP.Net Core MVC。

根据您的情况和描述,似乎您希望设计一个系统,在该系统中,您希望随时通知用户数据库发生的任何更改。

实际上,可以使用Asp.net Core SignalR或Sql Dependency来实现这一点。我希望您已经对这两种技术的工作原理有了基本的了解。

除此之外,我宁愿尝试回答您的问题:

表很多,每个表都需要进行通知,所以使用Sql Dependency可能意味着我必须为每个表创建依赖项。

正如您可能知道的,SqlDependency只是在特定表上创建触发器,一旦跟踪到任何更改,触发器就会触发。因此,当您配置依赖关系时,它将创建触发器。

由于您有很多表,您必须为每个表创建依赖关系,目前SqlDependency没有一次管理它们的机制。您可以在官方文档中获取更多详细信息。

SqlDependency会有性能问题吗?

这取决于情况,通常情况下,对于SQL Dependency,没有已知的性能问题。但是,正如您可能知道的,如果您的表包含许多触发器,这可能会影响性能。因为当触发器执行时,如果同时进行任何读写操作,那么它可能会严重影响应用程序性能。

还需要为每个客户端保持未读通知的计数。

我认为,跟踪这个不会太困难。您可以通过为特定客户端设置一个标志来检查它。例如,您可以设置一个属性,在单击事件上更改状态,因为当消息向用户广播时,客户端会发生更改事件。

此外,由于您正在使用Asp.net Core项目,您可以考虑asp.net core signalR。

这样,您可以查询所有实体,然后使用Clients.All.SendAsync方法广播更新。您可以创建一个API端点,该端点将通过SignalR Hub类通知所有用户。

演示:

[Route("cookie/BroadCastToUsers")]
[HttpGet]
public IHttpActionResult BroadCastToUsers()
{
    //从sells表获取总销售额
    var totalSell = 100;
    //从产品表获取总产品数:
    var totalProduct = 150;
    //从产品表获取畅销产品
    var topSellingProduct = "Microsoft Surface";

    var message = $"Hello from signalR Total sells:{totalSell}, Total product:{totalProduct}, Top selling product:{topSellingProduct}";

    var context = GlobalHost.ConnectionManager.GetHubContext<HubClassUpdateUsers>();
    context.Clients.All.BroadcastMessage("sysytem", message);

    return Ok();
}

Hub类:

public class HubClassUpdateUsers : Hub
{
    public override Task OnConnected()
    {
        return base.OnConnected();
    }

    public void BroadcastMessage(string name, string message)
    {
        Clients.All.broadcastMessage(name, message);
    }
}

输出:

使用钟形图标通知用户

注意: 有关详细的实施和指南,请参阅以下文档:

  1. Asp.net core SignalR
  2. SQL Dependency
  3. SQL Dependency Implementation
英文:

> I seem not to grasp how the concept should be like. I will like to
> have a simple logic and understandable codes. any help will be
> appreciated. I am using ASP.Net core MVC

Well, based on your scenario and description it seems that you are looking for designing a system where you want to keep informed your user about any changes take place within your database.

Actually, this can be done either using Asp.net core SignalR or Sql Dependency.
I hope you already, have the basic idea how these two technology works.

Apart from this, I would rather trying to answer your question:

> The tables are many and the notification need to be done for each
> table, so using sql dependency may mean I may have to create
> dependency for each table.

As you may know, SqlDependency nothing but creates a trigger on particular table and the trigger will be fired once any chnages would be tracked. So when you will configure the dependency it will create the trigger.

As you have many tables, you have to create the dependency for every table as well and at this moments SqlDependency doesn't have the mechanism to manage them at once. You can get more details here in official document.

> Will sqldependency have performance issues?

It depends, usually, for SQL Dependency there is no known performence issue. But, as you may know if your table contains many triggers it might effect upon performence issue. Because, while your triggers executing and if at the same time any read, write operation also being called then it would critically impact on the application performence.

> There is also the need to keep count of unread notifications for each
> client.

I think, this wouldn't be a big-deal to track. You can check it by keeping a flag for the particular client. For instance, you can set a property and on click event you can change the status, because, when the message would broadcust towards the user, there would be chnage event on client side.

In addition to this, as you are using Asp.net core project, you could consider asp.net core signalR.

So that, you can query all of your entity and then you can broadcust the update using Clients.All.SendAsync method. You may create and API endpoint which will do the task to notify all of your users using the SignalR Hub Class

Demo:

        [Route(&quot;cookie/BroadCastToUsers&quot;)]
        [HttpGet]
        public IHttpActionResult BroadCastToUsers()
        {
            //Get total sell from sells table
            var totalSell = 100;
            //Get Total product from product table:
            var totalProduct = 150;
            //Get top selling product from product table 
            var topSellingProduct = &quot;Microsoft Surface&quot;;



            var message = $&quot;Hello from signalR Total sells:{totalSell}, Total product:{totalProduct}, Top selling product:{topSellingProduct}&quot;;

            var context = GlobalHost.ConnectionManager.GetHubContext&lt;HubClassUpdateUsers&gt;();
            context.Clients.All.BroadcastMessage(&quot;sysytem&quot;,message);

            return Ok();
        }

Hub Class:

public class HubClassUpdateUsers : Hub
    {
        public override Task OnConnected()
        {
            return base.OnConnected();
        }

        public void BroadcastMessage(string name, string message)
        {
            Clients.All.broadcastMessage(name, message);
        }
        
    }

Output:

使用钟形图标通知用户

Note: Please refer to following document for details implementation and guidelines:

  1. Asp.net core SignalR
  2. SQL Dependency
  3. SQL Dependency Implementation

huangapple
  • 本文由 发表于 2023年7月27日 21:33:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定