-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
223 lines (188 loc) · 5.74 KB
/
Program.cs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EFCoreVarianceDemo
{
// Base Entity Class
public abstract class Entity
{
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
}
// Specific Entity Types
public class User : Entity
{
public string Username { get; set; }
public string Email { get; set; }
}
public class Admin : User
{
public bool IsSuperAdmin { get; set; }
}
// Covariant Repository Interface
public interface IReadRepository<out T> where T : Entity
{
T GetById(int id);
IQueryable<T> GetAll();
}
// Contravariant Repository Interface
public interface IWriteRepository<in T> where T : Entity
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
// Combined Repository Interface using Variance
public interface IRepository<T> :
IReadRepository<T>,
IWriteRepository<T>
where T : Entity
{
}
// Concrete Repository Implementation (Covariant)
public class EntityReadRepository<T> : IReadRepository<T>
where T : Entity
{
private readonly DbContext _context;
private readonly DbSet<T> _dbSet;
public EntityReadRepository(DbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public T GetById(int id)
{
return _dbSet.Find(id);
}
public IQueryable<T> GetAll()
{
return _dbSet.AsQueryable();
}
}
// Concrete Repository Implementation (Contravariant)
public class EntityWriteRepository<T> : IWriteRepository<T>
where T : Entity
{
private readonly DbContext _context;
private readonly DbSet<T> _dbSet;
public EntityWriteRepository(DbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public void Add(T entity)
{
_dbSet.Add(entity);
_context.SaveChanges();
}
public void Update(T entity)
{
_dbSet.Update(entity);
_context.SaveChanges();
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
_context.SaveChanges();
}
}
// Full Repository Implementation
public class EntityRepository<T> : IRepository<T>
where T : Entity
{
private readonly DbContext _context;
private readonly DbSet<T> _dbSet;
public EntityRepository(DbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
// Covariant Read Methods
public T GetById(int id)
{
return _dbSet.Find(id);
}
public IQueryable<T> GetAll()
{
return _dbSet.AsQueryable();
}
// Contravariant Write Methods
public void Add(T entity)
{
_dbSet.Add(entity);
_context.SaveChanges();
}
public void Update(T entity)
{
_dbSet.Update(entity);
_context.SaveChanges();
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
_context.SaveChanges();
}
}
// DbContext for the example
public class ApplicationDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Admin> Admins { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Use in-memory database for demonstration
optionsBuilder.UseInMemoryDatabase("DemoDatabase");
}
}
// Demonstration Class
public class Variancedemonstration
{
public static void Demonstrate()
{
// Setup DbContext
using var context = new ApplicationDbContext();
// Covariant Read Repositories
IReadRepository<User> userReadRepo = new EntityReadRepository<User>(context);
IReadRepository<Admin> adminReadRepo = new EntityReadRepository<Admin>(context);
// Covariance in action - can treat Admin repo as User repo
IReadRepository<Entity> entityReadRepo = adminReadRepo;
// Contravariant Write Repositories
IWriteRepository<Entity> entityWriteRepo = new EntityWriteRepository<Entity>(context);
IWriteRepository<User> userWriteRepo = entityWriteRepo;
// Full Repository
var userRepo = new EntityRepository<User>(context);
var adminRepo = new EntityRepository<Admin>(context);
// Demonstrate usage
var newUser = new User
{
Username = "johndoe",
Email = "[email protected]",
CreatedAt = DateTime.UtcNow
};
var newAdmin = new Admin
{
Username = "admin",
Email = "[email protected]",
IsSuperAdmin = true,
CreatedAt = DateTime.UtcNow
};
// Add entities
userRepo.Add(newUser);
adminRepo.Add(newAdmin);
// Retrieve entities
var retrievedUser = userRepo.GetById(newUser.Id);
var retrievedAdmin = adminRepo.GetById(newAdmin.Id);
Console.WriteLine($"User: {retrievedUser.Username}");
Console.WriteLine($"Admin: {retrievedAdmin.Username}");
}
}
// Main Program
class Program
{
static void Main(string[] args)
{
Variancedemonstration.Demonstrate();
}
}
}