2016-09-06 3 views
2

저장소 및 작업 단위와 함께 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에 위의 작업

단위는 조용 일반화하고 항상 부모에게 저장소 클래스를 대상으로합니다. 나는 또 다른 엔티티, 예를 들어 student를 가지고 있는데, 이는 리포지토리 클래스를 확장하는 자체 저장소를 가지고 있습니다. 학생용 저장소에는 "GetStudentMarks()"메소드가 있습니다. 이제는 항상 부모 저장소를 가리키기 때문에 일반 작업 단위 클래스를 사용할 수 없습니다.

이러한 상황을 처리하기위한 일반적인 작업 단위 (Unit of Work)를 구현하는 방법은 무엇입니까? 미리 감사드립니다.

답변

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 
} 

뭔가 일을해야한다.

0

GetStudentMarks 메서드에서 StudentMarks를 통해 일부 작업을 수행하려고한다고 가정합니다. 그렇지 않으면 모델이 올바르게 매핑 된 경우 관계 데이터로드 방법 중 하나를 사용하여 모델을로드하면됩니다. 그렇지 않으면, 일반적으로 저장소는 가장 당신의 엔티티에 맞는,하지만 나는이 저장소에 대한 확장 방법을 만드는 것이 좋습니다, 당신의 엔티티의 작은 번호를 몇 가지 추가 방법을 필요로하는 경우 :

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

문제는 일이 개발자는 소원을 기반으로 수업을 만드는 경향이 있습니다. 그래서 저는 그것을 막으려 고 했으므로보다 일반적인 UoW가 필요했습니다. –

1

일반의 UnitOfWork를!

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 
}