NewLife. XCode Getting Started Guide

Source: Internet
Author: User
Tags connectionstrings

What is XCode?

I will not introduce XCode too much here. XCode was a lightweight ORM Component and is now a heavyweight data ing framework that supports ing object data to different media, it provides an object-oriented way to operate databases, solving more than 90% of database operation scenarios. it is the brainchild of dashi and his NewLife team over the past 10 years. Thank you for planting the tree!

You can use XCode to generate Domain for addition, deletion, modification, and query. I don't need to be more powerful with An ORM. I just want to make it simple and easy to use. I can find the data I want, complete the operations I want, and help me in case of any problems. if your requirements are the same as mine, stop and take a look at the magic of XCode!

 

What is XCode?

Please move to the big stone blog

ORM Component XCode)

Http://www.cnblogs.com/nnhy/archive/2010/09/12/1824349.html

 

ORM Component XCode (Introduction)

Http://www.cnblogs.com/nnhy/archive/2010/09/13/1824666.html

 

 

What I said to the person who first came into contact with XCode

For beginners, XCode seems to have given a very high threshold, no complete tutorial, no guidance, and some are just source code and some fragmented blog posts and advanced applications, so that beginners cannot get started, I am also a newbie with xcodefor more than a month. I am afraid to give me some advice. I just want to enrich the XCode tutorials so that more people will not leave the door. this is the original intention of the author to write this article. as to whether it can be written into a series, let's look at the response.

 

Body starts!

 

Preparations for using XCode for the first time

For the first time users, the first thing necessary is to provide all the resources and materials. This is the first time the landlord used XCode to download the Dll and generator for a long time, I still don't know if the download is the latest. here, the full version of XCode is not available. The stable version means that every day the big stone and his NewLife team are updating, and the source code is compiled four times on ftp every day, finding what suits you is the most important thing. Some people in the group haven't updated the dll for three months, so it works well. do not blindly pursue the latest version, or you just like to be a mouse.

1. Download DLL

Http://dl.dbank.com/c0y8uwqaxg

2. Download the Code Generator

Http://dl.dbank.com/c0o72z14hm

3. Select Download entry instance

Http://dl.dbank.com/c09zc22xx4

 

Get started with XCode

 

1. Create a project

2. Reference DLL

It's really nice to everyone that I want to talk about here. After you decompress the DLL package, you will find that there are many DLL files. What exactly are these DLL files, which one should I reference?

DLL is actually all the components of the NewLife team. If you only use the ORM framework, you only need to use the DLL mentioned below. In fact, the role of other DLL is not very clear to the author, the landlord is not here to mislead everyone.

NewLife. Core. dll NewLife Core code, which must be referenced by XCode

XCode. dll XCode code

Therefore, the referenced DLL mainly uses these two DLL files. The XML files with the same name are copied together to display the comments when prompted intelligently. the pdb files with the same name are debugging files, when an error is reported, it indicates the number of rows in which the error occurred. It is used to locate the exact location of the error when asking big stone and his team for help.

 

3. Create a database

What makes XCode powerful is that it can not only export Domain information from the database, but also generate reverse databases based on connection fields. In other words, it may not be useful, you can use SQL2K for development. Then, you can directly deploy your program during deployment. XCode will automatically generate a database based on the connection fields. You can directly deploy the database to MySQL and SQL2005, you do not need to modify the code or back up the database. of course, XCode only generates a database table with no data. XCode also supports the initialization menu and administrator data, but it is not discussed in this section. these will be introduced later. (config is required for this function. This function is disabled by default)

Here we are talking about how to export Domain from the database for our use.

We will first create a new database named XCodeTest. Here we will use the most typical student-subject-instructor-role table to demonstrate it for convenience.

Create a Student table, Set ID to primary key, auto-Increment

Create a Subject table, Set ID to primary key, auto-Increment

Associate TeacherID. One subject is the responsibility of one teacher. One teacher can take the responsibility of multiple subjects.

Create a Teacher table, Set ID to primary key, auto-Increment

Create a sequence table, ID Primary Key, auto-Increment

Associate SubjectID with StudentID

When creating a database, I repeatedly stressed the ID Primary Key and auto-increment because XCode supports the auto-increment primary key better. Inside the Save method, you can select Insert or Update by judging whether the ID is null. You can also use Guid as the primary key, but it is inconvenient to use it. You need to control Insert or Update by yourself.

 

4. Code Generator

Finally, it was the turn of the code generator. To be honest, the landlord did not know how to use the code generator for the first time.

Before starting the code generator, we need to configure the connector to connect to the connector string. In the decompressed file, there is xcode.exe. config. Open it and edit it. We can see that there are already many connection fields, which are examples. If we are SQL, copy an mssql and modify them.

<add name="XCodeTest" connectionString="Server=(local);User id=sa;Pwd=sa;Database=XCodeTest" providerName="System.Data.SqlClient"/>

After modification, save and close.

Open the code generator and see that the connection string we just wrote has been read.

 

Then click Connect. We can see that all our tables have been read.

Next, change the connection name, namespace, and output directory.

The template is generated. Select the template and click "generate all tables". You only need to select two items for the template: entity business and Entity Data. Select a template and click "generate all tables" at a time, select another template and click again to generate all tables!

Turn off the code generator, return to our project, create a folder Domain, right-click the folder, add --- existing items, and add all the files just generated

 

5. Start from Login

Drag 2 TextBox and a Button will start.

Username: <asp: TextBox ID = "txtUserName" runat = "server"> </asp: TextBox> <br/> & nbsp; Password: <asp: textBox ID = "txtPsw" runat = "server" TextMode = "Password"> </asp: TextBox> <br/> <asp: button ID = "btnLogin" runat = "server" Text = "Button"/>

 
Next, click the event of the logon button, which is inserted here:
In this service, we need to search for users based on their usernames. Let's take a look at two Student-related files, one Student. cs and the other Student. Biz. cs.
Go in and take a closer look (PS, my Windows Live Writer code coloring plug-in seems useless ~~~ I don't know if the message will be colored)
public partial class Student : IStudent{}    public partial class Student : Entity<Student>{}

Here we can see that the partial attribute is partial, so our Student class can be written in multiple files, so that we can use this feature to create a new

Student. My. cs

If an error is reported, don't worry. Just change it. Then you can write all the code we added and modified in this file. In this case, if the database structure changes, the entity and business can be regenerated as long as the two files are overwritten. The code we write is not affected at all. Previously, the author directly wrote the code in Biz. in cs, the database was changed, and every time it was replaced, it was a fear of overwriting the code written by myself.

Next, we will continue our business. We need to find the user based on the user name, which is the Biz generated by XCode. the cs file already has some common methods. You can take a look at them, including some advanced methods, which have been commented out and can be modified accordingly,

/// <Summary> search by Name </summary> /// <param name = "id"> </param> /// <returns> </returns> public static student FindByUserName (string userName) {if (Meta. count & gt; = 1000) return Find (_. name, userName); else // entity cache return Meta. cache. entities. find (_. name, userName); // cache a single object // return Meta. singleCache [id];}

Here it is necessary to explain the code, Meta. count: Check whether the table contains more than 1000 data. If it does not exceed 1000, use the entity cache to obtain data. This is suitable for tables like menus and permissions with fixed and few contents, it can be obtained directly from the cache, which is highly efficient. If there are more than 1000 entries, the efficiency of searching from the memory and the database will not be very advantageous. Therefore, you can find them directly from the database.

Next, return to the logon page and click the event.

Protected void btnLogin_Click (object sender, EventArgs e) {string userName = txtUserName. text. trim (); string psw = txtPsw. text. trim (); Student student = Student. findByUserName (userName); if (student! = Null) {// here, the password is not encrypted by md5. In real applications, encryption is required. Do not repeat the same steps as csdn if (psw = student. psw) {// jump to Response after successful logon. redirect ("Welcome. aspx ") ;}} Response. write ("the user name or password is incorrect !!! ");}

Create a Welcome. aspx file and enter "Login successful ~~~ Saving the user status after logging on is not covered in this section! You can handle this by yourself ~~

To demonstrate this, add a user to the database ~

Compile and execute ~~ Result... error...

Taking a closer look, XCode was clever and found that TeacherID exists in the Subject table. Therefore, it is associated with the ID in the Teacher table, and a lot of extended attributes are generated,

The most terrible thing is that two copies are generated consecutively, resulting in errors, as long as the error occurs in Biz. in cs, just delete the content. If you have the ability, you can analyze the error carefully and delete the excess content. In fact, XCode is a kind of bad thing.

 

Here I have deleted the top and bottom lines with red lines!

 

An error is returned ~~

Okay, I admit that I was wrong. I should not name the table as Score, and there is another Score attribute in it. The result is that this class is called Score and there is a Score attribute in it, this is easy. Change the attribute Score to MySCore.

Compile again and continue to report an error,

Okay, there's another interface that hasn't been changed. Let's change it together.

 

Compile ~~~ Pass (PS: Here, I wrote the error and how to solve it. Let's take a look at it... when an error is reported, do not worry about it. Reading the error prompt is actually a good solution. You can't ask du Niang)

Once you enter your account and password, click OK. An error is returned.

Oh ~~~~ MyGod, why did I forget to configure the Web. Config connection field ~~~~ We can see that please set the XCodeTest connection string, so our connection field name must be XCodeTest, which is generated when we use the code generator .... in fact, you can also go to Student. the connection field name is declared in the attribute Declaration on top of cs.

End the program and open web. config.

  <connectionStrings>    <add name="XCodeTest" connectionString="Server=(local);User id=sa;Pwd=sa;Database=XCodeTest" providerName="System.Data.SqlClient"/>  </connectionStrings>

 

Configure the connection field and run it again ~~

When I saw this, I knew it was successful .~~ Try another wrong password ~~

Hey ~~ Succeeded ~~ In the next section, we will demonstrate the operations on the orders table ~~~

 

 

Demo of this article:

Http://dl.dbank.com/c0m64ioz5r

 

NewLife Forum address:

Http://www.53wb.com

 

Big stone blog:

Http://www.cnblogs.com/nnhy/

 

NewLife. XCode Development Resource Directory

Http://www.cnblogs.com/asxinyu/archive/2012/06/02/2532210.html

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.