-
Notifications
You must be signed in to change notification settings - Fork 1
/
Repository.linq
212 lines (178 loc) · 4.71 KB
/
Repository.linq
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
<Query Kind="Program">
<Output>DataGrids</Output>
<GACReference>Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</GACReference>
<NuGetReference>Autofac</NuGetReference>
<Namespace>Autofac</Namespace>
<Namespace>Autofac.Builder</Namespace>
<Namespace>Microsoft.VisualStudio.TestTools.UnitTesting</Namespace>
</Query>
private const bool IsTest = false;
private static IContainer Container { get; set; }
void Main()
{
RegisterComponents();
DumpListOf<CodeSource>();
DumpListOf<Edition>();
}
void RegisterComponents()
{
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(LocalDbConnector<>)).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(Repository<>)).AsImplementedInterfaces();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t =>
t.Name.EndsWith("Factory") && (t.GetCustomAttributes(typeof(ObsoleteAttribute)).FirstOrDefault() == null))
.AsImplementedInterfaces();
if (IsTest)
{
builder.RegisterGeneric(typeof(MockRepository<>)).AsImplementedInterfaces();
}
Container = builder.Build();
}
void DumpListOf<T>()
{
var list = new List<T>();
using (var lifetime = Container.BeginLifetimeScope())
{
using (var conn = lifetime.Resolve<IDbConnector<T>>().Connection)
{
var repo = lifetime.Resolve<IRepository<T>>();
foreach (var i in Enumerable.Range(1, 20))
{
list.Add(repo.Get(i));
}
}
}
list.Dump();
}
#region Factories
public interface IFactory<T>
{
string Catalog { get; }
string TableName { get; }
string KeyColumn { get; }
T Create(IDataReader result);
}
[Obsolete]
public class CodeSourceV1Factory : CodeSourceFactory
{
public override string Catalog
{
get { return "EncoderServices"; }
}
}
public class CodeSourceV2Factory : CodeSourceFactory
{
public override string Catalog { get { return "TestingGrounds"; } }
}
public abstract class CodeSourceFactory : IFactory<CodeSource>
{
public virtual string Catalog { get { return "EncoderServices"; } }
public virtual string TableName { get { return "CodeSources"; } }
public virtual string KeyColumn { get { return "CodeSourceId"; } }
public virtual CodeSource Create(IDataReader result)
{
var codeSource = new CodeSource();
codeSource.CodeSourceName = result["CodeSourceName"].ToString();
codeSource.Id = Convert.ToInt32(result["CodeSourceId"]);
return codeSource;
}
}
public class EditionV1Factory : EditionFactory { }
public abstract class EditionFactory : IFactory<Edition>
{
public virtual string Catalog { get { return "EncoderServices"; } }
public virtual string TableName { get { return "Editions"; } }
public virtual string KeyColumn { get { return "EditionId"; } }
public virtual Edition Create(IDataReader result)
{
var edition = new Edition();
edition.EditionName = result["EditionName"].ToString();
edition.Id = Convert.ToInt32(result["EditionId"]);
return edition;
}
}
#endregion
#region Repositories
public interface IRepository<T>
{
T Get(int id);
}
public class Repository<T> : IRepository<T> where T : IEntity
{
private IDbConnector<T> _connector;
private IFactory<T> _entityFactory;
public Repository(IDbConnector<T> connector, IFactory<T> entityFactory)
{
_connector = connector;
_entityFactory = entityFactory;
}
public T Get(int id)
{
T entity = default(T);
var cmd = _connector.Connection.CreateCommand();
cmd.CommandText = string.Format("select * from {0} where {1} = @ID", _entityFactory.TableName, _entityFactory.KeyColumn);
cmd.Parameters.Add(new SqlParameter("ID", id));
var result = cmd.ExecuteReader();
while (result.Read())
{
entity = _entityFactory.Create(result);
}
_connector.Connection.Close();
return entity;
}
}
public class MockRepository<T> : IRepository<T> where T : IEntity, new()
{
public MockRepository()
{
}
public T Get(int id)
{
return new T { Id = id };
}
}
#endregion
#region Entities
public interface IEntity
{
int Id { get; set; }
}
public class CodeSource : IEntity
{
public int Id { get; set; }
public string CodeSourceName { get; set; }
}
public class Edition : IEntity
{
public int Id { get; set; }
public string EditionName { get; set; }
}
#endregion
public interface IDbConnector<T>
{
IDbConnection Connection { get; }
}
public class LocalDbConnector<T> : IDbConnector<T>
{
private IFactory<T> _entityFactory;
public LocalDbConnector(IFactory<T> entityFactory)
{
_entityFactory = entityFactory;
}
public IDbConnection Connection
{
get
{
var conn = new SqlConnection(connectionString);
conn.Open();
return conn;
}
}
private string connectionString
{
get
{
return string.Format("Data Source=.\\;Initial Catalog={0};Integrated Security=true;", _entityFactory.Catalog);
}
}
}