当前位置:网站首页>Discussion on the development practice of aspnetcore, a cross platform framework

Discussion on the development practice of aspnetcore, a cross platform framework

2020-11-06 01:28:33 itread01

## Preface ![](https://upload-images.jianshu.io/upload_images/8869373-e1529b0c4e91d0e7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Recently, learning and doing , It's preliminary AspNetCore Development of , Yes MVC Get familiar with this set , Because I haven't done any independent projects before , It's all about cooperating with others , So the front and back ends separate , I just do WebApi, And it's a very small project ( Class level ), The most commonly used production environment should be Python Of Django frame , This time I'll take one that I did before Django For projects AspNetCore Rewrite it , Efficiency has improved a lot ,C# It's really not a cover ,AspNetCore This framework is also very effective , On the list (WebApi), Although MVC I don't have a name , But the efficiency still drops Django quite a lot ,Django Every time you go online, you do various performance optimizations , Terror , But it doesn't work too well , Later, the company feels the workload is not saturated ( flee ) ok, Since it's a random talk , It doesn't have to be very clear , I'd like to list a few aspects to compare with other frameworks (Django and Spring And so on ) Talk about it ~ Since I am a beginner , There may be some deviations in the understanding of various concepts, please point out , We are also welcome to communicate with each other ## Dependency injection ![](https://upload-images.jianshu.io/upload_images/8869373-eaf38529e04ecdfc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) As for writing habits Django I say , Dependency injection was rarely used before , I just searched , Find out Stack Overflow There's a discussion on (Why is IoC / DI not common in Python?) Well said , It's done me a bit of literacy , Portal :https://stackoverflow.com/questions/2461702/why-is-ioc-di-not-common-in-python Python The module is built into singleton mode , Compared with Java How many implementations of this even singleton pattern can be used in the test language , I don't know where it is ,C# Much better , There are static classes , However, in the development, we will still use Singleton Mode registered to IoC Container to implement singleton ~ There's a good analogy to dependency injection , It is to use static language as dynamic instruction code language : Throw a bunch of things in when you start IoC In the container , Tell you what the container will give you when it's ready to use , The container will generate it for you , And this pile of things in the container can be imported into other dependencies , So, layer by layer, we depend on each other , If there is a problem in the framework execution process, it will be very painful to investigate hhh, Also pay attention to avoid all kinds of problems such as loop dependence caused by design problems , So it feels like Django It's still good in design , Productivity is really high , other Web Use less frames , except AspNetCore and Django These , We used the traditional Spring、Beego And other minority languages MVC frame ( I even used dart Write backstage , But the eggs ), Have time to try the new Vert.x、WebFlux This kind of , It should be fun ~ That's it IoC and DI Say AspNetCore and Spring almost , It's almost functional , The way to use it is similar ,hhh, But it's certainly not as good as Python Happy , But why should I use AspNetCore What about this kind of thing , Because static type languages are cool to write ,Drf It's very fast to write interfaces , But it's basically guessing , Ha ha ha , I feel this description is very appropriate . Recently there was a FastApi It looks good , It is said to be 100% Type annotation of , You can put Python Write as a static type language , But this framework is so new , It's not proven by the market , It's hard to use in a production environment … ## ORM ![](https://upload-images.jianshu.io/upload_images/8869373-39acee66f2f8b164.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Database operations are now Web The core of the framework ( By mistake ?), I don't know either , Anyway Web Development is basically CRUD, It's right to deal with databases all day , therefore ORM Very important , The design is reasonable and easy to use ORM It can reduce a lot of work ,Django One of the big reasons why it's popular is that it's ORM It's powerful and easy to use , This should be one of the advantages of dynamic language . .NetCore Other things on the platform ORM I've only used EF Core and Chloe( Official website :https://github.com/shuxinqin/Chloe), This Chloe It's also a domestic product ORM, It's very convenient to use , But database migration is not supported , There is no way to generate database tables based on code , It's still a little uncomfortable .EFCore It's Microsoft's official ORM, Nature is easy to use , Database migration is supported , This is very convenient , And then I can't sum up the other features , Because it's not deep , No special experience . I chose this project ORM It's made in China FreeSQL, The reason is that I occasionally saw an article written by Wang Po selling melons in the blog Garden , I think it's not bad , And then you can use it , It's really good to use , Database connection can be established in a very light sentence , It can also be like EFCore That has all kinds of rich functions , And it also supports DB First, This is very comfortable , This time, it's because it's a Django Project moved to AspNetCore, So I'm going to try the original database first , And then FreeSQL a fit DB First After that, you can use it , It will strengthen the confidence to continue to use , ha-ha ( Although there are many holes in the back , It's mostly about many to many relationships , By DjangoORM Spoiled ) FreeSQL Official website file :https://github.com/dotnetcore/FreeSql/wiki Experience it ,FreeSQL For foreign keys ( One to many 、 Many to one )、 Many to many 、 The one-to-one relationship is still OK , Through 「 Navigation properties 」, Follow EFCore It should be about , But the definition Model If there are foreign keys , There is also a more defined field to store Id, It feels a little anti human ,EFCore no need , Reference objects directly , After migration, it is reflected as a foreign key in the table Id, But this FreeSQL It's a soft foreign key , A reference is a reference , Relationships are all handled in code , Do not generate foreign keys in the database , This also has its advantages , It's even an advantage , Sometimes it's a bit cumbersome to maintain integrity tables hhh Reference article :.NET ORM What problems can navigation properties solve ?https://www.cnblogs.com/kellynic/p/13575053.html And then a little comparison Kotlin Of Ktorm, It's also an excellent ORM, Last time I had a little trial , It's nice , Except that there is no migration function, you can't create a table automatically , This Ktorm It's similar to EFCore The experience of , It's the same as foreign key direct reference line , Don't look like FreeSQL Leave one like that Id Field to store , Each has its advantages and disadvantages … It doesn't unfold *PS: I make complaints about the previous articles Ktorm After being seen by the author, I found a hand to guide me to write code ( flee ), Some of the holes are not really about the frame , I'm still too good for myself …* ## Database ![](https://upload-images.jianshu.io/upload_images/8869373-8dba01755d701da8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) I use the database SQLite, I use this for all small projects , It works well , Easy to deploy , Although there are write locks , But just a few people are using the system , What kind of bike ? - Remember once SQLite Performance optimization https://www.isaced.com/post-272.html - Every day 4 Billion lines SQLite Order big data test https://cloud.tencent.com/developer/article/1016993 ## ViewModel ViewModel It's important in a hierarchical architecture , In fact, except ViewModel There's another one called DTO(Data Transfer Object) The concept of ,ViewModel and DTO stay MVC In the framework, it belongs to Model Layer ,DTO It can also be called Input Model, Because it is generally used as an object for external input data ( As the name implies ), And then it's always about Model, In fact, it can also be called Entity, These objects are directly mapped to tables in the database , Generally in View perhaps Api It doesn't directly present Model, It's about presenting ViewModel, Because maybe we will do some processing for the output data , For example, add fields and delete fields , use Model Definitely not , The tables are fixed . Empathy , When you pass in the data , It does not necessarily coincide with the data table , So we need help DTO, Actually with ViewModel There's no hard line , You want to take DTO It's OK to present , It's just that I feel ViewModel Look at the name is to show , Habits ha ha . In fact, the traditional MVC、MVVM The patterns are a little out of date , I think now Web The application has to follow DDD(Domain-Driven Design) Patterns to design , But DDD It's a little complicated for beginners , There are many concepts , It's hard for me to explain , A quote from Zhihu's answer : >DDD In short, it is : There are many in a project Bounded Context, For example, one is front-end user oriented , One is the backstage for the company's internal personnel . Every Bounded Context It will be divided into many aggregate. Every aggregate It contains a bunch of objects that need to be transactional consistent , That is to say, from the global perspective , Every aggregate As a whole . A common example is Order( Order ) and OrderLine( The goods in the order ).Entity Include Aggregate root They all have a unique identity . The difference between the two is aggregate root There is a unique identifier in the global , and aggregate Inside others entity It's just aggregate There's a unique identification inside ( Maybe you have a primary key in your database , But this means that conceptually, this unique identifier doesn't mean much to the whole domain , Only in aggregate The internal meaning is great ). additional value object It's an object without a logo , Their significance lies in its value , You can imagine comparing two entity Is it the same one that just needs to judge ID Whether it is equal or not , And judgment value objects You need to see if each attribute is the same . >![](https://upload-images.jianshu.io/upload_images/8869373-d514ca7d26cd74c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) > > author : Uncle Bao ya Link to :https://www.zhihu.com/question/25256772/answer/77940547 Source : You know The copyright belongs to the author . For commercial reprint, please contact the author for authorization , Please indicate the source of non-commercial reprint . And then , So many things are moving around , There are many similar fields , It's silly and unnecessary to assign values one by one , At this time AutoMapper It's about to be used , The specific usage is not the scope of this article ( Refer to the official documents listed below ), But to mention a little ,AutoMapper It can be configured , What fields are not allowed to be excluded , I am here Profile That's how it's written in Chinese : ```c# public class VideoProfile : Profile { private readonly List _unmapped = new List { "HostEnvironment", "Previews", "OverallRating" }; public VideoProfile() { CreateMap (); ShouldMapProperty = (property) => !_unmapped.Contains(property.Name); } } ``` Put the fields you don't want in one List Inside , And then use lambda To configure ShouldMapProperty That's it . Refer to - AutoMapper https://docs.automapper.org/en/latest/Getting-started.html - RESTful+FreeSql+AutoMapper https://luoyunchong.github.io/vovo-docs/dotnetcore/examples/freesql-sample-blog-restful-use-automapper.html#adding-profiles - FreeSql、AutoMapper Processing many to many query mappings https://blog.csdn.net/XinShun/article/details/104755959 ## Something about C# Language ![](https://upload-images.jianshu.io/upload_images/8869373-79ebb68a613ef300.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) C# I've used language for a long time , But there is hardly any special study of grammar , The language ,emmm, Lower limit , The ceiling is high , It's easy to get started , But it's more complicated than Java Much higher , There's a lot of grammar sugar , So many people in the circle feel superior to others. Ha ha , But in fact, the ecology in China is relatively poor , There are some training classes and so on , However, foreign countries can feel that it is a relatively popular technology , What kind of problems do you have , Maybe you can't find it with Chinese keywords , But in English, you can easily find many high-quality answers , It can only be said that the domestic technical environment has been biased by big factories like Ali , Everybody's going to do it Java 了 , A lot of other good technologies and good languages are out of the question , It's a pity …… This project didn't use anything tall , It's generic 、 Extension suite method 、 Higher order functions and so on , It can fly , Post a little bit of code to see Ha ha ~ ```c# public static IList ToViewModels ( this ISelect data, IService service, int limit = 0 ) where TModel : class where TViewModel : class { var limitData = limit == 0 ? data : data.Limit(limit); return limitData .ToList() .Select(service.Get) .ToList(); } public static IEnumerable ToSampleViewModels ( this ISelect data, IService service, int limit = 0 ) where TModel : class where TViewModel : class { return data.ToViewModels(service) .Shuffle() .Take(limit); } ``` This code is to put FreeSQL The result of query database is converted to ViewModel, The conversion process is through Service, The function is very simple , But it's not so cool to make an extension kit method and call , Less n Multiple code repeats , Very comfortable ~ And then C# At first, I was not used to it , Because I used to write before python、dart and js, All are map、reduce、filter These , however C# Of LINQ But it changed its name ,map Correspondence select、filter Correspondence where, It's like SQL The style of , I couldn't find it at first …… All in all C# Language is worth learning , It is helpful to improve work efficiency and programming thinking ! Read more and you'll be done ! ![](https://upload-images.jianshu.io/upload_images/8869373-9c07b1e44a9fb8ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### References - [C# in IEnumerable、ICollection、IList、List The difference between ](https://www.cnblogs.com/sunliyuan/p/5816666.html) - C# Introduction to higher order functions https://www.jianshu.com/p/8a8f3743969b ## Several libraries In addition, there are several libraries that seem to be quite good , I haven't used it yet , At present, we have put AspNetCore Applied to work , There should be a chance to use these libraries in the future hhh - Util Application framework :https://github.com/dotnetcore/Util/blob/master/readme.md - Masuit.Tools:https://github.com/ldqk/Masuit.Tools ## Welcome to exchange I organized a series of technical articles and materials , On the public account 「 Programming lab 」 Backstage reply linux、flutter、c#、netcore、android、kotlin、java、python Relevant technical articles and materials can be obtained , At the same time, any questions can be posted backstage on the public account

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢