我使用 this tutorial to create a Database model .
我决定通过阅读 Juwal Lowy 的“Programming .NET Components”这句话来为抽象创建另一个程序集,例如 IRepository
、IFooService
:
Because interfaces can be implemented by multiple components, it's good practice to put them in a separate assembly from that of the implementing components.
我的解决方案结构可能如下所示:
PersonsApp.Solution
--PersonsApp.WebUI
-- Controllers (PersonController)
--PersonApp.Common
--Core folder
-IGenericRepository.cs (Abstraction)
-IUnitOfWork.cs (Abstraction)
--Repositories folder
-IPersonRepository.cs (Abstraction)
--PersonApp.Persistence
--Infrastructure folder
-DbDactory.cs (Implementation)
-Disposable.cs (Implementation)
-IDbFactory.cs (Abstraction)
-RepositoryBase.cs (Abstraction)
--Models folder(Here we have DbContext, EF models (Implementation))
-Person.cs (Implementation)
-PersonContext.cs (Implementation)
--Repositories folder
-PersonRepository.cs (Implementation)
但是,PersonApp.Persistence
项目引用了 PersonApp.Common
。但是我的项目 PersonApp.Common
还需要引用 PersonApp.Persistence
因为我想在 Person 中创建一个
:IPersonRepository.cs
.普通
public interface IPersonRepository : IRepository<Person> {}
当我尝试将对 PersonApp.Persistence
的引用添加到 PersonApp.Common
时,抛出以下错误:
A reference to 'PersonApp.Persistence' could not be added. Adding this project as a reference would cause a circular dependency.
但是,我真的很想有单独的接口(interface)程序集和 Entity Framework 的另一个程序集。
如何将存储库接口(interface)移动到另一个程序集?
另外,我希望以后能够to keep the database schema in sync with the EF Core model by preserving data .
我使用 DatabaseFirst。预先感谢。任何帮助将不胜感激!
最佳答案
我将成为这里的弃儿并给你一个你可能不喜欢的答案,并且可能线程中的大多数其他人也不会。答案是……不要使用存储库。
多年前,当它是宇宙中最流行的东西时,我就试过了。我花了几个月的时间试图理解、实现和使用它。直到今天,当涉及到存储库时,我仍然无法理解实现的好处与复杂性。最后我停止尝试并将它们从我拥有的任何项目中删除。我从不回头。
我敢肯定有人会反驳我的说法,但这只是我的经验,我觉得 OP 正在走上我开发历史上的黑暗时期。
就您的项目结构而言,我建议如下:
PersonsApp.Solution
-- PersonsApp (optional)
-- Extensions/ (any root-level extensions you want available to the other projects)
-- PersonsApp.Data
-- references PersonsApp
-- Configurations/ (contains all IEntityTypeConfiguration<T> for your POCOs)
-- Models/ (contains all of your POCOs)
-- DbContext.cs
-- PersonsApp.Web
-- references PersonsApp and PersonsApp.Data
-- Extensions/ (DbContext extensions to project data the same way you would with a repository, without the repository and with less code)
-- ??? (your controllers, views, etc to display your app to your users)
这现在解决了您的循环依赖。
我还建议您查看 Jimmy Bogard's Vertical Slices Architecture使用 MediatR。当我第一次了解到它时,它让我大吃一惊,与过于复杂的 DDD 架构不同,使用它真是太棒了。
在我的例子中,PersonsApp.Web
结构如下所示:
-- PersonsApp.Web
-- Features/ (all vertical slices)
-- Resources/ (all scripts and styles that will be compiled by Gulp into the wwwroot folder)
-- Program.cs
-- Startup.cs
我使用 AutoMapper 和 Entity Framework Plus 的 Future Queries 在垂直切片上更进了一步。无耻自塞给my blog talking关于它。虽然有点过时,但这是一个不错的起点。我应该尽快更新它。
关于c# - EF Core,数据库优先。如何将通用存储库接口(interface)移动到另一个程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841727/