Injecting LiteDb as a service in ASP.NET Core

Intoduction

LiteDB is a simple, fast and lightweight embedded .NET document database. LiteDB was inspired by the MongoDB database and its API is very similar to MongoDB’s official .NET API.

Situation

I have been using LiteDb for some of my smaller projects and I’ve got to tell you that so far it has beed satisfying. But my problem was injecting LiteDb as a service inside ASP.NET Core applications. Usually in ASP.NET Core all buit in services are added like this :

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

Of course it’s possible to inject your services using “Action” syntax like this :

 public void ConfigureServices(IServiceCollection services)
        {
            ServicesConfig.Register(services);
        }

         public static void Register(IServiceCollection services)
        {
           
            services.AddTransient<IProductService, ProductService>();
            services.AddTransient<IOrderService, OrderService>();
        }

But thats not what I want. I want to have a neat looking startup. And I have seen a couple of popular frameworks like AutoMapper already using this :

     services.AddAutoMapper();

Implemention

I have always wanted to have something similar with my other services.So I tried to implement it for LiteDb.

First Step

first I used a different class to to keep all necessary configurations separately. For LiteDb we only need a path to create database file.

Second Step

Next I added an other class for the actual LiteDb service.

Finally

The last step would be to add an extension to IServiceCollection interface.

Then all we need to do is add “usings” and call *AddLiteDb” in ConfigureServices method at startup.

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddLiteDb(@"bug.db");
        }

Conclusion

Extensions in C# usally help with technical needs but in many cases we use them to add cleaner look in code. The best was to create an extension is to move backwards and start from the end.

You can download the source code here : https://github.com/codehaks/BugPages

Comments

comments powered by Disqus