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.

其他提示

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:

MyProject
├── 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
许可以下: CC-BY-SA归因
scroll top