Skip to content

Commit b813f23

Browse files
Refactor
1 parent 7de62bb commit b813f23

49 files changed

Lines changed: 907 additions & 63 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,43 @@ using System;
77
using System.Collections.Generic;
88
using System.Linq;
99
using SQLiteAbstractCrud;
10+
using SQLiteAbstractCrud.Proxy.Attributes;
1011
1112
namespace Sample
1213
{
1314
public class Program
1415
{
1516
static void Main(string[] args)
1617
{
18+
// 1. Create a repository class with RepositoryBase<T> as base
19+
// 2. Pass the path of the file db as constructor
1720
PersonRepository personRepository = new ("./my-db.db");
1821
1922
Console.WriteLine("\r\nSimple insert");
20-
personRepository.Insert(new Person(false, "Bob"));
21-
var person = personRepository.GetAll().First();
23+
personRepository.Insert(new Person(1, false, "Bob")); // insert
24+
var person = personRepository.GetAll().First(); // getAll
2225
Console.WriteLine(person.Name);
2326
2427
Console.WriteLine("\r\nBatch insert");
2528
var persons = new List<Person>
2629
{
27-
new Person(false, "Mary"),
28-
new Person(false, "John")
30+
new Person(2, false, "Mary"),
31+
new Person(3, false, "John")
2932
};
30-
personRepository.InsertBatch(persons);
31-
personRepository.GetAll().ToList().ForEach(person => Console.WriteLine(person.Name));
33+
personRepository.InsertBatch(persons); // insertBatch
34+
var people = personRepository.GetAll().ToList(); // getAll
35+
people.ForEach(person => Console.WriteLine(person.Name));
36+
37+
Console.WriteLine("\r\nDelete");
38+
personRepository.Delete(people[0].Id); // delete
39+
personRepository.GetAll().ToList().ForEach(person => Console.WriteLine(person.Name));// getAll
40+
41+
Console.ReadKey();
3242
}
3343
}
3444
3545
3646
// repository
37-
3847
public class PersonRepository : RepositoryBase<Person>
3948
{
4049
public PersonRepository(string pathDbFile) : base(pathDbFile)
@@ -45,29 +54,22 @@ namespace Sample
4554
4655
4756
// entity
48-
4957
public class Person
5058
{
5159
[PrimaryKey] // required
52-
[AutoIncrement] // optional
5360
public int Id { get; set; }
54-
public string Name { get; set; }
5561
public bool IsDriver { get; set; }
62+
public string Name { get; set; }
5663
5764
public Person(int id, bool isDriver, string name)
5865
{
5966
Id = id;
6067
IsDriver = isDriver;
6168
Name = name;
6269
}
63-
64-
public Person(bool isDriver, string name)
65-
{
66-
IsDriver = isDriver;
67-
Name = name;
68-
}
6970
}
7071
}
7172
7273
74+
7375
```

SQLiteAbstractCrud.sln

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SQLiteAbstractCrud.Tests",
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "samples\AbstractCrud\Sample.csproj", "{D12D818D-F419-4B69-9EDD-EFBE51282761}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SQLiteAbstractCrud.Proxy", "src\SQLiteAbstractCrud.Proxy\SQLiteAbstractCrud.Proxy.csproj", "{7CD84ECD-82CC-4E48-8B41-0DBB77923393}"
13-
EndProject
14-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util", "src\Util\Util.csproj", "{0524CDD3-2878-407E-AA53-CE9AE121A80A}"
15-
EndProject
16-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A98A9CCA-3BD6-4419-A0F6-E94579F6BFFC}"
17-
ProjectSection(SolutionItems) = preProject
18-
README.md = README.md
19-
EndProjectSection
20-
EndProject
2112
Global
2213
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2314
Debug|Any CPU = Debug|Any CPU
@@ -36,14 +27,6 @@ Global
3627
{D12D818D-F419-4B69-9EDD-EFBE51282761}.Debug|Any CPU.Build.0 = Debug|Any CPU
3728
{D12D818D-F419-4B69-9EDD-EFBE51282761}.Release|Any CPU.ActiveCfg = Release|Any CPU
3829
{D12D818D-F419-4B69-9EDD-EFBE51282761}.Release|Any CPU.Build.0 = Release|Any CPU
39-
{7CD84ECD-82CC-4E48-8B41-0DBB77923393}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40-
{7CD84ECD-82CC-4E48-8B41-0DBB77923393}.Debug|Any CPU.Build.0 = Debug|Any CPU
41-
{7CD84ECD-82CC-4E48-8B41-0DBB77923393}.Release|Any CPU.ActiveCfg = Release|Any CPU
42-
{7CD84ECD-82CC-4E48-8B41-0DBB77923393}.Release|Any CPU.Build.0 = Release|Any CPU
43-
{0524CDD3-2878-407E-AA53-CE9AE121A80A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44-
{0524CDD3-2878-407E-AA53-CE9AE121A80A}.Debug|Any CPU.Build.0 = Debug|Any CPU
45-
{0524CDD3-2878-407E-AA53-CE9AE121A80A}.Release|Any CPU.ActiveCfg = Release|Any CPU
46-
{0524CDD3-2878-407E-AA53-CE9AE121A80A}.Release|Any CPU.Build.0 = Release|Any CPU
4730
EndGlobalSection
4831
GlobalSection(SolutionProperties) = preSolution
4932
HideSolutionNode = FALSE

samples/AbstractCrud/Program.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using SQLiteAbstractCrud;
5+
using SQLiteAbstractCrud.Proxy.Attributes;
56

67
namespace Sample
78
{
89
public class Program
910
{
1011
static void Main(string[] args)
1112
{
13+
// 1. Create a repository class with RepositoryBase<T> as base
14+
// 2. Pass the path of the file db as constructor
1215
PersonRepository personRepository = new ("./my-db.db");
1316

1417
Console.WriteLine("\r\nSimple insert");
15-
personRepository.Insert(new Person(false, "Bob"));
16-
var person = personRepository.GetAll().First();
18+
personRepository.Insert(new Person(1, false, "Bob")); // insert
19+
var person = personRepository.GetAll().First(); // getAll
1720
Console.WriteLine(person.Name);
1821

1922
Console.WriteLine("\r\nBatch insert");
2023
var persons = new List<Person>
2124
{
22-
new Person(false, "Mary"),
23-
new Person(false, "John")
25+
new Person(2, false, "Mary"),
26+
new Person(3, false, "John")
2427
};
25-
personRepository.InsertBatch(persons);
26-
personRepository.GetAll().ToList().ForEach(person => Console.WriteLine(person.Name));
28+
personRepository.InsertBatch(persons); // insertBatch
29+
var people = personRepository.GetAll().ToList(); // getAll
30+
people.ForEach(person => Console.WriteLine(person.Name));
31+
32+
Console.WriteLine("\r\nDelete");
33+
personRepository.Delete(people[0].Id); // delete
34+
personRepository.GetAll().ToList().ForEach(person => Console.WriteLine(person.Name));// getAll
35+
36+
Console.ReadKey();
2737
}
2838
}
2939

3040

3141
// repository
32-
3342
public class PersonRepository : RepositoryBase<Person>
3443
{
3544
public PersonRepository(string pathDbFile) : base(pathDbFile)
@@ -40,27 +49,18 @@ public PersonRepository(string pathDbFile) : base(pathDbFile)
4049

4150

4251
// entity
43-
4452
public class Person
4553
{
4654
[PrimaryKey] // required
47-
[AutoIncrement] // optional
4855
public int Id { get; set; }
49-
public string Name { get; set; }
5056
public bool IsDriver { get; set; }
57+
public string Name { get; set; }
5158

52-
// required constructor with all properties in alphabetical order
5359
public Person(int id, bool isDriver, string name)
5460
{
5561
Id = id;
5662
IsDriver = isDriver;
5763
Name = name;
5864
}
59-
60-
public Person(bool isDriver, string name)
61-
{
62-
IsDriver = isDriver;
63-
Name = name;
64-
}
6565
}
6666
}

samples/AbstractCrud/Sample.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
<OutputType>Exe</OutputType>
44
<TargetFramework>netcoreapp6.0</TargetFramework>
55
</PropertyGroup>
6+
67
<ItemGroup>
7-
<Reference Include="SQLiteAbstractCrud">
8-
<HintPath>..\..\src\SQLiteAbstractCrud\bin\Debug\net6.0\SQLiteAbstractCrud.dll</HintPath>
9-
</Reference>
8+
<PackageReference Include="System.Data.SQLite" Version="1.0.115.5" />
109
</ItemGroup>
1110

12-
1311
<ItemGroup>
14-
<PackageReference Include="System.Data.SQLite" Version="1.0.115.5" />
12+
<ProjectReference Include="..\..\src\SQLiteAbstractCrud\SQLiteAbstractCrud.csproj" />
1513
</ItemGroup>
1614
</Project>

src/SQLiteAbstractCrud.Proxy/Attributes/AutoIncrementAttribute.cs renamed to src/SQLiteAbstractCrud/Proxy/Attributes/AutoIncrementAttribute.cs

File renamed without changes.

src/SQLiteAbstractCrud.Proxy/Attributes/PrimaryKeyAttribute.cs renamed to src/SQLiteAbstractCrud/Proxy/Attributes/PrimaryKeyAttribute.cs

File renamed without changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Reflection;
4+
using SQLiteAbstractCrud.Proxy.Queries;
5+
6+
namespace SQLiteAbstractCrud.Proxy
7+
{
8+
internal class Fields
9+
{
10+
public Fields(List<ProxyPropertyInfo> proxyPropertiesInfos)
11+
{
12+
foreach (var p in proxyPropertiesInfos)
13+
{
14+
var field = new Field(p.OriginalName, p.CSharpType, p.IsPrimaryKey, p.IsAutoIncrement);
15+
Items.Add(field);
16+
}
17+
}
18+
19+
public List<Field> Items { get; } = new();
20+
21+
public string GetPrimaryKeyName()
22+
{
23+
return Items.First(x => x.IsPrimaryKey).NameOnDb;
24+
}
25+
26+
public IEnumerable<Field> GetPrimariesKeys()
27+
{
28+
return Items.Where(x => x.IsPrimaryKey);
29+
}
30+
31+
public string GetQuotePrimaryKey()
32+
{
33+
return Items.First(x => x.IsPrimaryKey).Quote;
34+
}
35+
36+
public string GetPrimaryKeyType()
37+
{
38+
return Items.First(x => x.IsPrimaryKey).TypeCSharp;
39+
}
40+
}
41+
}
File renamed without changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Linq;
2+
using System.Reflection;
3+
using SQLiteAbstractCrud.Proxy.Attributes;
4+
5+
namespace SQLiteAbstractCrud.Proxy
6+
{
7+
public sealed class ProxyPropertyInfo
8+
{
9+
public int OriginalOrder { get; }
10+
private PropertyInfo _propertyInfo;
11+
public string OriginalName => _propertyInfo.Name;
12+
13+
public string CSharpType => _propertyInfo.PropertyType.Name;
14+
15+
public bool IsPrimaryKey
16+
{
17+
get
18+
{
19+
var primaryKeyAttribute = _propertyInfo.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
20+
return primaryKeyAttribute.Any();
21+
}
22+
}
23+
public bool IsAutoIncrement
24+
{
25+
get
26+
{
27+
var autoIncrementAttribute = _propertyInfo.GetCustomAttributes(typeof(AutoIncrementAttribute), true);
28+
return autoIncrementAttribute.Any();
29+
}
30+
}
31+
public int ProxyOrder { get; }
32+
33+
public ProxyPropertyInfo(PropertyInfo propertyInfo)
34+
{
35+
this._propertyInfo = propertyInfo;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)