10
1

若改造项目,也因历史遗留问题,数据库表设计也可能存在不合理,此时从头开始再搭建如此庞大的架子,感觉会有点虚空,同时也要考虑团队内部情况,不是那么容易上手,反而可能会违背初心,花更多时间和精力在各种模型理解上

我们完全可以为后续做铺垫,先搭建出底层基本设施,再基于此做灵活扩展即可,每个公司项目具体情况都不一样,比如仓储模式可能需要结合项目进行对应 改造,仓储只是提供了一种基本思想,若真将网上普遍流传的模式照搬可能并不是那么好用,可能会认为仓储莫不是一种反模式

.NET Core模块化插件

.NET Core内置提供了AssemblyLoadContext加载dll插件方式实现模块化,然后将其进行注册

        var mvcBuilder = services.AddMvc();
        foreach (var module in modules)
        {
            // Register controller from modules
            mvcBuilder.AddApplicationPart(module.Assembly);
        }

这种方式虽可行,在我看来只能作为一种临时解决方案并不利于长期,因为需额外创建一个新的项目,然后加载所生成dll,由于没有底层设施做支撑,所以极易引起版本不一致问题,而且手动被迫性质太强,实现模块化方案最终的目标则只需关注业务逻辑实现,我们来看看OrchardCore如何实现模块化。

OrchardCore模块化思想

这里我们并不讨论和ABP vNext二者谁更强大,没有任何意义,比如需结合现有项目情况、项目大小、是否为多租户、实施难度等等多方面考虑才能得出基本结论,而不是一味追求当前主流

比如我们只是想实现模块化方案,建议选择OrchardCore来实施,因为很简单,我们可将其剥离为我所用,而后结合项目情况是否考虑利用ABP vNext来进行分层处理。借鉴核心思想、才能保证一切可在控制范围内

首先我们先从整体上对OrchardCore做个认识,细枝末节暂不考虑:基于ASP.NET Core多租户模块化应用框架。

版本管理:无论是底层设施、基本框架、模块都通过包管理,同时框架和包版本基本(包管理走框架包版本)可以统一管理(对于版本升级很重要)

核心思想:模块实现模块特性,通过MSBuild构建主程序所添加实现模块特性的模块包,底层设施扫描模块特性将其注册到容器中,当然模块和模块特性都可进行基本信息描述

OrchardCore模块化原理

整个项目架构如下图所示

file

OrchardCore:底层设施以及可能需要添加的组件(比如本地化、日志、文件存储、缓存、Lucene等)

OrchardCore.Frameworks:MVC框架

OrchardCore.Modules:模块化包(比如邮件服务、后台作业服务、第三方集成等等)

OrchardCore.Modules.Cms:Cms模块包

OrchardCore.Themes:主题管理

OrchardCore.Cms.Web:主程序

我以内置所提供示例程序给大家讲解整个详细流程,而后有需要更细致了解的童鞋就可以很快上手了,如下示例主程序加载示例模块,主程序直接采用引用该示例模块(实际则是通过nuget下载该模块)

file

正常情况下我们通过nuget直接下载的是程序包,而OrchardCore对于入口则是利用MSBuild加载targets文件(其他组件则直接下载对应包),而targets引用对应包,通过这种中转方式根据我的理解主要解决了两个问题,其一则是可以屏蔽底层设施包(一次性下载),最重要的是通过targets文件可自动添加主程序程序集所加载模块包特性

是不是感觉有点懵,那到底是如何加载模块包特性的呢?来,请看如下图,我们以实际操作从头再来做一个完整梳理(注意:为排版美观,如下都将省略OrchardCore前缀)

file

【1】创建Mvc.Web程序,在nuget上下载Application.Mvc.Targets包

【2】创建Mvc.HelloWorld模块,在nuget上下载引用Module.Targets包

【3】Mvc.Web主程序引用我们所使用的Mvc.HelloWorld模块

【4】Application.Mvc.Targets包引用Application.Targets(引入底层设施)和MVC.Core(引入MVC框架)

【5】示例模块引入模块包,该包中存在模块特性(Module类)

【6】Application.Targets包下存在Application.Targets.targets文件,由于主程序引用了该包,添加所引用实现模块特性的包程序集信息到主程序集

到这里我们已经研究完主程序如何识别模块包,接下来则是如何加载模块包以及对应注册服务信息

OrcharCore核心在于OrchardCore和OrchardCore.Abstractions这两个底层设施包

归根到底,其底层设施源码一部分可能从官方源码拷贝过来(自我猜测),为实现多租户模式,势必要构建租户的容器和路由中间件,这中间就涉及在容器中需要维护每一个租户上下文(ShellContext),并且也要跟踪每个租户的状态。

ModularTenantContainerMiddleware作为创建租户容器中间件

ModularTenantRouterMiddleware作为租户路由中间件

30
6

在本指南中,您将使用项目模板将Orchard Core设置为内容管理系统。

您需要什么

.NET SDK的当前版本。您可以从以下网址下载它:https://dotnet.microsoft.com/download.
文本编辑器和终端,您可以在其中键入 dotnet 命令。

创建项目

有多种创建Orchard Core网站和模块的方法。您可以在这里了解更多信息。

在本指南中,我们将使用“代码生成模板”。您可以使用此命令安装最新稳定版本的模板:

dotnet new install OrchardCore.ProjectTemplates::1.5.0-*

注意

要使用模板的开发分支,请添加 --nuget-source https://nuget.cloudsmith.io/orchardcore/preview/v3/index.json

创建一个空文件夹来存放你的网站。打开终端,进入该文件夹并运行以下命令:

dotnet new occms -n MySite

这样就在一个名为 MySite 的文件夹中创建了一个新的Orchard Core CMS项目。

设置站点

应用程序已经由模板创建,但尚未设置。

通过执行以下命令启动应用程序:

dotnet run --project .\MySite\MySite.csproj

Note

如果你正在使用模板的开发分支,请在运行应用程序之前运行dotnet restore .\MySite\MySite.csproj --source https://nuget.cloudsmith.io/orchardcore/preview/v3/index.json

现在您的应用程序应该正在运行,并监听以下端口: 现在监听 on: https://localhost:5001 和 on: http://localhost:5000。应用已经启动,按 Ctrl+C 可以关闭应用。

在浏览器中打开 https://localhost:5001 可以显示设置屏幕。

为了演示的目的,我们将使用 Blog 配方创建网站。Blog 配方是 Orchard Core 的 入门配方之一,其中包含一系列功能和配置 Orchard Core 网站的步骤。

完成设置表单,选择 Blog 配方和 SQLite 数据库。

提交表单后,几秒钟后您将可以看到一个博客站点。

为了配置并开始编写内容,您可以转到 https://localhost:5001/admin

概要

您刚刚创建了一个由Orchard Core CMS驱动的博客引擎。

30
6

你将要构建什么

您将创建一个模块化的 ASP.NET Core MVC 网络应用程序,类似于包含在 Orchard Core 中的 "Hello World" 应用程序示例。它包括一个网络应用程序和一个模块。网络应用程序提供了布局,而模块注册了路由并响应首页请求。您可以参考 Orchard Core 中的以下项目以获取更多信息。

src/OrchardCore.Mvc.Web
src/OrchardCore.Modules/OrchardCore.Mvc.HelloWorld
所需材料
当前版本的 .NET SDK。您可以从此处下载: https://dotnet.microsoft.com/download.
一个文本编辑器和一个终端,您可以在其中运行 dotnet CLI 命令。
创建 Orchard Core 网站和模块
有不同的方法可以为 Orchard Core 创建网站和模块。您可以在 此处 了解更多信息。

在本指南中,我们将使用我们的代码生成模板。您可以使用以下命令安装模板的最新稳定版本: dotnet new install OrchardCore.ProjectTemplates::1.5.0-*

Note

如果想使用模板的开发分支,请添加 --nuget-source https://nuget.cloudsmith.io/orchardcore/preview/v3/index.json

创建一个名为OrchardCore.Mvc的空文件夹,它将包含我们的项目。打开一个终端,进入该文件夹并运行以下命令来创建Web应用程序:

dotnet new ocmvc -n OrchardCore.Mvc.Web

接下来,创建“ Hello World”模块。

dotnet new ocmodulemvc -n OrchardCore.Mvc.HelloWorld

将Web应用程序中指向该模块的项目引用添加到其中。

dotnet add OrchardCore.Mvc.Web reference OrchardCore.Mvc.HelloWorld

可选地,您可以添加一个解决方案文件,该文件引用了Web应用程序和模块,以便在Visual Studio中打开解决方案。

dotnet new sln -n OrchardCore.Mvc
dotnet sln add OrchardCore.Mvc.Web\OrchardCore.Mvc.Web.csproj
dotnet sln add OrchardCore.Mvc.HelloWorld\OrchardCore.Mvc.HelloWorld.csproj
测试生成的应用程序
从包含两个项目的OrchardCore.Mvc根文件夹中运行以下命令启动Web应用程序:

dotnet run --project .\OrchardCore.Mvc.Web\OrchardCore.Mvc.Web.csproj

注意

如果您正在使用模板的开发分支,请在运行应用程序之前运行dotnet restore .\MySite\MySite.csproj source https://nuget.cloudsmith.io/orchardcore/preview/v3/index.json

现在,您的应用程序应该在以下端口上运行和侦听:

现在侦听:https://localhost:5001
现在在http://localhost:5000上进行监听 应用程序已启动。按Ctrl+C关闭。
打开浏览器并导航至https://localhost:5001/OrchardCore.Mvc.HelloWorld/Home/Index。它应该会显示Hello from OrchardCore.Mvc.HelloWorld

布局来自主Web应用程序项目,而控制器、操作和视图来自模块项目。

注册自定义路由

默认情况下,模块中的所有路由都遵循模式{area}/{controller}/{action},其中 {area} 是模块名称。我们将更改模块中视图的路由以响应主页请求。

OrchardCore.Mvc.HelloWorldStartup.cs文件中,在Configure()方法中添加一个自定义路由。


    routes.MapAreaControllerRoute(
        name:“Home”,
        areaName:“OrchardCore.Mvc.HelloWorld”,
        pattern:“”,
        defaults:new {controller =“Home”,action =“Index”}
在Orchard Core中创建模块应用程序¶
在Orchard Core中,一个模块是指封装在自己的项目中的功能区块。这些模块可以通过应用程序进行加载和卸载,可以根据需要启用或禁用。在本教程中,您将学习如何创建一个用于 Orchard Core 的 ASP.NET Core MVC 模块应用程序。

先决条件

.NET Core SDK 3.1或更高版本。
Visual Studio 2019或更高版本。
步骤

1. 创建一个新的网站

打开 Visual Studio 并选择 创建新项目。
在左侧选择 ASP.NET Core Web 应用程序。
在右侧选择 Web 应用程序。
输入项目名称和位置,然后单击 创建。

2. 安装 Orchard Core

打开命令提示符。
执行以下命令:
dotnet new -i OrchardCore.ProjectTemplates::1.0.0-*
创建一个名为 OrchardCore.Web 的新文件夹,并在其中运行以下命令:
dotnet new ocmvc -n YourModuleName
完成后,切换到新的文件夹,然后运行以下命令启动应用程序:
dotnet run
导航到 https://localhost:5001 来查看网站。

3. 创建一个模块

在新文件夹的根目录中,创建一个名为 Modules 的文件夹。
在 Modules 文件夹中创建一个名为 YourModuleName 的新文件夹。
在 YourModuleName 文件夹中,创建一个新的 .NET Core 类库 项目。
选择 netstandard2.0 作为目标框架。
将生成的默认文件删除。
打开 YourModuleName.csproj 文件并添加以下 Orchard Core引用:

  
  

右键单击项目并选择 添加 -> 新项目文件夹。该目标文件夹应该是你的 Views 因此输入 Views 作为目录名称。 添加 Home 目录和 Index.cshtml 视图文件。 打开 Startup.cs 文件并将以下代码添加到 ConfigureServices* 方法的末尾:

services.AddMvc().AddOrchardCoreMvc();
这将向依赖注入容器注册OrchardCore MVC的必要服务。 现在,要将新的模块添加到 Orchard Core应用程序中,请打开 OrchardCore.Web.csproj* 文件并添加以下行:


  

重启应用程序,并导航到 https://localhost:5001 将显示 Orchard Core的默认欢迎页面。

4. 更改默认欢迎页面

打开 YourModuleName 项目的 Controllers 文件夹。
创建一个名为 HomeController.cs 的新文件并输入以下内容:
using Microsoft.AspNetCore.Mvc;

namespace YourModuleName.Controllers
{
  public class HomeController : Controller
  {
    public ActionResult Index()
    {
      return View();
    }
  }
}
这将创建一个名为 Index 的动作,该动作在呈现视图时显示。 * 将以下代码复制到您的视图文件(Views/Home/Index.cshtml)中以显示 Hello World:

Hello World

重启应用程序并导航到 https://localhost:5001 将显示 Hello World 消息。 总结 您刚刚创建了一个使用Orchard Core的模块化ASP.NET Core MVC web应用程序。 它包括一个提供布局的Web应用程序和一个响应主页请求的自定义模块。 教程 https://www.youtube.com/watch?v=LoPlECp31Oo
20
6

OrchardCore入门指南

0
归档:2023年6月分类:C#和.NET

指南
无论你在构建什么,这些指南旨在让你尽快使用最新的Orchard Core项目版本和Orchard团队推荐的技术,进入工作状态。

入门指南
这些指南旨在在15-30分钟内完成,提供快速的、实践操作的指令,用于使用Orchard Core构建任何开发任务的“Hello World”。在大多数情况下,唯一的先决条件是一个.NET SDK和一个文本编辑器。

创建一个模块化的ASP.NET Core应用程序
在启动时运行代码
自定义编码设置
Orchard Core CMS指南
这些指南专门针对Orchard Core CMS:

创建Orchard Core CMS网站
向管理导航添加菜单项
安装本地化文件
如何使用资产转码器/绑定器/缩小器管道- 集成 Facebook 插件
实现全文检索
将 AzureAD 集成为外部提供程序
教程
这些教程旨在在2-3小时内完成,提供更深入、上下文探讨企业应用程序开发主题,并让您准备实现现实世界的解决方案。

使用 Razor Pages 构建解耦的网站
构建一个来自 Web 模板的网站(待定)
实施自助式 SaaS 解决方案(待定)

07
6

基于ASP.NET Core的整洁架构

0
归档:2023年6月分类:LAMP开发

干净体系结构将业务逻辑和应用程序模型置于应用程序的中心。 而不是让业务逻辑依赖于数据访问或其他基础设施,此依赖关系被倒置:基础结构和实现细节依赖于应用程序内核。 此功能是通过在应用程序核心中定义抽象或接口来实现的,然后通过基础设施层中定义的类型实现。 将此体系结构可视化的常用方法是使用一系列同心圆,类似于洋葱。

遵循依赖倒置原则以及域驱动设计原则 (DDD) 的应用程序倾向于达到类似的体系结构。 多年来,这种体系结构有多种名称。 最初的名称之一是六边形体系结构,然后是端口 - 适配器。 最近,它被称为洋葱体系结构或干净体系结构。 此电子书中将后一种名称“干净体系结构”用作此体系结构的名称。

在此关系图中,依赖关系流向最里面的圆。 “应用程序内核”因其位于此关系图的核心位置而得名。 从关系图上可见,该应用程序内核在其他应用程序层上没有任何依赖项。 应用程序的实体和接口位于正中心。 在外圈但仍在应用程序核心中的是域服务,它通常实现内圈中定义的接口。 在应用程序内核外面,UI 和基础结构层均依赖于应用程序内核,但不一定彼此依赖。

08
5

最近创建基于firstcode的开发模式,整理了一下如何使用Entity Framework Core 创建DbContext。

1、第一种重写DbContext的OnConfiguring方法,每次生成一个DbContext的方法的时候就会重新来这个方法这里读一下配置。

这种情况下,首先尝试通过调用 Program.CreateHostBuilder()、调用 Build(),然后访问 Services 属性来获取服务提供程序。

public class Program
{
    public static void Main(string[] args)
        => CreateHostBuilder(args).Build().Run();

    // EF Core uses this method at design time to access the DbContext
    public static IHostBuilder CreateHostBuilder(string[] args)
        => Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(
                webBuilder => webBuilder.UseStartup<Startup>());
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
        => services.AddDbContext<ApplicationDbContext>();

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    }
}

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

2、使用不带参数的构造函数

这种情况就是在已经注入的情况下,就不再需要自己去New一个对象了,直接在使用的地方把DbContext 当成构造参数传进来

如果无法从应用程序服务提供程序获得 DbContext,则这些工具将查找项目中的派生 DbContext 类型。 然后,它们尝试使用不带参数的构造函数创建实例。 如果 DbContext 是使用 OnConfiguring 方法配置的,则这可以是默认构造函数。

从设计时工厂还可以通过实现 Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory 接口来告知工具如何创建 DbContext:如果在与派生的 DbContext 相同的项目中或在应用程序的启动项目中找到实现此接口的类,则这些工具会绕过创建 DbContext 的其他方式,转而使用设计时工厂。

public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
    public BloggingContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
        optionsBuilder.UseSqlite("Data Source=blog.db");

        return new BloggingContext(optionsBuilder.Options);
    }
}
19
4

为什么叫里氏替换原则?

里氏替换原则在SOLID这五个设计原则中是比较特殊的存在:如果违反了里氏替换原则,不只是降低软件设计的优雅性,很可能会导致Bug。

里氏替换原则译自Liskov substitution principle。Liskov是一位计算机科学家,也就是Barbara Liskov,麻省理工学院教授,也是美国第一个计算机科学女博士,师从图灵奖得主John McCarthy教授,人工智能概念的提出者。

Robert Martin在《敏捷软件开发:原则、模式与实践》一书中对原论文的解读:子类型(subtype)必须能够替换掉他们的基类型(base type)。这个是更简明的一种表述。

违背 LSP 原则的一个简单示例

一个非常明显地违背 LSP原则的示例就是使用 RTTI(Run Time Type Identification)来根据对象类型选择函数执行。

void DrawShape(const Shape& s)
{
    if (typeid(s) == typeid(Square))
        DrawSquare(static_cast<Square&>(s));
    else if (typeid(s) == typeid(Circle))
        DrawCircle(static_cast<Circle&>(s));
}

正方形和长方形,违背原则的微妙之处

很多情况下对 LSP 原则的违背方式都十分微妙。设想在一个应用程序中使用了 Rectangle 类,描述如下:

public class Rectangle
  {
    private double _width;
    private double _height;

    public void SetWidth(double w) { _width = w; }
    public void SetHeight(double w) { _height = w; }
    public double GetWidth() { return _width; }
    public double GetHeight() { return _height; }
  }

违反里氏替换原则的危害

当我们违反了这一原则会带来有一些危害:反直觉。期望所有子类行为是一致的,但如果不一致可能需要文档记录,或者在代码跑失败后涨此知识;不可读。如果子类行为不一致,可能需要不同的逻辑分支来适配不同的行为,徒增代码复杂度;不可用。可能出错的地方终将会出错。

如何避免违反里氏替换原则

谈到如何避免,当然要基于里氏替换原则的定义,与期望行为一致的替换。

从行为出发来设计。在做抽象或设计时,不只是要从模型概念出发,还要从行为出发,比如一个经典的例子,正方形和长方形,从现实的概念中正方形是一个长方形,但是在计算其面积的行为上是不一致的。

基于契约设计。这个契约即是基类方法签名、功能描述、参数类型、返回值等。在派生类的实现时,时刻保持派生类与基类的契约不被破坏。

开放封闭原则(Open Closed Principle)是许多面向对象设计启示思想的核心。符合该原则的应用程序在可维护性、可重用性和鲁棒性等方面会表现的更好。里氏替换原则(Liskov Substitution Principle)则是实现 OCP 原则的重要方式。只有当衍生类能够完全替代它们的基类时,使用基类的函数才能够被安全的重用,然后衍生类也可以被放心的修改了。

19
4

Author: Stephen Watts

SOLID is a popular set of design principles that are used in object-oriented software development. SOLID is an acronym that stands for five key design principles: single responsibility principle, open-closed principle, Liskov substitution principle, interface segregation principle, and dependency inversion principle. All five are commonly used by software engineers and provide some important benefits for developers.

The SOLID principles were developed by Robert C. Martin in a 2000 essay, “Design Principles and Design Patterns,” although the acronym was coined later by Michael Feathers. In his essay, Martin acknowledged that successful software will change and develop. As it changes, it becomes increasingly complex. Without good design principles, Martin warns that software becomes rigid, fragile, immobile, and viscous. The SOLID principles were developed to combat these problematic design patterns.

The broad goal of the SOLID principles is to reduce dependencies so that engineers change one area of software without impacting others. Additionally, they’re intended to make designs easier to understand, maintain, and extend. Ultimately, using these design principles makes it easier for software engineers to avoid issues and to build adaptive, effective, and agile software.

While the principles come with many benefits, following the principles generally leads to writing longer and more complex code. This means that it can extend the design process and make development a little more difficult. However, this extra time and effort is well worth it because it makes software so much easier to maintain, test, and extend.

Following these principles is not a cure-all and won’t avoid design issues. That said, the principles have become popular because when followed correctly, they lead to better code for readability, maintainability, design patterns, and testability. In the current environment, all developers should know and utilize these principles.

Single Responsibility Principle

Robert Martin summarizes this principle well by mandating that, “a class should have one, and only one, reason to change.” Following this principle means that each class only does one thing and every class or module only has responsibility for one part of the software’s functionality. More simply, each class should solve only one problem.

Single responsibility principle is a relatively basic principle that most developers are already utilizing to build code. It can be applied to classes, software components, and microservices.

Utilizing this principle makes code easier to test and maintain, it makes software easier to implement, and it helps to avoid unanticipated side-effects of future changes.

To ensure that you’re following this principle in development, consider using an automated check on build to limit the scope of classes. This check is not a foolproof way to make sure that you’re following single responsibility principle, but it can be a good way to make sure that classes are not violating this principle.

Open-Closed Principle

The idea of open-closed principle is that existing, well-tested classes will need to be modified when something needs to be added. Yet, changing classes can lead to problems or bugs. Instead of changing the class, you simply want to extend it. With that goal in mind, Martin summarizes this principle, “You should be able to extend a class’s behavior without modifying it.”

Following this principle is essential for writing code that is easy to maintain and revise. Your class complies with this principle if it is:

Open for extension, meaning that the class’s behavior can be extended; and
Closed for modification
, meaning that the source code is set and cannot be changed.

At first glance, these two criteria seem to be inherently contradictory, but when you become more comfortable with it, you’ll see that it’s not as complicated as it seems. The way to comply with these principles and to make sure that your class is easily extendable without having to modify the code is through the use of abstractions. Using inheritance or interfaces that allow polymorphic substitutions is a common way to comply with this principle. Regardless of the method used, it’s important to follow this principle in order to write code that is maintainable and revisable.

Liskov Substitution Principle

Of the five SOLID principles, the Liskov Substitution Principle is perhaps the most difficult one to understand. Broadly, this principle simply requires that every derived class should be substitutable for its parent class. The principle is named for Barbara Liskov, who introduced this concept of behavioral subtyping in 1987. Liskov herself explains the principle by saying:

What is wanted here is something like the following substitution property: if for each object O1 of type S there is an object O2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when O1 is substituted for O2 then S is a subtype of T.

While this can be a difficult principle to internalize, in a lot of ways it’s simply an extension of open-closed principle, as it’s a way of ensuring that derived classes extend the base class without changing behavior.

Following this principle helps to avoid unexpected consequences of changes and avoids having to open a closed class in order to make changes. It leads to easy extensions of software, and, while it might slow down the development process, following this principle during development can avoid lots of issues during updates and extensions.

Interface Segregation Principle

The general idea of interface segregation principle is that it’s better to have a lot of smaller interfaces than a few bigger ones. Martin explains this principle by advising, “Make fine grained interfaces that are client-specific. Clients should not be forced to implement interfaces they do not use.”

For software engineers, this means that you don’t want to just start with an existing interface and add new methods. Instead, start by building a new interface and then let your class implement multiple interfaces as needed. Smaller interfaces mean that developers should have a preference for composition over inheritance and for decoupling over coupling. According to this principle, engineers should work to have many client-specific interfaces, avoiding the temptation of having one big, general-purpose interface.

Dependency Inversion Principle

This principle offers a way to decouple software modules. Simply put, dependency inversion principle means that developers should “depend on abstractions, not on concretions.” Martin further explains this principle by asserting that, “high level modules should not depend upon low level modules. Both should depend on abstractions.” Further, “abstractions should not depend on details. Details should depend upon abstractions.”

One popular way to comply with this principle is through the use of a dependency inversion pattern, although this method is not the only way to do so. Whatever method you choose to utilize, finding a way to utilize this principle will make your code more flexible, agile, and reusable.

Conclusion

Implementing SOLID design principles during development will lead to systems that are more maintainable, scalable, testable, and reusable. In the current environment, these principles are used globally by engineers. As a result, to create good code and to use design principles that are competitive while meeting industry standards, it’s essential to utilize these principles.

While implementing these principles can feel overwhelming at first, regularly working with them and understanding the differences between code that complies with the principles and code that does not will help to make good design processes easier and more efficient.

公告栏

欢迎大家来到我的博客,我是dodoro,希望我的博客能给你带来帮助。