Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问对象列表&;如何为fatest请求声明对象_C#_Sql Server_Asp.net Mvc 5_Locking_Signalr - Fatal编程技术网

C# 访问对象列表&;如何为fatest请求声明对象

C# 访问对象列表&;如何为fatest请求声明对象,c#,sql-server,asp.net-mvc-5,locking,signalr,C#,Sql Server,Asp.net Mvc 5,Locking,Signalr,我正在开发一个Asp.net mvc项目: 有一个列表存储所有在线学生 有几种方法: -登录(学生p):如果登录确定,p将添加到学生列表中 -注销(学生p):如果注销正常,p将从学生列表中删除 ->两种方法有一个相同的潜在问题是“修改时不能修改列表”,因为有很多学生同时登录到系统和注销。添加一个瞳孔时,另一个瞳孔将从瞳孔列表中删除->异常抛出 我试图在修改(插入/删除)时使用lock锁定列表,但这是一种好方法吗?你有更好的主意吗 最后一种方法是索赔(b册) 管理员将一些书籍放入GUI中,所有登录

我正在开发一个Asp.net mvc项目:

有一个列表存储所有在线学生

有几种方法: -登录(学生p):如果登录确定,p将添加到学生列表中 -注销(学生p):如果注销正常,p将从学生列表中删除

->两种方法有一个相同的潜在问题是“修改时不能修改列表”,因为有很多学生同时登录到系统和注销。添加一个瞳孔时,另一个瞳孔将从瞳孔列表中删除->异常抛出

我试图在修改(插入/删除)时使用lock锁定列表,但这是一种好方法吗?你有更好的主意吗

最后一种方法是索赔(b册)

管理员将一些书籍放入GUI中,所有登录的学生都可以看到这些书籍。他们可以索取任何他们想要的书。声称最快的学生将拥有那本书。那么我们怎么知道最快的索赔人呢?在更新数据行时。同时,有许多书被许多学生认领。但只有一个速度最快的学生在成功申请后才能拥有一本书

你有解决这个问题的办法吗?这个解决方案就像你下达购买股票的命令一样。最快的人将拥有股票

记住,很多学生会在同一时间做同样的事情。因此,我们必须确保该系统正常、准确地工作

提前谢谢你,
最好的问候

我在这里看到的问题是,您的设计假设应用程序始终处于打开状态,并且列表是关于谁拥有哪本书的绝对事实。重置服务器时会发生什么情况?如果您的库变得足够大,需要为应用程序安装第二台服务器,该怎么办

您需要将列表保存在数据库或其他某种持久介质中。在内存中保存列表将为您提供一个读/写缓冲区,但必须从库数据库的持久性层填充列表

using System;
using System.Collections.Concurrent;


namespace TestArea
{
    public class Pupil
    {
        public Guid Id { get; set; }
        public string Name { get; set; }

        public string UserName { get; set; }        
    }

    public class Book
    {
        //Supports having more than one ISBN in the library... We may have more than one To Kill a Mockingbird in our school library
        public Guid Id { get; set; }
        public string ISBN { get; set; }
    }
    public class SchoolLibrary
    {
        private ConcurrentDictionary<Guid, Pupil> Pupils { get; set; }
        private ConcurrentDictionary<Guid, Book> Books{ get; set; }
        private ConcurrentDictionary<Guid, Guid> CheckOuts { get; set; }

        public Pupil Login(string userName, string password)
        {
            //Call repository to authenticate pupil into library system

            //Mocked return assuming password check success
            var id = Guid.NewGuid();

            return Pupils.GetOrAdd(id, (i) =>
            {
                //Replace with function to get student info
                return new Pupil
                {
                    Id = i,
                    Name = "Bac Clunky",
                    UserName = userName
                };
            });
        }

        public bool CheckOut(Guid pupilId, Guid bookId)
        {
            //If book exists
            if (Books.ContainsKey(bookId))
            {
                Guid currentOwner;
                //...is not currently checked out by anyone
                if (CheckOuts.TryAdd(bookId, pupilId))
                {
                    return true; //book is now checked out
                }

                if (CheckOuts.TryGetValue(bookId, out currentOwner))
                {
                    return currentOwner == pupilId; //returns true if pupil already has the book, false if another student has it
                }                    
            }

            return false; //all other cases fail to check out book
        }
    }
}
使用系统;
使用System.Collections.Concurrent;
命名空间测试区
{
公办班学生
{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共字符串用户名{get;set;}
}
公共课堂用书
{
//支持在图书馆有多个ISBN…我们学校图书馆可能有多个ISBN来杀死一只知更鸟
公共Guid Id{get;set;}
公共字符串ISBN{get;set;}
}
公立学校图书馆
{
私有并发字典{get;set;}
私有ConcurrentDictionary图书{get;set;}
私有ConcurrentDictionary签出{get;set;}
公共学生登录(字符串用户名、字符串密码)
{
//调用存储库对进入库系统的学生进行身份验证
//假设密码检查成功,模拟返回
var id=Guid.NewGuid();
返回学生。GetOrAdd(id,(i)=>
{
//替换为获取学生信息的函数
返校
{
Id=i,
Name=“Bac Clunky”,
用户名=用户名
};
});
}
公共bool签出(Guid pupilId、Guid bookId)
{
//如果书存在
if(图书容器(图书ID))
{
Guid当前所有者;
//…当前未被任何人签出
if(checkout.TryAdd(bookId,pupilId))
{
return true;//书本现在已签出
}
if(checkout.TryGetValue(bookId,out currentOwner))
{
return currentOwner==pupilId;//如果学生已经拥有该书,则返回true;如果其他学生拥有该书,则返回false
}                    
}
return false;//其他所有案例都无法签出图书
}
}
}

您只是在使用列表吗?如果是这样,那就永远不会被认为是线程安全的。你试过使用并行包吗@我会试试的