使用命令行開始你的netcore之路

在Windows/Linux/macOS 下使用命令行開始你的.netcore 之路

準備:

sdk工具:官方提供、最後的latest .NET Core CLI t工具:官方提供

hellow  控制檯app

using System;
namespace ConsoleApplication
{
   public clss Program
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Hello World!");
       }
   }
}
$ dotnet new
$ dotnet restore
$ dotnet run

快速開始:
$ dotnet new
創建一個 project.json 文件以完成依賴關係和程序的入口點.

{
    "version": "1.0.0-*",
    "buildOptions": {
        "emitEntryPoint": true
    },
    "dependencies": {
        "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0"
        }
    },  
   "frameworks": {
        "netcoreapp1.0": {
            "imports": "dnxcore50"
        }
    }
}

Program.cs:

using System;
namespace ConsoleApplication
{
   public class Program
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Hello World!");
       }
   }
}

$ dotnet restore
分析 project.json 文件, 下載依賴並返回結果。 (或 從一個 cache 機器 grabs ), 寫 project.lock.json 文件.
project.lock.json 文件:准許和完成NuGet的graph。 其他工具將讀取它,如: dotnet build dotnet run, 保證用一個正確的方式處理原代碼。即: NuGet 依賴和和綁定方案.
$ dotnet run
調用dotnet build 建造運行,調用dotnet來運行應用..

$ dotnet run
Hello, World!

可以用dotnet build 來編譯代碼:修改 project.json 文件來滿足你的要求。
首先刪除"type": "platform" 依賴元素. 這個項目僅依賴"Microsoft.NETCore.App".

"dependencies": {
   "Microsoft.NETCore.App": {
       "version": "1.0.0"
   }
},

接着指明一個目標環境的運行節點,.以下爲:Windows 10  64 位版本 和 Mac OS X 的 10.1版本1

"runtimes": {
 "win10-x64": {},
 "osx.10.11-x64": {}


$ dotnet restore
$ dotnet build
$ .\bin\Debug\netcoreapp1.0\win10-x64\HelloNative.exe
Hello World!

您會注意到本地應用程序需要長時間build,但運行速度快一些。隨着應用程序的增長,變得更加明顯。
build 進程產生一些文件. 文件在 bin\Debug\netcoreapp1.0\中,RID 選項.項目的hellonative.dll,有一個hellonative.exe,它完成運行庫加載和啓動應用程序。注意,所生成的應用程序的名稱被更改,因爲項目目錄的名稱已更改。
您可能希望將此應用程序包,放在網絡運行時的機器上執行。請使用DotNet命令發佈。 DotNet發佈命令創建一個新的子目錄。/bin/debug / netcoreapp1.0 / < >目錄稱爲發佈平臺。它複製可執行文件,所有相關的DLL和框架,可以將該目錄複製到另一臺機器(或一個容器)並在那裏執行應用程序。
以下爲:第一個Hello World示例發佈。程序是一個可移植的應用程序,默認類型的應用程序。一種便攜式應用程序,在目標機器上安裝了一個網絡核心。便攜式應用可以建立在一臺機器上,並在任何地方執行。本地應用程序必須單獨爲每個目標機構建。DotNet發佈創建一個目錄,應用程序的dll,和任何相關的DLL,不是平臺安裝部分。
建議program
讓我們改變文件一點點。斐波那契數列是有趣的:
Program.cs:

using static System.Console;
namespace ConsoleApplication
{
   public class Program
   {
       public static int FibonacciNumber(int n)
       {
           int a = 0;
           int b = 1;
           int tmp;

           for (int i = 0; i < n; i++)
           {
               tmp = a;
               a = b;
               b += tmp;
           }

           return a;  
       }

       public static void Main(string[] args)
       {
           WriteLine("Hello World!");
           WriteLine("Fibonacci Numbers 1-15:");

           for (int i = 0; i < 15; i++)
           {
               WriteLine($"{i+1}: {FibonacciNumber(i)}");
           }
       }
   }
}

開始運行:假設在windows裏,項目目錄爲: Fibonacci:

$ dotnet build
$ .\bin\Debug\netcoreapp1.0\win10-x64\Fibonacci.exe
1: 0
2: 1
3: 1
4: 2
5: 3
6: 5
7: 8
8: 13
9: 21
10: 34
11: 55
12: 89
13: 144
14: 233
15: 377

單一的文件是簡單的一次性程序,多個文件有多個組件。多個文件是一種好的方法。 創建一個新的文件,並給它一個唯一的命名空間::

using System;
namespace NumberFun
{
   // code can go here
}
以下是 Program.cs文件:
using static System.Console;
using NumberFun;<< span="">

build它:
$ dotnet build
使新的文件做一些事情            
例如:一個斐波那契序列發生,比如說建立了以前的斐波那契,緩存一些斐波納中間值和添加一些遞歸。更好的斐波那契的例子看起來像這樣:

using System;
using System.Collections.Generic;

namespace NumberFun
{
   public class FibonacciGenerator
   {
       private Dictionary<int, int>_cache = new Dictionary<int, int>();

       private int Fib(int n) => n < 2 ? n : FibValue(n - 1) + FibValue(n - 2);

       private int FibValue(int n)
       {
           if (!_cache.ContainsKey(n))
           {
               _cache.Add(n, Fib(n));
           }

           return _cache[n];
       }

       public IEnumerableGenerate(int n)
       {
           for (int i = 0; i < n; i++)
           {
               yield return FibValue(i);
           }
       }
   }
}

注意,使用Dictionary< int,int,int>和IEnumerable均在System.Collections namespace< >系統中。microsoft.netcore.app包是一元軟件包,包含了許多NET框架的核心組件。通過包括本元軟件包,你已經有system.collections.dll組件作爲項目的一部分。你可以通過運行驗證這個模塊發佈和檢查是安裝包的一部分文件。你會看到列表中的system.collections.dll。

{
 "version": "1.0.0-*",
 "buildOptions": {
   "debugType": "portable",
   "emitEntryPoint": true
 },
 "dependencies": {},
 "frameworks": {
   "netcoreapp1.0": {
     "dependencies": {
       "Microsoft.NETCore.App": {
         "version": "1.0.0"
       }
     },
     "imports": "dnxcore50"
   }
 },
 "runtimes": {
   "win10-x64": {},
   "osx.10.11-x64": {}
 }
}

調整在Program.cs文件下main()方法、如下所示。假設cs使用System;聲明,有一個使用using static System.Console。

public static void Main(string[] args)
{
   var generator = new FibonacciGenerator();
   foreach (var digit in generator.Generate(15))
   {
       WriteLine(digit);
   }
}
完成運行
$ dotnet run
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

使用文件夾組織代碼,通過添加多個文件,確保給他們的命名空間.
/MyProject
|__Program.cs
|__AccountInformation.cs
|__MonthlyReportRecords.cs
|__project.json

當你的項目的規模相對較小,這是偉大的作品。然而,如果你有一個更大的應用程序,有許多不同的數據類型和潛在的多個層,這是文件夾的發揮。你可以創建自己的文件和文件夾。            
/NewTypes
|__/Model
|__Program.cs
|__project.json

增加新的類型到文件夾:
/NewTypes
|__/Model
  |__AccountInformation.cs
  |__MonthlyReportRecords.cs
|__Program.cs
|__project.json

同一個目錄使用同一個明媚空間
Program.cs.
Example: Pet Types
以下爲創建兩個新類型例子.
Folder Structure:
/NewTypes
|__/Pets
  |__Dog.cs
  |__Cat.cs
  |__IPet.cs
|__Program.cs
|__project.json

IPet.cs:
using System;
namespace Pets
{
   public interface IPet
   {
       string TalkToOwner();
   }
}
Dog.cs:
using System;
namespace Pets
{
   public class Dog : IPet
   {
       public string TalkToOwner() => "Woof!";
   }
}
Cat.cs:
using System;
namespace Pets
{
   public class Cat : IPet
   {
       public string TalkToOwner() => "Meow!";
   }
}
Program.cs:
using System;
using Pets;
using System.Collections.Generic;
namespace ConsoleApplication
{
   public class Program
   {
       public static void Main(string[] args)
       {
           Listpets = new List
           {
               new Dog(),
               new Cat()  
           };

           foreach (var pet in pets)
           {
               Console.WriteLine(pet.TalkToOwner());
           }
       }
   }
}
project.json:
{
 "version": "1.0.0-*",
 "buildOptions": {
   "emitEntryPoint": true
 },
 "dependencies": {
   "Microsoft.NETCore.App": {
     "type": "platform",
     "version": "1.0.0"
   }
 },
 "frameworks": {
   "netcoreapp1.0": {
     "imports": "dnxcore50"
   }
 }
}

開始運行

$ dotnet restore
$ dotnet run<< span="">

Woof!
Meow!

新的寵物類型可以添加(如鳥),擴展這個項目。
這裏有一個很好的方法來做:動源你現有的項目進入了一個新的SRC文件夾。

/Project
|__/src
1.Create a /test directory.
/Project
|__/src
|__/test
2,Create a new global.json file:
/Project
|__/src
|__/test
|__global.json
3.global.json:
{
  "projects": [
     "src", "test"
  ]
}

該文件告訴構建系統,這是一個多項目系統,它允許尋找依賴關係,而不僅僅是當前文件夾中的執行情況。這是很重要的,因爲它允許您將依賴於測試項目中的代碼放在測試中。 例如:擴展新項目 現在項目系統已就位,您可以創建測試項目並開始編寫測試項目!從這裏開始,本指南將使用和擴展示例類型項目。此外,它將使用xUnit測試框架。隨時按照或創建自己的多項目系統測試。
整個項目結構應該是這樣的:   
/NewTypes
|__/src
  |__/NewTypes
     |__/Pets
        |__Dog.cs
        |__Cat.cs
        |__IPet.cs
     |__Program.cs
     |__project.json
|__/test
  |__NewTypesTests
     |__PetTests.cs
     |__project.json
|__global.json

有兩個新的東西,以確保你在你的測試項目:
一個正確的project.json
參考xUnit
參考模塊測試xUnit
在測試中對應於代碼的命名空間的引用
XUnit測試類。
newtypestests / project.json: :

{
 "version": "1.0.0-*",
 "testRunner": "xunit",

 "dependencies": {
   "Microsoft.NETCore.App": {
     "type":"platform",
     "version": "1.0.0"
   },
   "xunit":"2.2.0-beta2-build3300",
   "dotnet-test-xunit": "2.2.0-preview2-build1029",
   "NewTypes": "1.0.0"
 },
 "frameworks": {
   "netcoreapp1.0": {
     "imports": [
       "dnxcore50",
       "portable-net45+win8"
     ]
   }
 }

PetTests.cs:

using System;
using Xunit;
using Pets;
public class PetTests
{
   [Fact]
   public void DogTalkToOwnerTest()
   {
       string expected = "Woof!";
       string actual = new Dog().TalkToOwner();

       Assert.Equal(expected, actual);
   }

   [Fact]
   public void CatTalkToOwnerTest()
   {
       string expected = "Meow!";
       string actual = new Cat().TalkToOwner();

       Assert.Equal(expected, actual);
   }
}}

現在開始測試,請確保在頂級目錄
$ dotnet restore
$ cd test/NewTypesTests
$ dotnet test

輸出: 
xUnit.net .NET CLI test runner (64-bit win10-x64)
 Discovering: NewTypesTests
 Discovered:  NewTypesTests
 Starting:    NewTypesTests
 Finished:    NewTypesTests
=== TEST EXECUTION SUMMARY ===
  NewTypesTests  Total: 2, Errors: 0, Failed: 0, Skipped: 0, Time: 0.144s
SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章