0

As per my understanding GenericRepository is inherited from IGenericRepository. It has properties as IDbFactory DbFactory, DBCustomerEntities dbContext and DBCustomerEntities DbContext. We are getting the value for DBCustomerEntities dbContext using Init method of IDbFactory, which is actually initialising database.

My question is why constructor GenericRepository is required and what is its role?

public class GenericRepository<T> : IGenericRepository<T> where T : class  
{   
    private DBCustomerEntities dbContext;  

    protected IDbFactory DbFactory  
    { get; private set; }  

    protected DBCustomerEntities DbContext  
    {
        get { return dbContext ?? (dbContext = DbFactory.Init()); }  
    }  

    public GenericRepository(IDbFactory dbFactory)  
    {  
        DbFactory = dbFactory;  
    }  

    public IQueryable<T> GetAll()  
    {  
        return DbContext.Set<T>();  
    }   
halfer
  • 19,824
  • 17
  • 99
  • 186
Aniruddha
  • 837
  • 3
  • 13
  • 37
  • It's not clear what you're asking here, other than to explain some basic C#' code. Perhaps you need to learn what generics are and the functionality of the various types you mention? – DavidG May 03 '18 at 10:23
  • 1
    Isn't its role to receive dbFactory from the DI container and assign that to a value stored in the class so that the class can use it? You can contact me for my mailing address to send the prize to. – ProgrammingLlama May 03 '18 at 10:23
  • 1
    The constructor allows the dependency of an instance of `IDbFactory` to be injected into the class for it to use, so that it doesn't have to instantiate it itself and can rely on the instantiating code to decide which implementation of `IDbFactory` it should use. Also, just as a point of interest, `GenericRepository` _implements_ `IGenericRepository` (i.e. provides all of the methods specifed by the `IGenericRepository` interface - it doesn't _inherit_ it, which is a different concept (not trying to be pedantic, they're distinctly different things). – Diado May 03 '18 at 10:29
  • You need to look up dependency injection, constructor injection, and the repository pattern. The class as it is, is a tricky generic way of implementing standard operations on a table , and as most repository solutions are probably over engineered to save a few lines of code for suspect reasons – TheGeneral May 03 '18 at 10:30
  • Thanks Diado. Yes, that's right, it implements not inherit (my bad). Thanks for your explaining too. – Aniruddha May 03 '18 at 10:32
  • Thanks CodeNotFound. – Aniruddha May 03 '18 at 10:33

1 Answers1

2

why constructor GenericRepository is required and what is it's role?

Because you need to inject an implementation of IDbFactory into GenericRepository to let it work. Also, you're looking for abstracting how DbContext is instantiated using a factory, so you don't want to see how the factory is instantiated itself.

IMO, the actual usage of IDbFactory seems ugly to just avoid some lines, and it can be solved as follows (which, in fact, saves more lines!):

public class GenericRepository<T> : IGenericRepository<T> where T : class  
{
    public GenericRepository(IDbFactory dbFactory)  
    {  
        DbContext = new Lazy<DBCustomerEntities>(dbFactory.Init);
    } 


    protected Lazy<DBCustomerEntities> DbContext { get; }

    public IQueryable<T> GetAll() => DbContext.Value.Set<T>();
    .......

When you need to initialize something once only if you access it, you should use Lazy<T>.

There's another thing that looks less promising and it's that you're building a repository relying on IQueryable<T>. Please see this other Q&A: Repository design pattern to get more insights about this topic.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206