博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IDisposable
阅读量:5236 次
发布时间:2019-06-14

本文共 4903 字,大约阅读时间需要 16 分钟。

public class Project

{
public string Id { get; set; }
public string Name { get; set; }

public virtual BaseInfo BaseInfo { get; set; }

public Project()

{
this.Id = Guid.NewGuid().ToString();
this.BaseInfo = new BaseInfo();
this.BaseInfo.ProjectId = this.Id;
}
}

public class BaseInfo

{
public string ProjectId { get; set; }
public string No { get; set; }
public double Area { get; set; }

public virtual Project Project { get; set; }

}

public class ProjectEntityTypeConfiguration : EntityTypeConfiguration<Project>

{
public ProjectEntityTypeConfiguration()
{
ToTable("_Project");
HasKey(t => t.Id);

// 这种配置必须手工先删除Info项,再删除Project

HasOptional(p => p.BaseInfo).WithRequired(i => i.Project);

// 这种配置无法删除对象

//HasRequired(p => p.BaseInfo).WithRequiredDependent(i => i.Project);
}
}

public class BaseInfoEntityTypeConfiguration : EntityTypeConfiguration<BaseInfo>

{
public BaseInfoEntityTypeConfiguration()
{
ToTable("_BaseInfo");
HasKey(t => t.ProjectId);
}
}

public class DBContext : DbContext

{
public DBContext() : base()
{

}

public DBContext(string nameOrConnectionString) : base(nameOrConnectionString)

{

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{
if (modelBuilder == null) { throw new ArgumentNullException("modelBuilder"); }

var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()

.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
}

internal class InternalEntityStore<TEntity> where TEntity : class

{
public DbContext Context
{
get;
private set;
}

public IQueryable<TEntity> EntitySet

{
get
{
return this.DbEntitySet;
}
}

public DbSet<TEntity> DbEntitySet

{
get;
private set;
}

public InternalEntityStore(DbContext context)

{
this.Context = context;
this.DbEntitySet = context.Set<TEntity>();
}

public virtual Task<TEntity> GetByIdAsync(object id)

{
return this.DbEntitySet.FindAsync(new object[]
{
id
});
}

public void Create(TEntity entity)

{
this.DbEntitySet.Add(entity);
}

public void Delete(TEntity entity)

{
this.DbEntitySet.Remove(entity);
}

public virtual void Update(TEntity entity)

{
if (entity != null)
{
this.Context.Entry<TEntity>(entity).State = EntityState.Modified;
}
}
}

public class EntityStore<TEntity> : IDisposable where TEntity : class

{
public DBContext Context
{
get;
private set;
}

public IQueryable<TEntity> EntitySet

{
get
{
return this._EntityStore.EntitySet;
}
}

private InternalEntityStore<TEntity> _EntityStore;

public EntityStore(DBContext context)

{
if (context == null)
{
throw new ArgumentNullException("context");
}
this.Context = context;
this._EntityStore = new InternalEntityStore<TEntity>(context);
}

public virtual async Task CreateAsync(TEntity entity)

{
this.ThrowIfDisposed();
if (entity == null) { throw new ArgumentNullException("entity"); }

this._EntityStore.Create(entity);

await this.Context.SaveChangesAsync();
}

public virtual async Task DeleteAsync(TEntity entity)

{
this.ThrowIfDisposed();
if (entity == null) { throw new ArgumentNullException("entity"); }

this._EntityStore.Delete(entity);

await this.Context.SaveChangesAsync();
}

public virtual async Task UpdateAsync(TEntity entity)

{
this.ThrowIfDisposed();
if (entity == null) { throw new ArgumentNullException("entity"); }

this._EntityStore.Update(entity);

await this.Context.SaveChangesAsync();
}

public virtual Task<TEntity> FindByIdAsync(string id)

{
this.ThrowIfDisposed();
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentException("id can not be null or empty.");
}
return this._EntityStore.GetByIdAsync(id);
}

private bool _disposed;

public bool DisposeContext
{
get;
set;
}
private void ThrowIfDisposed()
{
if (this._disposed)
{
throw new ObjectDisposedException(base.GetType().Name);
}
}
protected virtual void Dispose(bool disposing)
{
if (this.DisposeContext && disposing && this.Context != null)
{
this.Context.Dispose();
}
this._disposed = true;
this.Context = null;
this._EntityStore = null;
}

public void Dispose()

{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}

public class ProjectStore : EntityStore<Project>

{
public ProjectStore(DBContext context):base(context)
{
_baseInfoStore = new EntityStore<BaseInfo>(context);
}

private EntityStore<BaseInfo> _baseInfoStore;

public override async Task DeleteAsync(Project entity)

{
var info = await _baseInfoStore.FindByIdAsync(entity.Id);
if(info!=null)
{
await _baseInfoStore.DeleteAsync(info);
}
await base.DeleteAsync(entity);
}

private bool _disposed;

protected override void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_baseInfoStore.Dispose();
}
_disposed = true;
}
base.Dispose(disposing);
}
}

转载于:https://www.cnblogs.com/two/p/6939407.html

你可能感兴趣的文章
用于代码检查的错误列表
查看>>
Java线程面试题
查看>>
C#2.0 读word的多个表格到DataGridView或是其它控件 XP Vista
查看>>
sql script: Graphs, Trees, Hierarchies and Recursive Queries
查看>>
Paper Reading: Relation Networks for Object Detection
查看>>
Android中点中overlay弹出带尾巴的气泡的实现
查看>>
Mybatis接口中传递多个参数
查看>>
Dreamweaver层使用八定律
查看>>
Java IO流学习总结
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
数组的几种常用方法总结
查看>>
递归函数,二分运算,正则表达式
查看>>
阅读软件工程的问题
查看>>
【Netty】UDP广播事件
查看>>
(4)Numpy+矩阵计算+和生成
查看>>
ttt
查看>>
[置顶] java处理office文档与pdf文件(一)
查看>>
Flutter之内置动画(转)
查看>>
MySql优化相关概念的理解笔记
查看>>
sql索引影响数据存储位置的示例
查看>>