Repository?????????
???????????? ???????[ 2016/4/6 10:17:35 ] ?????????????????? ???????
????1.????IRepository??????????????EntityFramework
???????IRepository?????????????????????????????????Service?????????Repository??????????ü????????????????????IRepository??????Mock?????????
?????????????????????У??????????????????????????????????????????????????????????о????????Repository??????????????????????????壬?????????????÷???汾??Repository<T>?????ɡ?nopcommerce????????????????Java?е?α?????????????汾??Repository<T>??????????????Repository<T>??????л??T???????
1 namespace Example.Application
2 {
3 public interface IRepository<T> where T : class
4 {
5 T FindBy(object id);
6
7 IQueryable<T> Query { get; }
8
9 void Add(T entity);
10
11 void Remove(T entity);
12
13 void Update(T entity);
14
15 int Commit();
16 }
17 }
????2.???DbContext????????
??????1?????????????EfDbContext????DbContext??IDbConnectionFactory??ConnectionString??????????????????????DbSettings?У??????????????????????????????е???????
1 namespace Example.Infrastructure.Repository
2 {
3 public class EfDbContext : DbContext?? IDbContext
4 {
5 private DbSettings _dbSettings;
6
7 public EfDbContext(IConfiguration configuration?? ILogger logger?? DbSettings dbSettings) : base(dbSettings.NameOrConnectionString)
8 {
9 this._dbSettings = dbSettings;
10 if (this._dbSettings.DbConnectionFactory != null)
11 {
12 #pragma warning disable
13 Database.DefaultConnectionFactory = this._dbSettings.DbConnectionFactory;
14 }
15 if (configuration.Get<bool>("database.log:"?? false))
16 {
17 this.Database.Log = sql => logger.Information(sql);
18 }
19 this.Database.Log = l => System.Diagnostics.Debug.WriteLine(l);
20 }
21
22 protected override void OnModelCreating(DbModelBuilder modelBuilder)
23 {
24 base.OnModelCreating(modelBuilder);
25
26 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
27 if (_dbSettings.EntityMaps != null)
28 {
29 foreach (var item in _dbSettings.EntityMaps)
30 {
31 modelBuilder.Configurations.Add((dynamic)item);
32 }
33 }
34 if (_dbSettings.ComplexMaps != null)
35 {
36 foreach (var item in _dbSettings.ComplexMaps)
37 {
38 modelBuilder.Configurations.Add((dynamic)item);
39 }
40 }
41 }
42
43 public void SetInitializer<T>() where T : DbContext
44 {
45 if (this._dbSettings.Debug)
46 {
47 if (this._dbSettings.UnitTest)
48 {
49 Database.SetInitializer(new DropCreateDatabaseAlways<T>());
50 }
51 {
52 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<T>());
53 }
54 }
55 else
56 {
57 Database.SetInitializer<T>(null);
58 }
59 }
60
61 public new IDbSet<T> Set<T>() where T : class
62 {
63 return base.Set<T>();
64 }
65
66 public int Commit()
67 {
68 return base.SaveChanges();
69 }
70 }
71 }
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11