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.


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.

  • 2
    Not sure I agree with this. Add additional layers of indirection if you think they're needed for decoupling, but a single source of truth (i.e. one database) can be incredibly useful. I don't know what you mean by "make one of the systems responsible for the database schema." I would think that would be the RDBMS itself. – Robert Harvey May 4 '17 at 19:52

What I have done in the past is setup a git repo that shares multiple solutions. Both solutions would share the Repository and Domain projects

The folder structure would be something like this:

├── Solutions
│   ├── MyProject.SolutionOne.sln
│   ├── MyProject.SolutionTwo.sln
|   |
├── Projects
|   |
│   ├── MyProject.SolutionOne.Web
│   │   ├── MyProject.SolutionOne.Web.csproj
|   |   
│   ├── MyProject.SolutionTwo.Web
│   │   ├── MyProject.SolutionTwo.Web.csproj
|   |
│   ├── MyProject.Repository
│   │   ├── MyProject.Repository.csproj
|   |
│   ├── MyProject.Domain
│   │   ├── MyProject.Domain.csproj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.