@@ -9,68 +9,66 @@ namespace Repository
9
9
// Implements IRepository using a SqlKataDatabase
10
10
public abstract class SqlKataRepository < T > : IRepository < T >
11
11
{
12
+ // hold a reference to the SqlKataDatabase instance
12
13
protected readonly SqlKataDatabase Db ;
13
- protected readonly string tableName ;
14
+
15
+ // name of the table this repository utilizes
16
+ protected readonly string TableName ;
14
17
18
+ /// <param name="db">Instance of the database object</param>
19
+ /// <param name="tableName">Name of the table to be utilized</param>
15
20
public SqlKataRepository ( SqlKataDatabase db , string tableName )
16
21
{
17
- this . Db = db ;
18
- this . tableName = tableName ;
22
+ Db = db ;
23
+ TableName = tableName ;
19
24
}
20
25
21
- public abstract object ToRow ( T entry ) ;
26
+ /// <summary>
27
+ /// Converts a object of type T to a generic untyped object (to a table row)
28
+ /// </summary>
29
+ /// <param name="entry">An object of type T to be converted</param>
30
+ /// <returns>Generic object (table row) representing the given object of type T</returns>
31
+ protected abstract object ToRow ( T entry ) ;
22
32
23
- public abstract T FromRow ( dynamic d ) ;
33
+ /// <summary>
34
+ /// Converts a generic object (table row) to an object of type T
35
+ /// </summary>
36
+ /// <param name="d">Object to be converted to an object of type T</param>
37
+ /// <returns>Returns an object of type T converted from the generic object (table row)</returns>
38
+ protected abstract T FromRow ( dynamic d ) ;
24
39
25
- protected abstract int _getEntryId ( T entry ) ;
40
+ /// <summary>
41
+ /// Returns an ID of the given object of type T
42
+ /// </summary>
43
+ /// <param name="entry">Object whose ID should be returner</param>
44
+ /// <returns>ID of the given object</returns>
45
+ protected abstract int GetEntryId ( T entry ) ;
26
46
27
- public int Add ( T entry )
28
- {
29
- var id = Db . Get ( ) . Query ( tableName ) . InsertGetId < int > ( ToRow ( entry ) ) ;
30
- return id ;
31
- }
32
-
33
- public bool Update ( T entry )
34
- {
35
- var updated = Db . Get ( ) . Query ( tableName ) . Where ( "id" , _getEntryId ( entry ) ) . Update ( ToRow ( entry ) ) ;
36
- return updated > 0 ;
37
- }
47
+ // use the InsertGetId method of the SqlKata library in order to insert a new row and get it's ID
48
+ public int Add ( T entry ) => Db . Get ( ) . Query ( TableName ) . InsertGetId < int > ( ToRow ( entry ) ) ;
38
49
39
- public bool Delete ( T entry )
40
- {
41
- Db . Get ( ) . Query ( tableName ) . Where ( "id" , _getEntryId ( entry ) ) . Delete ( ) ;
42
- return true ;
43
- }
50
+ // updates the given object of type T using the WHERE and UPDATE method calls, returns a boolean flag indicating
51
+ // whether at least one table row was updated
52
+ public bool Update ( T entry ) =>
53
+ Db . Get ( ) . Query ( TableName ) . Where ( SqlSchema . TableIdPrimaryKey , GetEntryId ( entry ) ) . Update ( ToRow ( entry ) ) > 0 ;
44
54
55
+ // deletes the given object of type T and returns a boolean flag indicating whether at least one table row was deleted
56
+ public bool Delete ( T entry ) => Db . Get ( ) . Query ( TableName ) . Where ( SqlSchema . TableIdPrimaryKey , GetEntryId ( entry ) ) . Delete ( ) > 0 ;
57
+
45
58
public T Get ( int id )
46
59
{
47
- var result = Db . Get ( ) . Query ( tableName ) . Where ( "id" , id ) . FirstOrDefault ( ) ;
48
- if ( result == null )
49
- {
50
- return default ;
51
- }
52
-
53
- return FromRow ( result ) ;
60
+ // find a table rows based on the given ID
61
+ var result = Db . Get ( ) . Query ( TableName ) . Where ( SqlSchema . TableIdPrimaryKey , id ) . FirstOrDefault ( ) ;
62
+
63
+ // convert the given table row a ta an object of type T
64
+ return result == null ? default : FromRow ( result ) ;
54
65
}
55
66
56
- public List < T > GetAll ( ) => RowsToObjects ( Db . Get ( ) . Query ( tableName ) . Select ( ) . Get ( ) ) ;
67
+ // select all rows from the database and converts them to objects of type T
68
+ public List < T > GetAll ( ) => RowsToObjects ( Db . Get ( ) . Query ( TableName ) . Select ( ) . Get ( ) ) ;
57
69
58
- protected List < T > RowsToObjects ( IEnumerable < dynamic > rows )
59
- {
60
- List < T > items = new List < T > ( ) ;
61
- foreach ( var row in rows )
62
- {
63
- items . Add ( FromRow ( row ) ) ;
64
- }
65
-
66
- return items ;
67
- }
68
- }
69
-
70
- public class SqlKataRepositoryException : Exception
71
- {
72
- public SqlKataRepositoryException ( string message ) : base ( message )
73
- {
74
- }
70
+ // converts a collection of table rows to a list of objects of type T
71
+ protected List < T > RowsToObjects ( IEnumerable < dynamic > rows ) => rows . Select ( row => ( T ) FromRow ( row ) ) . ToList ( ) ;
72
+
75
73
}
76
74
}
0 commit comments