Skip to content

Custom Repositories

Omar Piani edited this page Feb 14, 2018 · 2 revisions

This example explain how to add a method to all the repositories you have configured for type of "Contact"

 public class EmailRepository : ConfigurationBasedRepository<Contact, string>
    {
        public EmailRepository(ISharpRepositoryConfiguration configuration, string repositoryName = null) : base(configuration, repositoryName)
        {
        }

        public IEnumerable<string> GetAllMails()
        {
            return this.GetAll().SelectMany(c => c.Emails).Select(m => m.EmailAddress).Distinct();
        }
    }

you have to add to your service container your custom repository.

For example in .NET Core in Startup.cs

services.AddTransient<EmailRepository>(r => new EmailRepository(RepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection("sharpRepository"))));

will instantiate EmailRepository how you configured in your appsettings.json and inject in your controller constructor

public EmailsController(EmailRepository repository)

Clone this wiki locally