image

[23 Design Modes] Builder Mode (4)

Preface In a software system, sometimes you are faced with the creation of “a complex object”, which is usually composed of sub-objects of each part using a certain algorithm; due to changes in requirements, each part of this complex object often faces severe challenges changes, but the algorithms that combine them are relatively stable. How to deal with this change? How to provide an “encapsulation mechanism” to isolate changes in “various parts of complex objects”, so as to keep the “stable construction algorithm” in the system from changing as requirements change? That is the Builder Pattern shared today, also known as Builder Pattern, and its English name is Builder Pattern. builder mode definition In real life, we often encounter some complex objects, such as cars, computers and mobile phones. They are a complex object, mainly assembled from various parts, and their assembly process is fixed. Take a car as an example, the assembly line is fixed and unchanged, and it needs to assemble the chassis, wheels, doors, lights, engine, lights and exhaust pipes, etc. together. However, due to changes in requirements, this car complex object is aimed at different brands, and the various parts often face drastic changes, but the…

.NET Core WebAPI Basic File Upload

Yesterday I shared an article on how to receive parameters in WebApi Portal, and then a newcomer friend asked, what about file uploads, how to do this, so I wrote a small demo and shared it with everyone. Generally speaking, when uploading files, do we use POST, or the old rules first upload the code: txt file This is the txt file that needs to be uploaded. Specify file parameter name Code [HttpPost(“UploaFile “)] public async Task<Dictionary<string, string>> UploadFileAsync(IEnumerable files) { var fileStream = files.FirstOrDefault()?.OpenReadStream(); var fileContent = new StringBuilder(); if (fileStream != null) { using var reader = new StreamReader(fileStream!); while (reader. Peek() >= 0) { fileContent.AppendLine(await reader. ReadLineAsync()); } } var result = new Dictionary<string, string() { [“fileContent”] = fileContent. ToString() }; return result; } Call example There is one thing to pay attention to here, that is, the parameter name of the uploaded file needs to be consistent with the input parameter name of the interface, that is, files. The type we use is IEnumerable, mainly IFormFile, because we upload There may be more than one file, so a collection is used here. If you don’t like IEnumerable, you can replace it with List; Do not specify…

image

Shallow copy and deep copy in C#

Preface As we all know, there are two types of variables in C#: value type and reference type. For the value type, copy is equivalent to copying the whole disk, and the real copy is a deep copy; for the reference type, the general copy is just a shallow copy >, just copy to the address of the reference object, which is equivalent to passing a reference pointer by value, The new object still points to the object in the original memory through the address reference. What is shallow copy and deep copy ​ Shallow copy: Only copy the basic type of the object, the object type, and still belong to the original reference, also known as shadow clone; ​ Deep copy: not only copy the basic class of the object, but also copy the object in the original object, which is completely generated by the new object, also known as deep clone; Implementation of Shallow Copy In C#, System.Object is the base class of all class types, structure types, enumeration types and delegate types. It can be said that it is the basis of type inheritance. System.Object includes a MemberwiseClone member method for creating a copy of the current…

image

[23 Design Patterns] Prototype Pattern (5)

Preface In a software system, when the process of creating an instance of a class is expensive or complicated, and we need to create multiple instances of such a class, if we use the new operator to create such a class instance, this will Increase the complexity of creating classes and the coupling between the creation process and client code complexity. If the factory model is used to create such instance objects, with the continuous increase of product classes, the number of subclasses will continue to increase, which will also lead to the increase of corresponding factory classes, and the maintenance code dimension will increase, because there are two types of products and factories. This dimension increases the complexity of the system, so it is not appropriate to use the factory pattern to encapsulate the class creation process here. Since each class instance is the same, the same refers to the same type, but the state parameters of each instance will be different, if the state value is also the same, it is meaningless, there is only one such object. When we need multiple instances of the same class, we can complete the creation by copying the original object. This…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索