Using Apworks to rapidly develop data services in ASP.

Source: Internet
Author: User
Tags install mongodb

Many of my friends who follow my blog know that I have developed an enterprise application development framework called Apworks in about 2009 years, designed to provide domain-driven (DDD)-oriented framework-level solutions for distributed enterprise system software development, and to support a variety of system architecture styles. The development and maintenance of this framework I have been holding on for a long time, until 2015, and I have been constantly refactoring this project. The project is currently on GitHub with nearly 260 recommendations, a lot of friends who are interested in technology have been keeping in touch with me, and even enthusiasts have spontaneously formed a group of technical discussions dedicated to sharing the Apworks framework.

However, with software development techniques and. NET development, the framework of the design and development of technology are gradually outdated, the restructuring of the difficulty gradually increased, many of its own support technology, such as MSMQ, NHibernate also gradually fade out of sight, in contrast, cloud computing, microservices, big Data, Cross-platform and other related technologies more and more attracted the attention of the industry, the success of more and more cases. How to quickly build efficient, economical, stable and secure software system architecture based on cloud Platform (PaaS + IaaS) has become a hot topic in the last two years. Microsoft is also adapting to this trend, making a lot of changes, in a short period of time, led. NET cross-platform, open-source many well-known projects, such as. NET, Core CLR, Roslyn, ASP, Entity Framework, PowerShell, and so on, and start accepting and embracing non-Windows operating systems such as visual Studio cross-platform, PowerShell cross-platform, SQL Server cross-platform, Visual C + + supports a variety of compilers, and more. It is clear that the original Apworks is no longer a cross-platform, cloud-friendly, fast-developing trait, and I am determined to rewrite the apworks.

The new Apworks Core application Development Framework

The new Apworks core is also open source project, which is still based on the Apache 2.0 License Agreement, the project address is: Https://github.com/daxnet/apworks-core. The development phase is still ongoing, with only the basic concepts (entities, aggregations, entity keys, warehousing, etc.) in the original DDD being implemented, and for memory concurrency dictionaries (Concurrent Dictionary), MongoDB, and entity Framework With three different warehousing implementations, the entire framework is fully implemented by. NET core (currently available in both net461 and NET Standard 1.6 compilations), so you can use the classic. NET Framework in Windows, It can also be used in the. NET core of Linux. Not only that, the integration and expansion of the ASP. NET Core Web Api,apworks, which makes the development of data services very simple and convenient, this is what this article is prepared to introduce, I believe that after reading this article, you will learn more about Apworks The openness and extensibility of the core, and the ability to realize the NET application development ecosystem, Apworks Core will bring you more help.

Walkthrough: Using Apworks Core to rapidly develop data services

Before you begin our Apworks core development walkthrough, complete the following preparations:

    1. Install visual Studio 2017 to ensure that the development features of. NET Core and ASP.
    2. The Https://www.myget.org/F/daxnet-apworks-pre/api/v3/index.json and https://www.myget.org/F/daxnet-utils/api/v3/ Index.json Two package source added to NuGet's package source:


    3. Prepare a MongoDB database server, it is recommended to download the version of Windows directly on the local machine, you can also run the MONGO Docker container directly under Linux, eliminating the steps to install MongoDB
    4. If you want to be able to try out the Docker features of Visual Studio 2017 together, you'll also need to be sure to install the latest version of Windows and Docker for Windows. Of course, the completion of this walkthrough does not require Docker
Development steps
  1. First, create an ASP. NET core application, named CustomerService, by manage NuGet Packages adds a reference to Apworks.Repositories.MongoDB and Apworks.Integration.AspNetCore. When adding references, be careful to select Https://www.myget.org/F/daxnet-apworks-pre/api/v3/index.json This package source:



  2. Create a new models directory under CustomerService, add two classes with the name address and customer, with the following code:

    public class address{Public    string Country {get; set;}    public string State {get; set;}    public string City {get; set;}    public string Street {get; set;}    public string ZipCode {get; set;}} public class customer:iaggregateroot<guid>{Public    Guid Id {get; set;}    public string Name {get; set;}    public string Email {get; set;}    Public Address contactaddress {get; set;}}

  3. Under the Controllers subdirectory, create a new controller, named Customerscontroller, with the following code:

    public class Customerscontroller:dataservicecontroller<guid, customer>{public    Customerscontroller ( Irepositorycontext repositorycontext): Base (Repositorycontext)    {    }}

  4. Open the Startup.cs file, and in the Configureservices method, add the following code:

    public void Configureservices (iservicecollection services) {    //ADD framework services.    Services. Addmvc ();    Services. Addapworks ()        . Withdataservicesupport (new dataserviceconfigurationoptions new            Mongorepositorycontext                (new Mongorepositorysettings ("localhost", "Customer-service")))        . Configure ();}

  5. Also in the Startup.cs file, in the Configure method, add the following code:

    public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {    Loggerfactory.addconsole (Configuration.getsection ("Logging"));    Loggerfactory.adddebug ();    App. Enrichdataserviceexceptionresponse ();    App. Usemvc ();}

  6. OK, you ' re all set! After completion, the project structure in Solution Explorer is as follows, adding a models directory, and a customerscontroller:

Compile run

Just press CTRL + F5 to run the site, and when you get up, you'll find that the browser opens up to a standard new ASP. NET Core Web API project, showing the two strings returned by the Valuescontroller get method. Don't worry, open the Fiddler, let's simply test the newly created customer data Service (note that the URL is replaced, my local service runs on port 2238):

    1. POST http://localhost:2238/api/customers



      The post operation successfully returned HTTP 201, while the new object ID was returned in the response body.
    2. GET http://localhost:2238/api/customers

Confined to space, this is not tested for all basic HTTP operations. The Apworks Data Service supports the following HTTP methods by default and, of course, you can extend them as you wish:

    1. Get: Get all objects, default support service end paging, 15 records per page, can be http://localhost:2238/api/customers?page=aaa&size= BBB This format to specify the size of each page and the number of pages to get
    2. get {Id}:http://localhost:2238/api/customers/{id}: Gets an object with an ID value of {ID}
    3. POST {ID}: Replace object with ID value {ID}, return HTTP 204 (No Content) if successful replacement
    4. PATCH {ID}: Updates an object with an ID value of {ID} and the request body needs to conform to the specifications of the Microsoft ASP. NET Json patch. Update successfully returned HTTP 204 (No Content)
    5. Delete {ID}: Deletes an object with an ID value of {ID}. Delete successfully returned HTTP 204 (No Content)
Several highlights
    1. The development and configuration process and its simplicity, as described above, are five steps to complete the development of data services for a data object. Fluent interface (fluent API) configuration, which enables the development of the data service to have the same developer experience as the development process of the ASP. NET Core Web API
    2. HTTP Get calls default support service-side paging, which is represented by the HAL tag in the returned results
    3. HTTP get returns direct support for hypertext Application Language (HAL, official website: Http://stateless.co/hal_ specification.html), return Content-type to Application/hal+json. It was implemented through one of my other open source projects, the HAL project implemented the HAL specification completely and comprehensively under. NET core, and provided a good developer experience through the fluent interface (fluent API) Https://github.com/daxnet/hal. When developing data services through Apworks core, developers can choose whether to require HAL support by setting the Dataserviceconfigurationoptions parameter, or to extend the halbuildconfiguration type, To implement the custom of the HAL return result
    4. Warehousing implementations can choose to use In-memory Concurrent Dictionary, MongoDB, and entity Framework Core, the entire technology stack is completely cross-platform. With the entity Framework Core, EF core support for SQL Server, PostgreSQL, and SQLite is good, and provider support is also available for Oracle, so You can choose from a variety of relational databases to quickly implement data services based on Apworks core
    5. Adding the Enrichdataserviceexceptionresponse method allows the data service's return result to contain more accurate error codes and error messages when an error occurs

For the three types of storage currently supported, the HAL return result customization, as well as the extension of the data service, I will introduce you in the future, there is not much to say.

Summarize

Now that Apworks core is just beginning, there is still a long way to go, from the integration of data Services and ASP. It also requires a richer set of features, such as the help page for the API, the specification of the query criteria and the ordering criteria, and so on. Support for query criteria I'm going to use one of the open source I migrated. NET Parsing framework: Irony project to complete. After that, I'm going to implement a complete application case based on Apworks core, as usual, to demonstrate the capabilities of Apworks core.

On the other hand, Apworks Core's project branching strategy and continuous integration can also be shared. I don't have an open apworks core continuous Integration System, but anyone can learn about the state of the latest build from the project's GitHub home page, or see the latest preview and release version of the package source. Apworks Core is compiled at the same time under Windows Server and Ubuntu Linux, and the compilation is done entirely with PowerShell scripts, so for Linux systems you need to install PowerShell first. I will also try to make some time to introduce you.

Finally, or that sentence, we have a lot of exchanges, more valuable advice. I strive to do better.

Using Apworks to rapidly develop data services in ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.