Skip to content

Commit 0767456

Browse files
committed
StudentsInformationProvider: загружать данные асинхронно при запуске
1 parent 9093638 commit 0767456

File tree

1 file changed

+4
-36
lines changed

1 file changed

+4
-36
lines changed

HwProj.StudentInfo/StudentsInfo/StudentsInformation.cs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,12 @@
66
using IStudentsInfo;
77
using Newtonsoft.Json;
88
using Novell.Directory.Ldap;
9-
using System.Threading;
109

1110
namespace StudentsInfo
1211
{
13-
public class InterruptibleLazy<T>
14-
{
15-
private Func<T> _valueFactory;
16-
private readonly object _lockObj = new object();
17-
private T _value;
18-
19-
public InterruptibleLazy(Func<T> valueFactory)
20-
{
21-
_valueFactory = valueFactory;
22-
}
23-
24-
public T Value
25-
{
26-
get
27-
{
28-
if (_valueFactory == null) return _value;
29-
30-
lock (_lockObj)
31-
{
32-
if (_valueFactory == null) return _value;
33-
34-
_value = _valueFactory();
35-
Interlocked.MemoryBarrier();
36-
_valueFactory = null;
37-
}
38-
39-
return _value;
40-
}
41-
}
42-
}
43-
4412
public class StudentsInformationProvider : IStudentsInformationProvider
4513
{
46-
private readonly InterruptibleLazy<Task<Dictionary<string, List<string>>>> _programsGroups;
14+
private readonly Task<Dictionary<string, List<string>>> _programsGroups;
4715
private readonly string _ldapHost = "ad.pu.ru";
4816
private readonly int _ldapPort = 389;
4917
private readonly string _searchBase = "DC=ad,DC=pu,DC=ru";
@@ -54,7 +22,7 @@ public class StudentsInformationProvider : IStudentsInformationProvider
5422

5523
public async Task<List<GroupModel>> GetGroups(string programName)
5624
{
57-
var programsGroups = await _programsGroups.Value;
25+
var programsGroups = await _programsGroups;
5826
if (!programsGroups.TryGetValue(programName, out var groups))
5927
return new List<GroupModel>();
6028

@@ -152,7 +120,7 @@ private void SafeDisconnect(LdapConnection connection)
152120

153121
public async Task<List<ProgramModel>> GetProgramNames()
154122
{
155-
var programGroups = await _programsGroups.Value;
123+
var programGroups = await _programsGroups;
156124
return programGroups.Keys
157125
.Select(key => new ProgramModel { ProgramName = key })
158126
.ToList();
@@ -168,7 +136,7 @@ public StudentsInformationProvider(string username, string password, string ldap
168136
_searchBase = searchBase;
169137
_httpClient = new HttpClient();
170138

171-
_programsGroups = new InterruptibleLazy<Task<Dictionary<string, List<string>>>>(async () =>
139+
_programsGroups = Task.Run(async () =>
172140
{
173141
var programsGroups = new Dictionary<string, List<string>>();
174142

0 commit comments

Comments
 (0)