Gongli Products: Designing and Developing a .NET Framework from Scratch - Framework Source Code and Tutorial E-Book

2024年3月21日 122点热度 8人点赞 2条评论
内容目录

Why Write This Tutorial

After graduation, readers have written a large number of articles and open-source projects. It is through the persistence of learning while outputting that the author has ultimately evolved from a newbie to an experienced developer.

In the growth of a programmer, we encounter questions about mindset, how to learn, and how to progress. For example, what skills are necessary to grow into an architect? Technical ability, which is the core foundation—what level of technical skill do we need to achieve? Can we code a framework from scratch? Kick assembly code? Punch microservice architectures?

There are often exchanges with friends in the tech community and readers, many of whom are unsure about how to learn. I am also in the same boat; I don't know how to learn. I'm dabbling in various things and haven’t yet pinpointed what I want to specialize in, such as learning AI recently. Young people often feel confused about their future paths. Getting rich overnight, perhaps.

Returning to the point, I am writing this tutorial because I enjoy settling technology, thoughts, and knowledge into code and documentation. Upholding the spirit of open-source, I want to share these insights with everyone, which is why I have spent a lot of time writing this open-source project and the accompanying technical documentation. I often write a series of longer articles and e-books, as a lengthy series of tutorials can yield complete and systematic knowledge, whereas shorter, fragmented pieces do not facilitate the learning and consolidation of knowledge for readers. Writing long articles is tiring; therefore, readers who appreciate this series of tutorials are encouraged to star the repository.

Please retain the source.

About Designing a .NET Development Framework from Scratch

Author: whuanle

Repository Address: https://github.com/whuanle/maomi

Documentation Address: https://maomi.whuanle.cn

Author's Blog:

https://www.whuanle.cn

https://www.cnblogs.com/whuanle

Project and Tutorial Introduction

The Maomi framework is a simple and clean development framework. In addition to the features provided by the framework itself, Maomi also serves as an easily readable open-source project that can offer developers ideas and code for designing frameworks.

The Maomi framework currently includes four modules: modularity and automatic service registration, multi-language support, event bus, and Web. The entire solution encompasses 62 projects, including demonstration code for writing parts of a framework, such as how to create diagnostic tools similar to dotnet-dump, how to customize a logging framework, how to write a logging framework, how to use EMIT for AOP, how to write a code compiler using Roslyn, how to design modularity similar to ABP, among others, including unit testing.

This tutorial is primarily divided into four sections:

  • Fundamental Knowledge: This section introduces the basics of developing this type of framework, along with some abstractions and principles, but will not delve deeply into source code and details to avoid reading difficulties.

  • Design and Abstraction: When you want to develop a framework, do not rush into coding—think about what needs to be done, and then how to abstract the interfaces.

  • Starting Coding: Begin implementing your code and enjoy the joy of coding.

  • Extending Techniques: Unit testing, performance testing, creating NuGet packages, creating toolkits, etc.

This tutorial has over sixty projects, and each article will generally guide readers on how to develop the corresponding framework. Each chapter will contain abundant examples, all of which can be found in the repository. This avoids issues where readers are unsure how to create projects and run examples while reading the article's source code; readers can directly download the repository source examples to debug and understand the code at their own pace.

If you want to write your own development framework from scratch, this tutorial is perfect for you. It encompasses a wide range of common knowledge and implementation principles in .NET development. Each chapter is a part of the whole; you can choose to read only the portions that interest you. I hope this can help you learn and understand how to write various frameworks.

Here is the table of contents for the tutorial:

Explains the usage and basic principles of Maomi.Core.

Discusses how Maomi.Core is designed and implemented. When developing a framework, how to proceed from design to abstraction, coding, and final implementation. The principles of modularity and automatic service registration, how to develop from scratch, and finally create a NuGet package for distribution on nuget.org for everyone to use.

Introduces some methods for fault diagnosis and the logging abstraction interface in .NET.

How to design and develop your own logging framework.

Highly recommended reading, this article introduces the configuration and usage of Serilog, discussing lifecycle scope, attributes, log templates, and more, to produce very efficient logs during program execution for easier problem diagnosis. Many developers use logging very superficially, not knowing how to make good use of logging tools; this article can help.

Introduces some methods and principles for .NET diagnostics, then discusses how to develop tools like dotnet-trace, dotnet-counters, dotnet-dump, etc. Yes, we can also write such tools!

Briefly outlines the principles and usage of IConfiguration and Options, custom configuration providers, and using SignalR to implement a configuration center.

This chapter is quite rich in content, explaining various features of serialization and deserialization in .NET, custom configurations, usage tips, how to create custom enum converters, string converters, date format converters, etc., along with detailed implementation details. Finally, it introduces Utf8JsonReader and how to write performance testing code, demonstrating JSON parsing using Utf8JsonReader, ensuring readers grasp the principles; in subsequent chapters, we will also introduce how to use Utf8JsonReader to implement basic capabilities like multi-language support.

This chapter is rich in content, starting with the usage of the Maomi.I18n framework, how ASP.NET Core handles multi-language requests, understanding what is needed in the i18n framework, and then beginning to design abstractions and write implementation code. After writing the framework, unit tests must also be created, and the author introduces how to write these tests. Next, it covers implementing multi-language support based on Redis, and finally how to package language files in NuGet packages for sharing with others.

This chapter details the usage of HttpClient, covering not just the basics but also aspects like request parameters, credentials, and exception handling. It then elaborates on IHttpClientFactory, covering techniques such as request interception, request strategies (retry, timeout), etc. It introduces the usage of the Refit tool and how to quickly generate HTTP request code in business development, simplifying the development process. Finally, it covers how to write a CLI tool similar to curl, mastering the skills and techniques for writing command line tools with .NET.

The event bus is one of the most commonly used decoupled communication mechanisms in DDD development. In this chapter, we will guide readers through designing an event bus framework from scratch, covering the entire process from abstract design to implementation, explaining the principles and implementation code at each stage. Reflection, delegates, expression trees, and other technologies will be used in the event bus. If you are unfamiliar with expression trees, that’s alright—just follow along with the tutorial; there’s no need to get bogged down in technical details, just grasp the overall design and development approach.

This chapter is quite rich, discussing EMIT technology and how to develop an AOP framework, the two methods of using expression trees, writing an object mapping framework, a simple ORM framework, introducing Roslyn technology, code generation and compilation, and Natasha framework's simple usage. Lastly, it covers Source Generators (SG technology) for code generation.

Due to space limitations, this chapter won't delve into all various technologies. If readers want to build a solid foundation, they may refer to the author's other e-books:

Basics of Reflection: https://reflect.whuanle.cn/

Basics of Expression Trees: https://ex.whuanle.cn

This chapter discusses common aspects of customizing web frameworks for enterprise internal needs. It covers common items in development and how to customize them, such as how model validation is implemented, how to create a custom model validator, using i18n in model validators, the usage and techniques of various filters, custom development of filters (Action filters, Resource filters, Exception filters), and Swagger customization (model class property type conversion, API grouping, API versioning, microservice routing suffixes), etc.

A detailed introduction to the usage of Maomi.Mapper.

痴者工良

高级程序员劝退师

文章评论