RepositoryBase and IOC in .NET Core

I am migrating projects to .NET Core, I get good old ‘Object reference not set to an instance of an object…at ServiceStack.RepositoryBase.get_Db()’ when using RepositoryBase.

I have the following within the Startup.cs public void ConfigureServices(IServiceCollection services)

        var connectionString = Configuration.GetValue<string>("AppDb");
        var ormLiteConnectionFactory = new OrmLiteConnectionFactory(connectionString, SqlServer2012Dialect.Provider);
        using (ormLiteConnectionFactory.Open())
            OrmLiteConfig.StringFilter = s => s.Trim(); // Should Trim all strings going into database.

I have tried also registering directly in the SS AppHost within public override void Configure(Container containe)…then I tried removing the startup.cs one, although I need it to work for both of course.

Is RepositoryBase broken?

.NET Core’s IOC doesn’t support property injection so if you wanted to register it in .NET Core’s IServiceCollection you will need to refactor your dependencies to use Constructor Injection.

Either that or you can resolve it via the singleton HostContext.TryResolve<IDbConnectionFactory>() in your base class when needed.

So I am going to need to remove Autowiring everywhere? Or just in the ‘Logic’ layer and below…I can leave the SS services? What about the MVC controllers?

In the case of all the repos inheriting from RepositoryBase I will need to add a constuctor with an IDbConnectionFactory?

You can’t use property injection in ASP.NET Core but you can in ServiceStack, so you would only need to refactor it for dependencies you want to use outside of ServiceStack.

Yes, everywhere outside of ServiceStack that is injected by .NET Core’s IOC, except you wont need to for ServiceStack dependencies accessible within the ServiceStackController base class.

Right, you’d need to make all dependencies available via constructor injection.

Ok, thanks, it won’t be the end of the world and I prefer constructor injection these days anyway. :slight_smile:

A quick follow up question, after doing all the repos I have a ControllerBase that inherits from ServiceStackController …you said “you wont need to for ServiceStack dependencies accessible within the ServiceStackController base class.”…I probably first took that as dependencies in general…but you do say “ServiceStack dependencies”.

So just for confirmation, I’ll need to constructor inject all of my own classes? A simple ‘Yes’ answer is fine.

Yes you do for your own classes, you don’t for any of the dependencies in the ServiceStackController base class.