Atl.Repository.Standard – Using IOC or a DI Container

If you haven’t already read the Basic Setup guide please read it here. As this guide requires knowledge from the previous article.

In the previous article we saw how to setup the Domain classes, the System clock, the DB Context factory generator, the Id Generator and Domain injector and to use them to create the repository and use the repository.

This is a pretty small article, as we don’t have to do a lot of this here. All we need to do is inject the components into one of your favorite DI container and resolve the repository to use it.

In this example I am going to use the default service provider for ASP.Net Core. You can find it at nuget package Microsoft.Extensions.DependencyInjection. Lets install it first –

Install-Package Microsoft.Extensions.DependencyInjection

//for dotnet use

dotnet add package Microsoft.Extensions.DependencyInjection

And lets create our service collection –

var serviceCollection = new ServiceCollection();

And inject the required service for the repository –

//register repository

serviceCollection.AddTransient<IDomainContextFactory<DatabaseContext>, DomainContextFactory>();
serviceCollection.AddTransient<IDomainInjector, DomainInjector>();
serviceCollection.AddTransient<IKeyGenerator<long>, IdentityKeyGenerator>();
serviceCollection.AddTransient<IConfigurationProvider, ConfigProvider>();
serviceCollection.AddTransient<ISystemClock, DefaultSystemClock>();
serviceCollection.AddTransient(typeof(IGenericRepository<>), typeof(Repository<>));


//build the service provider from service collection
ServiceProvider = serviceCollection.BuildServiceProvider();

Here please note that I am using the default provided Databasecontext class, but if you want you can also subclass it and provide that class instead, in case you need some special code to run for the context.

You can either use Scoped or Transient lifestyle. There is not dependency on any specific lifestyle.

Then inject the repository to you service and use them right away .. 🙂

public class OrderService : IOrderService
        private readonly IGenericRepository<long> _repository;
        public OrderService(IGenericRepository<long> repository, IMapper mapper, ICacheStore cacheStore)
            _repository = repository;

            _repository.GetAll<...>().Where(x => x.... == ...).OrderBy(x => x....).ToList();

I would like to say something ...

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at

Up ↑

%d bloggers like this: