|
|
|
|
<!doctype html>
|
|
|
|
|
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-dbcontext-repository" data-has-hydrated="false">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="generator" content="Docusaurus v3.0.0">
|
|
|
|
|
<title data-rh="true">9.5 仓储模式 | Furion</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="http://furion.baiqian.ltd/docs/dbcontext-repository"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="9.5 仓储模式 | Furion"><meta data-rh="true" name="description" content="📝 模块更新日志"><meta data-rh="true" property="og:description" content="📝 模块更新日志"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="http://furion.baiqian.ltd/docs/dbcontext-repository"><link data-rh="true" rel="alternate" href="http://furion.baiqian.ltd/docs/dbcontext-repository" hreflang="en"><link data-rh="true" rel="alternate" href="http://furion.baiqian.ltd/docs/dbcontext-repository" hreflang="x-default"><link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="Furion RSS Feed">
|
|
|
|
|
<link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="Furion Atom Feed"><link rel="stylesheet" href="/assets/css/styles.3f87a095.css">
|
|
|
|
|
<script src="/assets/js/runtime~main.89709a83.js" defer="defer"></script>
|
|
|
|
|
<script src="/assets/js/main.10e5dc01.js" defer="defer"></script>
|
|
|
|
|
</head>
|
|
|
|
|
<body class="navigation-with-keyboard">
|
|
|
|
|
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const a=new URLSearchParams(window.location.search).entries();for(var[t,e]of a)if(t.startsWith("docusaurus-data-")){var n=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(n,e)}}catch(t){}}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())</script><div id="__docusaurus"><div class="floatbar_hUWl"><div class="qrcode_sWwE"><img title="微信扫码关注 Furion 官方公众号" src="/img/weixin_qrcode.jpg" style="display:block"><div>❤️ 关注 Furion 微信公众号有惊喜哦!</div></div><div style="display:flex;flex-direction:row-reverse;align-items:flex-start"><div class="title_SD0k">🫠 遇到问题了</div><div class="extend__5i1"><a class="item_Y35a" href="/docs/subscribe"><div style="flex:1"><div class="itemTitle_If_T">⭐️ VIP 服务 ⭐️</div><div class="itemDesc_dp7r">仅需 499 元/年,尊享 365 天项目无忧</div></div><div class="jiantou_mV_o"></div></a><div class="item_Y35a"><div style="flex:1"><div class="itemTitle_If_T">问题反馈</div><div class="itemDesc_dp7r">到 Furion 开源仓库反馈</div></div><div class="jiantou_mV_o"></div></div></div></div></div><div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><div class="announcementBar_mb4j" style="background-color:#4623d9;color:yellow" role="banner"><div class="content_knG7 announcementBarContent_xLdY">⭐️ 开通 VIP 服务仅需 499 元/年,尊享 365 天项目无忧 <a href="/docs/subscribe" style="background-color:rgb(199, 29, 36);color:#ffffff;padding:1px 10px;border-radius:3px;text-decoration:none;display:inline-block;margin:0 5px;font-size:12px;cursor:pointer;">立即开通</a>⭐️</div></div><nav aria-label="Main" class="navbar navbar--fixed-top navbarHideable_m1mJ"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/furionlogo.png" alt="Furion Logo" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src="/img/furionlogo.png" alt="Furion Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><b class="navbar__title text--truncate">Furion</b></a><a aria-current="page" class="navbar__item navbar__link navbar__link--active" href="/docs/category/appendix">文档</a><a class="navbar__item navbar__link" href="/docs/global/app">静态类</a><a class="navbar__item navbar__link" href="/docs/settings/appsettings">配置</a><a class="navbar__item navbar__link" href="/blog">博客</a><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">更新日志</a><ul class="dropdown__menu"><li><a class="dropdown__link" href="/docs/upgrade">📝 查看日志(v4.9.1.7)</a></li><li><a class="dropdown__link" href="/docs/target">🚀 路线图</a></li></ul></div><a href="http://furion.baiqian.ltd/api/api" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">API<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></div><div class="navbar__items navbar__it
|
|
|
|
|
<li>
|
|
|
|
|
<p><strong>新特性</strong></p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li> <label class="label_p8vM" title="新增" style="background-color:#39b54a"><svg viewBox="0 0 1024 1024" width="14px" height="14px" style="display:block" class="icon_knQK"><path d="M512 71.68c-242.688 0-440.32 197.632-440.32 440.32s197.632 440.32 440.32 440.32 440.32-197.632 440.32-440.32-197.632-440.32-440.32-440.32z m0 819.2c-208.896 0-378.88-169.984-378.88-378.88s169.984-378.88 378.88-378.88 378.88 169.984 378.88 378.88-169.984 378.88-378.88 378.88z" fill="white"></path><path d="M542.72 261.12H481.28v220.16H261.12v61.44h220.16v220.16h61.44v-220.16h220.16V481.28h-220.16z" fill="white"></path></svg> <!-- -->新增</label> <code>IRepositoryFactory<TEntity, TDbContextLocator></code> 仓储功能,解决在 <code>Blazor</code> 中使用 <code>EFCore</code> 问题 <sup>4.9.1.1</sup> <sup>⏱️2023.11.16</sup> <a href="https://gitee.com/dotnetchina/Furion/commit/4285ec0b8debc2d71c7f978126cb3dc394a8ad30" target="_blank" rel="noopener noreferrer">4285ec0</a> <a href="https://learn.microsoft.com/zh-cn/aspnet/core/blazor/blazor-ef-core?view=aspnetcore-7.0" target="_blank" rel="noopener noreferrer">文档说明</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</li>
|
|
|
|
|
</ul></div></div></div></div></details>
|
|
|
|
|
<h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="951-什么是仓储">9.5.1 什么是仓储<a href="#951-什么是仓储" class="hash-link" aria-label="Direct link to 9.5.1 什么是仓储" title="Direct link to 9.5.1 什么是仓储"></a></h2>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>在领域层和数据映射层的中介,使用类似集合的接口来存取领域对象,实际上,仓储被用于领域对象在数据库上的操作(实体 Entity 和值对象 Value types)。一般来说,我们针对不同的实体(或聚合根 Aggregate Root)会创建相对应的仓储。</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<p>简单来说,仓储就是数据存取操作的载体,但不限定于数据库。</p>
|
|
|
|
|
<h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="952-内置仓储">9.5.2 内置仓储<a href="#952-内置仓储" class="hash-link" aria-label="Direct link to 9.5.2 内置仓储" title="Direct link to 9.5.2 内置仓储"></a></h2>
|
|
|
|
|
<p><code>Furion</code> 框架内置了一个数据库操作的仓储,方便大家拓展和集成:</p>
|
|
|
|
|
<div class="theme-admonition theme-admonition-info admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>关于依赖注入说明</div><div class="admonitionContent_BuS1"><p>目前能够被依赖注入解析服务的仓储有:</p><ul>
|
|
|
|
|
<li><code>IRepository</code></li>
|
|
|
|
|
<li><code>IRepository<TEntity></code></li>
|
|
|
|
|
<li><code>IRepository<TEntity, TDbContextLocator></code></li>
|
|
|
|
|
<li><code>ISqlRepository</code></li>
|
|
|
|
|
<li><code>ISqlRepository<TDbContextLocator></code></li>
|
|
|
|
|
<li><code>IMSRepository</code></li>
|
|
|
|
|
<li><code>IMSRepository<TMasterDbContextLocator></code></li>
|
|
|
|
|
<li><code>IMSRepository<TMasterDbContextLocator,...TSlaveDbContextLocator8></code></li>
|
|
|
|
|
<li><code>IDbRepository<TDbContextLocator></code></li>
|
|
|
|
|
</ul><p>还有两个私有仓储,也是所有仓储的基类(用于高级自定义开发)</p><ul>
|
|
|
|
|
<li><code>IPrivateRepository<TEntity></code>:所有实体仓储的基类</li>
|
|
|
|
|
<li><code>IPrivateSqlRepository</code>:所有数据库操作的基类</li>
|
|
|
|
|
</ul><p>除此之后的所有仓储只能通过 <code>rep.Constraint<TRepository>()</code> 进行约束创建,如,只读仓储:</p><div class="language-cs codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-cs codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv codeBlockLinesWithNumbering_o6Pm"><span class="token-line codeLine_lJS_" style="color:#bfc7d5"><span class="codeLineNumber_Tfdd"></span><span class="codeLineContent_feaV"><span class="token class-name keyword" style="color:rgb(255, 203, 107);font-style:italic">var</span><span class="token plain"> readRepository </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> rep</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token generic-method function" style="color:rgb(130, 170, 255)">Constraint</span><span class="token generic-method generic class-name punctuation" style="color:rgb(199, 146, 234)"><</span><span class="token generic-method generic class-name" style="color:rgb(255, 203, 107)">IReadableRepository</span><span class="token generic-method generic class-name punctuation" style="color:rgb(199, 146, 234)"><</span><span class="token generic-method generic class-name" style="color:rgb(255, 203, 107)">TEntity</span><span class="token generic-method generic class-name punctuation" style="color:rgb(199, 146, 234)">></span><span class="token generic-method generic class-name punctuation" style="color:rgb(199, 146, 234)">></span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">;</span></span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></div></div>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9521-非泛型超级仓储">9.5.2.1 非泛型超级仓储<a href="#9521-非泛型超级仓储" class="hash-link" aria-label="Direct link to 9.5.2.1 非泛型超级仓储" title="Direct link to 9.5.2.1 非泛型超级仓储"></a></h3>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><code>IRepository</code>:默认非泛型仓储接口,支持切换到任何仓储</li>
|
|
|
|
|
<li><code>EFCoreRepository</code>:默认非泛型仓储实现</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9522-泛型实体仓储">9.5.2.2 泛型实体仓储<a href="#9522-泛型实体仓储" class="hash-link" aria-label="Direct link to 9.5.2.2 泛型实体仓储" title="Direct link to 9.5.2.2 泛型实体仓储"></a></h3>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><code>IRepository<TEntity></code>:默认数据库实体仓储接口</li>
|
|
|
|
|
<li><code>EFCoreRepository<TEntity></code>:默认数据库实体仓储实现</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9523-泛型多数据库实体仓储">9.5.2.3 泛型多数据库实体仓储<a href="#9523-泛型多数据库实体仓储" class="hash-link" aria-label="Direct link to 9.5.2.3 泛型多数据库实体仓储" title="Direct link to 9.5.2.3 泛型多数据库实体仓储"></a></h3>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><code>IRepository<TEntity, TDbContextLocator></code>:任意数据库的实体仓储接口</li>
|
|
|
|
|
<li><code>EFCoreRepository<TEntity, TDbContextLocator></code>:任意数据库的实体仓储实现</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9524-sql-操作仓储">9.5.2.4 <code>Sql</code> 操作仓储<a href="#9524-sql-操作仓储" class="hash-link" aria-label="Direct link to 9524-sql-操作仓储" title="Direct link to 9524-sql-操作仓储"></a></h3>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><code>ISqlRepository</code>:默认数据库 <code>Sql</code> 操作仓储接口</li>
|
|
|
|
|
<li><code>SqlRepository</code>:默认数据库 <code>Sql</code> 操作仓储实现</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="9525-多数据库-sql-操作仓储">9.5.2.5 多数据库 <code>Sql</code> 操作仓储<a href="#9525-多数据库-sql-操作仓储" class="hash-link" aria-label="Direct link to 9525-多数据库-sql-操作仓储" title="Direct link to 9525-多数据库-sql-操作仓 |