0

This is really a question about what would be a recommended pattern...

I use ASP.NET Core 2.0 and use dependency injection to allow my controllers and other classes to have access to a IMyDatabaseRepositor, via class constructors.

I also have several other projects in separate assemblies. I was thinking of allowing these other projects do some repository work as well. We're really talking about providing access to an existing DbContext.

Should I somehow pass the IMyDatabaseRepository instance to methods in the other project classes, or should those classes simply instantiate their own IMyDatabaseRepository (and do all their own "Startup" and connection string stuff for database and DI)?

I'm not sure if the other project classes can use the IServicesCollection somehow as well in order to get DI instances from my main ASP.NET Core web app.

Any thoughts?

1 Answer 1

1

Libraries should not compose object graphs, only the startup project should.

This means that in the library you simply use Constructor Injection, while in your Startup class you register all components from all libraries.

Sign up to request clarification or add additional context in comments.

1 Comment

Steven, you are correct. It was like magic. I did just use normal dependency injection in my separate class libraries and given they were executing along side the web app (with Startup), the injection just worked. Very cool indeed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.