ASP.NET Core – Best Practices for Sharing a Database with Other Projects

asp.net-corecentity-framework

I have multiple ASP.NET Core web applications that need to share an employee database. I want to be able to write the Repository and Models once and use it in multiple projects.

What is best practice for this and how can I achieve this with ASP.NET Core?

Using VS2017, SQL Server and IIS right now.

Best Answer

I would actually advise against doing that. Sharing a database - any kind of database - between multiple applications is pretty much coupling central, and you can find yourself in a very tough spot down the line when any kind of schema changes are required. There's actually an anti-pattern for this - the Integration Database (https://martinfowler.com/bliki/IntegrationDatabase.html).

Sharing a data access layer is a between the applications is a possibility, whether by a package manager or by source control shared folders, but in my experience this actually makes the flexibility and maintainability of the systems a hundred times worse. The coupling between the systems may make the timing of database updates significantly harder.

Instead, I would recommend making one of the systems responsible for the database schema. Usually, there's an application whose domain more naturally covers the data. Then, expose access to the data via some public API, whether WCF, HTTP, a message bus, or whatever. This gives you a layer of insulation from change; when the database has reason to change, only the responsible application needs to update at the same time. As far as the other applications are concerned, nothing changes when this happens, as the API remains the same.

Related Topic