通用工作单位

时间:2016-09-06 08:07:18

标签: c# entity-framework repository-pattern unit-of-work

我已经实现了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
}

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

如何实施一般工作单位来处理这种情况?提前谢谢。

3 个答案:

答案 0 :(得分:1)

您可以创建类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
}

这样的事情应该有效。

答案 1 :(得分:1)

Generic UnitOfWork !!! 你实施了错误的工作单元

请参阅本准则:

Sub equalAdder()
While ActiveCell.Value <> ""
    ActiveCell.Value = "=" & ActiveCell.Value
    ActiveCell.Offset(1, 0).Activate
Wend
End Sub

为什么要使用UnitOfWork?

由于:

  • 更好的表现
  • 并发问题
  • 正确使用交易

参见示例:

<强>分类

using System.Data.Entity;
using System;


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

<强>产品

   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; }
}

<强>的UnitOfWork

    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; }
}

<强>的DbContext

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

答案 2 :(得分:0)

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

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