So I have a .txt file with the following entities
1,1,10,PARTICIPANT
2,1,15,PARTICIPANT
3,1,8,PARTICIPANT
4,1,20,PARTICIPANT
5,1,30,PARTICIPANT
6,1,7,REZERVA
7,1,5,PARTICIPANT
8,1,14,PARTICIPANT
9,1,4,PARTICIPANT
10,1,11,PARTICIPANT
11,1,10,REZERVA
12,1,6,PARTICIPANT
13,2,15,REZERVA
14,2,25,PARTICIPANT
15,2,10,PARTICIPANT
16,2,6,PARTICIPANT
17,2,40,PARTICIPANT
18,2,18,PARTICIPANT
19,2,7,PARTICIPANT
20,2,17,REZERVA
21,2,24,PARTICIPANT
22,2,15,PARTICIPANT
23,2,10,PARTICIPANT
24,2,11,PARTICIPANT
1,3,11,PARTICIPANT
2,3,12,PARTICIPANT
3,3,6,REZERVA
4,3,13,PARTICIPANT
5,3,14,PARTICIPANT
6,3,17,PARTICIPANT
19,3,11,PARTICIPANT
20,3,8,REZERVA
21,3,11,PARTICIPANT
22,3,12,PARTICIPANT
23,3,14,PARTICIPANT
24,3,15,PARTICIPANT
7,4,0,REZERVA
8,4,20,PARTICIPANT
9,4,21,PARTICIPANT
10,4,13,PARTICIPANT
11,4,9,PARTICIPANT
12,4,14,PARTICIPANT
13,4,30,PARTICIPANT
14,4,25,PARTICIPANT
15,4,15,PARTICIPANT
16,4,44,PARTICIPANT
17,4,12,REZERVA
18,4,8,PARTICIPANT
1,5,14,PARTICIPANT
2,5,8,PARTICIPANT
3,5,8,PARTICIPANT
4,5,8,REZERVA
5,5,18,PARTICIPANT
6,5,28,PARTICIPANT
13,5,40,PARTICIPANT
14,5,20,REZERVA
15,5,15,PARTICIPANT
16,5,8,PARTICIPANT
17,5,8,PARTICIPANT
18,5,12,PARTICIPANT
19,6,10,PARTICIPANT
20,6,20,PARTICIPANT
21,6,20,PARTICIPANT
22,6,15,PARTICIPANT
23,6,15,PARTICIPANT
24,6,16,REZERVA
7,6,28,PARTICIPANT
8,6,10,PARTICIPANT
9,6,28,PARTICIPANT
10,6,10,REZERVA
11,6,15,PARTICIPANT
12,6,5,PARTICIPANT
In my Model folder i have a Validator folder and 3 other clases:
Entity.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ex.Model
{
public class Entity<TID>
{
public TID ID { get; set; }
}
}
JucatorActiv.cs where i describe the JucatorActiv entity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ex.Model
{
enum Status { REZERVA, PARTICIPANT }
class JucatorActiv : Entity<String>
{
public String IdMeci { get; set; }
public Double NrPuncteInscrise { get; set; }
public Status Tip { get; set; }
public override string ToString()
{
return ID + " " + IdMeci + " " + NrPuncteInscrise + " " + Tip;
}
}
}
And JucatorActivToFile.cs where i transform the string into an JucatorActiv entity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ex.Model
{
class JucatorActivToFile
{
public static JucatorActiv CreateJucatorA(string line)
{
string[] fields = line.Split(',');
// new char[] { ',' }
JucatorActiv jucA = new JucatorActiv()
{
ID = fields[0],
IdMeci = fields[1],
NrPuncteInscrise = Double.Parse(fields[2]),
Tip = (Status)Enum.Parse(typeof(Status), fields[3])
};
return jucA;
}
}
}
After in my Repo folder i have:
A DataReader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Ex.Repo
{
class DataReader
{
public static List<T> ReadData<T>(string fileName, CreateEntity<T> createEntity)
{
List<T> list = new List<T>();
using (StreamReader sr = new StreamReader(fileName))
{
string s;
while ((s = sr.ReadLine()) != null)
{
T entity = createEntity(s);
list.Add(entity);
}
}
return list;
}
}
}
A repo interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex.Model;
namespace Ex.Repo
{
interface IRepo<ID, E> where E : Entity<ID>
{
IEnumerable<E> FindAll();
E Save(E entity);
}
}
A InMemoryRepo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex.Model;
using Ex.Model.Validator;
namespace Ex.Repo
{
class InMemoryRepo<ID, E> : IRepo<ID, E> where E : Entity<ID>
{
protected IValidator<E> validator;
protected IDictionary<ID, E> entities = new Dictionary<ID, E>();
public InMemoryRepo(IValidator<E> validator)
{
this.validator = validator;
}
public IEnumerable<E> FindAll()
{
return entities.Values.ToList<E>();
}
public E Save(E entity)
{
if (entity == null)
throw new ArgumentNullException("Entity cannot be null!");
if (this.entities.ContainsKey(entity.ID))
return entity;
else
this.entities[entity.ID] = entity;
return default;
}
}
}
A InFileRepo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex.Model;
using Ex.Model.Validator;
namespace Ex.Repo
{
public delegate E CreateEntity<E>(String record);
abstract class InFileRepo<ID, E> : InMemoryRepo<ID, E> where E : Entity<ID>
{
protected String filename;
protected CreateEntity<E> CreateEntity;
public InFileRepo(IValidator<E> validator, String filename, CreateEntity<E> createEntity) : base(validator)
{
this.filename = filename;
this.CreateEntity = createEntity;
if (createEntity != null)
loadFromFile();
}
protected virtual void loadFromFile()
{
List<E> list = DataReader.ReadData(filename, CreateEntity);
list.ForEach(x => entities[x.ID] = x);
}
}
}
And a JucatorActivInFileRepo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex.Model;
using Ex.Model.Validator;
namespace Ex.Repo
{
class JucatorActivInFileRepo : InFileRepo<string, JucatorActiv>
{
public JucatorActivInFileRepo(IValidator<JucatorActiv> vali, string fileName) : base(vali, fileName, JucatorActivToFile.CreateJucatorA)
{
}
}
}
In my Service class i have the followings:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex.Model;
using Ex.Repo;
namespace Ex.Service
{
class ServiceJucatori
{
private IRepo<String, JucatorActiv> repoJucatoriActivi;
public ServiceJucatori(IRepo<String, JucatorActiv> repoJucatoriActivi)
{
this.repoJucatoriActivi = repoJucatoriActivi;
}
public List<JucatorActiv> FindAllJA()
{
return repoJucatoriActivi.FindAll().ToList();
}
}
}
In App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="jucatorActivFileName" value ="..\\..\\..\\Data\\jucatoriActivi.txt"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
And finally in my Program i have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ex.Model;
using Ex.Model.Validator;
using Ex.Repo;
using Ex.Service;
using System.Configuration;
namespace Ex
{
class Program
{
static void Main(string[] args)
{
String jucatorActivFileName = ConfigurationManager.AppSettings["jucatorActivFileName"];
IValidator<JucatorActiv> valJA = new JucatoriActiviValidator();
IRepo<String, JucatorActiv> repoJA = new JucatorActivInFileRepo(valJA, jucatorActivFileName);
ServiceJucatori serv = new ServiceJucatori(repoJA);
foreach (JucatorActiv jA in serv.FindAllJA())
Console.WriteLine(jA);
}
}
}
After I run Program.cs the output is the following:
1 5 14 PARTICIPANT
2 5 8 PARTICIPANT
3 5 8 PARTICIPANT
4 5 8 REZERVA
5 5 18 PARTICIPANT
6 5 28 PARTICIPANT
7 6 28 PARTICIPANT
8 6 10 PARTICIPANT
9 6 28 PARTICIPANT
10 6 10 REZERVA
11 6 15 PARTICIPANT
12 6 5 PARTICIPANT
13 5 40 PARTICIPANT
14 5 20 REZERVA
15 5 15 PARTICIPANT
16 5 8 PARTICIPANT
17 5 8 PARTICIPANT
18 5 12 PARTICIPANT
19 6 10 PARTICIPANT
20 6 20 PARTICIPANT
21 6 20 PARTICIPANT
22 6 15 PARTICIPANT
23 6 15 PARTICIPANT
24 6 16 REZERVA