Location>code7788 >text

Abp source code analysis of the virtual file system

Popularity:111 ℃/2024-11-12 16:28:31

preamble

is an important component of the ABP (Boilerplate) framework, which provides a way of abstracting the file system so that applications can easily access and manage file resources, whether they come from a physical file system, embedded resources, or remote storage.

pass (a bill or inspection etc)The developer can use a unified interface to work with files and directories without having to care about where they are actually stored. This makes applications more flexible and allows them to easily switch between different file storage methods without having to modify a lot of code.

New mvc project

Cite the following nuget package

newly built

using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using BookCategory;

namespace BookApp
{
    [DependsOn(
        typeof(AbpAutofacModule),
        typeof(AbpLocalizationModule),
        typeof(AbpVirtualFileSystemModule),
        typeof(AbpAspNetCoreMvcModule),
        typeof(BookCategoryModule)
    )]
    public class BookAppWebModule: AbpModule
    {
        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            var hostingEnvironment = ();
            var configuration = ();

            <AbpMvcDataAnnotationsLocalizationOptions>(options =>
            {
                (
                    typeof(BookStoreResource)
                );
            });
        }
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var hostingEnvironment = ();

            ConfigureVirtualFileSystem(hostingEnvironment);

            Configure<AbpLocalizationOptions>(options =>
            {
                (new LanguageInfo("ar", "ar", "العربية"));
                (new LanguageInfo("cs", "cs", "Čeština"));
                (new LanguageInfo("en", "en", "English"));
                (new LanguageInfo("en-GB", "en-GB", "English (UK)"));
                (new LanguageInfo("hu", "hu", "Magyar"));
                (new LanguageInfo("fi", "fi", "Finnish"));
                (new LanguageInfo("fr", "fr", "Français"));
                (new LanguageInfo("hi", "hi", "Hindi"));
                (new LanguageInfo("it", "it", "Italiano"));
                (new LanguageInfo("pt-BR", "pt-BR", "Português"));
                (new LanguageInfo("ru", "ru", "Русский"));
                (new LanguageInfo("sk", "sk", "Slovak"));
                (new LanguageInfo("tr", "tr", "Türkçe"));
                (new LanguageInfo("zh-Hans", "zh-Hans", "simplified Chinese"));
                (new LanguageInfo("zh-Hant", "zh-Hant", "Traditional Chinese"));
                (new LanguageInfo("de-DE", "de-DE", "Deutsch"));
                (new LanguageInfo("es", "es", "Español"));

                
                    .Add<BookStoreResource>("en")
                    .AddVirtualJson("/Localization/BookStore");

                 = typeof(BookStoreResource);
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = ();
            var env = ();

            ();

            if (())
            {
                ();
            }

            ();
            ();

            ();
        }

        private void ConfigureVirtualFileSystem(IWebHostEnvironment hostingEnvironment)
        {
            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                <BookAppWebModule>();

                if (())
                {
                    <BookAppWebModule>();
                    <BookCategoryModule>((, ("..{0}BookCategory", )));
                }
            });
        }
    }
}

modifications

using BookApp;
using ;

var builder = (args);


    .AddAppSettingsSecretsJson()
    .UseAutofac();

await <BookAppWebModule>();

var app = ();

await ();

(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

await ();

New Resource File

New BookCategory library project

newly built

using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace BookCategory
{
    [DependsOn(
        typeof(AbpVirtualFileSystemModule),
        typeof(AbpAspNetCoreMvcModule)
    )]
    public class BookCategoryModule: AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var hostingEnvironment = ();
            var configuration = ();

            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                <BookCategoryModule>();//Adding an assembly to a virtual file system

                if (())
                {

                }
            });

            Configure<AbpLocalizationOptions>(options =>
            {
                
                    .Add<BookCategoryResource>("en")
                    .AddVirtualJson("/Localization/BookCategory"); //It is necessary to add,Otherwise, the localization will not find the appropriatejsonfile
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {

        }
    }
}

newly built


using ;

namespace ;

[LocalizationResourceName("BookCategory")]
public class BookCategoryResource
{

}

New Resource File

Modify the Privacy method in the mvc project.

        public IActionResult Privacy()
        {
            var resourcePath = "/Localization/BookCategory/";
            var fileInfo = _fileProvider.GetFileInfo(resourcePath);

            if ()
            {
                using (var stream = ())
                using (var reader = new StreamReader(stream))
                {
                    var content = ();
                    return Content(content);
                }
            }

            return Content("Resource not found");
        }

At this point we can access the resource files in the BookCategory class library

Modifying views in mvc projects

@using 
@using 
@using 
@using 

@inject IHtmlLocalizer<BookStoreResource> HtmlLocalizer
@inject IStringLocalizer<BookCategoryResource> StringLocalizer

@{
    ViewData["Title"] = "Home Page";
}

<div>string: @StringLocalizer["AppName"]</div>

<div>html: @HtmlLocalizer["AppName"]</div>

IStringLocalizer StringLocalizer reads the resource files in the BookCategory library.

author

Wu Xiaoyang (cell phone: 13736969112 wechat the same number)