SimpleORM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Reflection;

namespace SimpleORM
{
    public class ORM<T> where T : new()
    {
        private string connectionString;
        private string tableName;

        public ORM(string connectionString, string tableName)
        {
            this.connectionString = connectionString;
            this.tableName = tableName;
        }

        public List<T> SelectAll()
        {
            List<T> list = new List<T>();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = $"SELECT * FROM {tableName}";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            T obj = new T();
                            PropertyInfo[] properties = typeof(T).GetProperties();
                            foreach (PropertyInfo property in properties)
                            {
                                property.SetValue(obj, reader[property.Name]);
                            }
                            list.Add(obj);
                        }
                    }
                }
            }
            return list;
        }

        public int Insert(T obj)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = $"INSERT INTO {tableName} VALUES (";
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    sql += $"@{property.Name},";
                }
                sql = sql.TrimEnd(',') + ")";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    foreach (PropertyInfo property in properties)
                    {
                        command.Parameters.AddWithValue($"@{property.Name}", property.GetValue(obj));
                    }
                    return command.ExecuteNonQuery();
                }
            }
        }

        public int Update(T obj)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = $"UPDATE {tableName} SET ";
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    sql += $"{property.Name}=@{property.Name},";
                }
                sql = sql.TrimEnd(',') + " WHERE Id=@Id";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    foreach (PropertyInfo property in properties)
                    {
                        command.Parameters.AddWithValue($"@{property.Name}", property.GetValue(obj));
                    }
                    return command.ExecuteNonQuery();
                }
            }
        }

        public int Delete(int id)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string sql = $"DELETE FROM {tableName} WHERE Id=@Id";
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.Parameters.AddWithValue("@Id", id);
                    return command.ExecuteNonQuery();
                }
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Reflection;

namespace MyORM
{
    public class EntityMapper<TSource, TDestination> where TSource : class where TDestination : class, new()
    {
        private readonly Dictionary<string, PropertyInfo> _sourceProperties;
        private readonly Dictionary<string, PropertyInfo> _destinationProperties;

        public EntityMapper()
        {
            _sourceProperties = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty);
            _destinationProperties = typeof(TDestination).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty);
        }

        public TDestination Map(TSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var destination = new TDestination();

            foreach (var sourceProperty in _sourceProperties)
            {
                if (_destinationProperties.TryGetValue(sourceProperty.Key, out var destinationProperty))
                {
                    destinationProperty.SetValue(destination, sourceProperty.Value.GetValue(source));
                }
            }

            return destination;
        }

        public IEnumerable<TDestination> Map(IEnumerable<TSource> source)
        {
            foreach (var item in source)
            {
                yield return Map(item);
            }
        }
    }
}