IDisposable

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