Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Common Methods for all Repositories #1422


User avatar
0
shyamjith created

Hi,

I need to write some common methods for all repositories , how do i achieve this? I dont want to write custom repository for all entities . I need to use either ABP default repository or generic repository.

In RepositoryBase class I have seen an area for writing common methods

public abstract class SampleRepositoryBase<TEntity, TPrimaryKey> : EfRepositoryBase<SampleDbContext, TEntity, TPrimaryKey>
        where TEntity : class, IEntity<TPrimaryKey>
    {
        protected SampleRepositoryBase(IDbContextProvider<SampleDbContext> dbContextProvider)
            : base(dbContextProvider)
        {

        }
       
        //add common methods for all repositories
    }

I did wrote code here but i can't invoke it using Repository


1 Answer(s)
  • User Avatar
    0
    gpcaretti created

    <cite>Shyamjith: </cite> I did wrote code here but i can't invoke it using Repository

    What do you mean with this sentence? Do you mean you don't see methods from a client class of your repository? If so, it is a matter of interfaces.

    Your concrete repository is probably declare in this way:

    public class MySpecificRepo : SampleRepositoryBase<MyEntity, int>, IMySpecificRepo {
       ...
    }
    

    This means, clients see your class by the interface IMySpecificRepo.

    So you have to define a ISampleRepositoryBase interface declaring the new methods added to SampleRepositoryBase and declare your Concrete Interface by extending it:

    public interface ISampleRepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> {
       // here your additional common methods declarations
      ...
    }
    
     public interface IMySpecificRepo :  ISampleRepositoryBase<MyEntity, int> {
       // here your specific repository methods
       ...
    }
    
    public class MySpecificRepo : SampleRepositoryBase<MyEntity, int>, IMySpecificRepo {
       // here the implementations
       ...
    }
    

    In this way all works

    Gp