0

Ich habe ein einfaches generisches Repository.Can Set() -Methode im Repository mit Bezug zu meinem DBcontext

In diesem Repository habe ich einen generischen DBContext und das heißt, es ist nicht spezifisch für meine Anwendung.

Die generischen DbContext sind noch in der Lage meiner DBcontexts entites zugreifen, wie

Context.Set<TEntity>().Add(entity) 

die Methode Set Do() hier Bezug auf meinen webapplikationens Geting DbContext, oder was geschieht?

Voll Code:

public interface IRepository<TEntity> where TEntity : class 
    { 
     TEntity Get(int id); 
     void Add(TEntity entity); 
    } 

... 

    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class 
    { 
     protected readonly DbContext Context; 

     public Repository(DbContext context) 
     { 
      Context = context; 
     } 

     public TEntity Get(int id) 
     { 
      // Here we are working with a DbContext, not PlutoContext. So we don't have DbSets 
      // such as Courses or Authors, and we need to use the generic Set() method to access them. 
      //Returns a non-generic DbSet instance for access to entities of the given type in the context and the underlying store . 
      return Context.Set<TEntity>().Find(id); 
     } 

     public void Add(TEntity entity) 
     { 
      Context.Set<TEntity>().Add(entity); 
     } 
    } 
................................. 

    public interface IAuthorRepository : IRepository<Author> 
    { 
     Author GetAuthorWithCourses(int id); 
    } 
.. 

    public class AuthorRepository : Repository<Author>, IAuthorRepository 
    { 
     public AuthorRepository(PlutoContext context) : base(context) 
     { 
     } 

     public Author GetAuthorWithCourses(int id) 
     { 
      return PlutoContext.Authors.Include(a => a.Courses).SingleOrDefault(a => a.Id == id); 
     } 

     public PlutoContext PlutoContext 
     { 
      get { return Context as PlutoContext; } 
     } 
    } 
.. 


    public interface IUnitOfWork : IDisposable 
    { 
     IAuthorRepository Authors { get; } 
     int Complete(); 
    } 
... 

    public class UnitOfWork : IUnitOfWork 
    { 
     private readonly PlutoContext _context; 

     public UnitOfWork(PlutoContext context) 
     { 
      _context = context; 
      Authors = new AuthorRepository(_context); 
     } 

     public IAuthorRepository Authors { get; private set; } 

     public int Complete() 
     { 
      return _context.SaveChanges(); 
     } 

     public void Dispose() 
     { 
      _context.Dispose(); 
     } 
    } 


... 

     public class Repository<Entity> : IRepository<Entity> where Entity : class 
     { 
     protected readonly DbContext Context; 

     public Repository(DbContext context) 
     { 
      Context = context; 
     } 


     public void Add(Entity entity) 
     { 
      Context.Set<TEntity>().Add(entity); 
     } 
    } 
+0

'Set()' gibt jede Entität zurück, die im Kontext-Subtyp registriert ist, den Sie in das Repository injizieren. –

+0

Wenn wir uns vorstellen, dass ich mein generisches Repository mit dieser Entität "House" injiziere. Diese Zeile Context.Set () Find (id); wird in meinem Repository übersetzt in: Context.House(). Finde (id); Aber Context sie ist immer noch generisch und sollte ich es nicht konvertieren zu Meine Anwendung DBContext? –

+0

Das Argument 'DbContext context' ist nur ein * Kompilierzeit * -Typ, der * tatsächliche * Typ des Parameters wird nicht geändert. Aber warum versuchst du es nicht einfach? Normalerweise sieht man es, hilft es zu verstehen. –

Antwort

0

Das Argument DbContext Kontext ist nur ein Kompilierung-Typ, die tatsächliche Art des Parameters nicht geändert wird.

Verwandte Themen