通用工作单元

Generic Unit Of Work

我已经实现了 EntityFramework 模式以及存储库和工作单元。实现类似于 Code Project Repository Example,但是我需要增强工作单元。

工作单元

public class GenericUnitOfWork : IDisposable
{
    // Initialization code

    public Dictionary<Type, object> repositories = new Dictionary<Type, object>();

    public IRepository<T> Repository<T>() where T : class
    {
        if (repositories.Keys.Contains(typeof(T)) == true)
        {
            return repositories[typeof(T)] as IRepository<T>
        }
        IRepository<T> repo = new Repository<T>(entities);
        repositories.Add(typeof(T), repo);
        return repo;
    }

    // other methods
}

上面的 UoW 是安静的概括,它始终以父存储库 class 为目标。我有另一个实体,例如学生,它有自己的存储库扩展存储库 class。学生特定的存储库有一个方法 "GetStudentMarks()"。现在我不能使用通用工作单元 class,因为它总是指向父存储库。

如何实现一个通用的工作单元来处理这种情况?提前致谢。

您可以制作 class GenericUnitOfWork 泛型,指定实体和存储库类型:

public class GenericUnitOfWork<TRepo, TEntity> : IDisposable
    where TRepo : Repository<TEntity>
{
    // Initialization code

    public Dictionary<Type, TRepo> repositories = new Dictionary<Type, TRepo>();

    public TRepo Repository()
    {
        if (repositories.Keys.Contains(typeof(TEntity)) == true)
        {
            return repositories[typeof(TEntity)];
        }
        TRepo repo = (TRepo)Activator.CreateInstance(
            typeof(TRepo),
            new object[] { /*put there parameters to pass*/ });
        repositories.Add(typeof(TEntity), repo);
        return repo;
    }

    // other methods
}

像这样的东西应该有用。

我想,您想在 GetStudentMarks 方法中对 StudentMarks 执行一些操作。否则,如果您的模型已正确映射,您可以使用其中一种关系数据加载方法加载它们。 否则,如果通用存储库适合您的大多数实体,但您需要一些额外的方法来处理少量实体,那么我建议为这些存储库创建扩展方法:

public static class StudentReposityExtensions
{
    public List<Mark> GetStudentMarks(
        this IRepository<Student> studentRepostitory)
    {
        .....
    }
}

通用工作单元!!! 您实施了错误的工作单元

查看此代码:

using System.Data.Entity;
using System;


namespace EF_Sample07.DataLayer.Context
{
 public interface IUnitOfWork
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
   }
 }

为什么要使用 UnitOfWork?

因为:

  • 更好的性能
  • 并发问题
  • 交易的正确使用

参见示例:

类别

   public class Category
{
    public int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Title { get; set; }


    public virtual ICollection<Product> Products { get; set; }
}

产品

    public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }


    [ForeignKey("CategoryId")]
    public virtual Category Category { get; set; }
    public int CategoryId { get; set; }
}

UnitOfWork

  public interface IUnitOfWork
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}

DbContext

    public class Sample07Context : DbContext, IUnitOfWork
{
    public DbSet<Category> Categories { set; get; }
    public DbSet<Product> Products { set; get; }


    #region IUnitOfWork Members
    public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
    {
        return base.Set<TEntity>();
    }
    public int SaveAllChanges()
    {
        return base.SaveChanges();
    }
    #endregion
}