通用工作单元

Vya*_*Rao 3 c# entity-framework unit-of-work repository-pattern

我已经实现了 EntityFramework 模式以及 Repository 和 Unit Of Work。该实现类似于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
}
Run Code Online (Sandbox Code Playgroud)

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

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

Soh*_*deh 5

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

请参阅此代码:

using System.Data.Entity;
using System;


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

为什么要使用 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; }
}
Run Code Online (Sandbox Code Playgroud)

产品

    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; }
}
Run Code Online (Sandbox Code Playgroud)

工作单位

  public interface IUnitOfWork
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

数据库上下文

    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
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*ria 4

您可以创建类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
}
Run Code Online (Sandbox Code Playgroud)

像这样的东西应该有效。