This article describes how to update an existing CustomerReviews sample module from VC Platform version 2.x to 3.0.
!!! note A sample module source code can be found here: https://github.com/VirtoCommerce/vc-samples/tree/release/3.0.0/CustomerReviews.
Open the folder of selected v2 module in file manager (e.g., Windows Explorer).
If exists, delete packages folder.
Delete Properties folder from each of the projects' folder.
Convert the projects from ASP.NET to ASP.NET Core:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Remove="dist\**" />
<EmbeddedResource Remove="dist\**" />
<None Remove="dist\**" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
Create src and tests subfolders in module's root folder (Windows Explorer)
Move CustomerReviews.Core, CustomerReviews.Data, CustomerReviews.Web projects to src
Move CustomerReviews.Test project to tests
If exists, move module.ignore from CustomerReviews**.Web** project up to the same folder as CustomerReviews**.sln** is.
Open CustomerReviews.sln solution in Visual Studio
Remove all projects from the solution
Add src and tests Solution Folders
Add the existing CustomerReviews.Core, CustomerReviews.Data, CustomerReviews.Web projects to src folder
Add the existing CustomerReviews.Test project to tests folder
Remove all files related to .NET Framework 4.x in every project:
Add references to projects:
References to NuGet packages:
Add other NuGet dependency packages, if any exists in module.manifest.
If missing, add class ModuleConstants.cs for module constants:
Follow the structure as defined in ModuleConstants.cs in CustomerModule.
Update ICustomerReviewService.cs:
Task<>
Async
If there is a search service defined:
SearchCriteriaBase
;GenericSearchResult<CustomerReview>
.public interface ICustomerReviewSearchService
{
Task<CustomerReviewSearchResult> SearchCustomerReviewsAsync(CustomerReviewSearchCriteria criteria);
}
If any model-related changing/changed events were defined in Events folder, ensure that each of them derive from the base GenericChangedEntryEvent
class.
If any custom Notifications were added to Notifications folder:
Repositories folder
Create CustomerReviewsDbContext.cs
DbContextWithTriggers
Note: 1 constructor is public and another is protected.
modelBuilder.Entity<CustomerReviewEntity>().ToTable("CustomerReview").HasKey(x => x.Id);
modelBuilder.Entity<CustomerReviewEntity>().Property(x => x.Id).HasMaxLength(128).ValueGeneratedOnAdd();
Note: a factory for creating derived Microsoft.EntityFrameworkCore.DbContext instances.
Create DesignTimeDbContextFactory.cs
IDesignTimeDbContextFactory<CustomerReviewsDbContext>
Update ICustomerReviewsRepository.cs
Task<>
Async
Update CustomerReviewsRepository.cs
DbContextRepositoryBase<CustomerReviewsDbContext>
or if the module is an extension then derive from derived module's repository.public CustomerReviewRepository(CustomerReviewsDbContext dbContext) : base(dbContext)
{
}
public IQueryable<CustomerReviewEntity> CustomerReviews => DbContext.Set<CustomerReviewEntity>();
Caching folder
CancellableCacheRegion<T>
class:public class CustomerReviewCacheRegion : CancellableCacheRegion<CustomerReviewCacheRegion>
{
}
Services folder
Task<CustomerReviewSearchResult>
in CustomerReviewSearchService servicecheck this example for more details VirtoCommerce.CustomerModule.Data.Services.
Migrations folder
Create InitialCustomerReviews migration
Add-Migration InitialCustomerReviews -Verbose
Create Migration for backward compatibility with v2.x
migrationBuilder.Sql(@"IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '__MigrationHistory'))
IF (EXISTS (SELECT * FROM __MigrationHistory WHERE ContextKey = 'CustomerReviews.Data.Migrations.Configuration'))
BEGIN
INSERT INTO [dbo].[__EFMigrationsHistory] ([MigrationId],[ProductVersion]) VALUES ('20191129134041_InitialCustomerReviews', '2.2.3-servicing-35854')
END");
ContextKey
value is the V2 migration Configuration name, including namespace. Retrieve the namespace from your notes, as you put it there in the previous section. Typically, the value is "{ModuleId}.Data.Migrations.Configuration".MigrationId
has to be the name of your new migration, added in previous step. ('20191129134041_InitialCustomerReviews' in our case). Check 20000000000000_UpdateCoreV2.cs migration as another example.ProductVersion
should be taken from 20000000000000_UpdateCustomerReviewsV2.Designer line 19:.HasAnnotation("ProductVersion", "2.2.3-servicing-35854")
If separated databases are used by the solution, follow the steps in Prepare separated databases for VC v3.
It's useful sometimes to apply migrations without starting the platform. There is an utility to extract and apply migrations (integrated to vc-build tool). Take a reference to Grab migrator utility.
Execute "Reload Project" on CustomerReviews.Web project in Solution Explorer (as it was unloaded earlier).
Changes in module.manifest
<version>3.0.0</version>
<version-tag></version-tag>
<platformVersion>3.0.0</platformVersion>
<dependencies>
<dependency id="VirtoCommerce.Core" version="3.1.0" />
</dependencies>
Add localizations for permissions/settings to Localizations/en.CustomerReviews.json file as this.
Sample resulting keys: "permissions.customerReview:read"
, "settings.CustomerReviews.CustomerReviewsEnabled.title"
.
Remove permissions, settings definitions sections from the manifest file.
Changes in Module.cs
var settingsRegistrar = appBuilder.ApplicationServices.GetRequiredService<ISettingsRegistrar>();
settingsRegistrar.RegisterSettings(ModuleConstants.Settings.AllSettings, ModuleInfo.Id);
var permissionsProvider = appBuilder.ApplicationServices.GetRequiredService<IPermissionsRegistrar>();
permissionsProvider.RegisterPermissions(ModuleConstants.Security.Permissions.AllPermissions.Select(x => new Permission() { GroupName = "CustomerReview", Name = x }).ToArray());
using (var serviceScope = appBuilder.ApplicationServices.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<CustomerReviewsDbContext>();
dbContext.Database.MigrateIfNotApplied(MigrationName.GetUpdateV2MigrationNameByOwnerName(ModuleInfo.Id, <<your company prefix in moduleId>>));
dbContext.Database.EnsureCreated();
dbContext.Database.Migrate();
}
!!! note The MigrateIfNotApplied extension method is needed for the database backward compatibility with version 2.x. This extension enables to skip generating the initial migration, as there are changes (tables, indexes) in the database already.
Changes to all API Controllers in Controllers/Api folder:
If the endpoint should have a restricted access, an Authorize attribute with the required permission should be added. Use the ModuleConstants class, which was previously defined in CustomerReviews.Core project.
async Task<>
), if needed[FromBody]
attribute for all endpoints. The attribute for Delete endpoint should be [FromQuery]
.
E.g., SearchCustomerReviews method converted to ASP.NET Core MVC:[HttpPost]
[Route("search")]
[Authorize(ModuleConstants.Security.Permissions.Read)]
public async Task<ActionResult<CustomerReviewSearchResult>> SearchCustomerReviews([FromBody]CustomerReviewSearchCriteria criteria)
Read Controller action return types in ASP.NET Core web API for more info.
If there are any JavaScript or stylesheet files in the project:
namespace: 'CustomerReviews'
Open Command prompt and navigate to CustomerReviews.Web folder. Run:
npm install
!!! note
fix any css errors, if the previous command would fail. For CustomerReviews sample also change line 12 in webpack.config.js to:
js ...glob.sync('./Content/css/*.css', { nosort: true })
Add dist/
line to .gitignore
file;
Add node_modules/
line to .gitignore
file;
Install WebPack Task Runner extension to Visual Studio (restart required);
Build the scripts:
!!! note The resulting file(s) (app.js, style.css) were generated to *.Web/dist folder.
ItemGroup
to project file: <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
[Trait("Category", "IntegrationTest")]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。