ShowProgramCode

2021年4月23日 星期五

C#中DTO資料傳給另一個DTO

 最近換了工作,所以要開始寫ASP.NET了,C#和PHP的差異不小,因此寫起來有點...

遇到問題是一定的,決定開始慢慢建構自己的C#筆記。

不過對於C#而言,我還是個新手,如果有什麼錯誤還請多多包容。

今天遇到的問題就是要把一個資料物件轉到另一個相似的資料物件裡,這個物件除了get()和set()外,幾乎沒有任何其他函式,PHP的話很簡單,不過遇到嚴謹的C#就糟糕了。

查了諸多資料,終於嘗試出一個解法,把程式碼和測試碼放在下面。

參考網頁:https://dotblogs.com.tw/JustTree/2019/01/04/140125

程式碼:

public static class DtoDataToDto

{

     /// <summary>容器物件轉換</summary>

     /// <typeparam name="T1">輸入物件格式</typeparam>

    /// <typeparam name="T2">輸出物件格式</typeparam>

    /// <param name="Data1">輸入物件資訊</param>

    /// <returns>T2</returns>

    public static T2 ObjectDataChange<T1, T2>(T1 Data1)

    {

        string strjsonData = JsonConvert.SerializeObject(Data1);

        var returnData = JsonConvert.DeserializeObject<T2>(strjsonData);

        return returnData;

    }

}


測試碼:

    class TestDemo1

    {

        public int item1 = 1;

        public string item2 = "2";

        private int item3 = 3;

        private string item4 = "4";

        public int item5 { get { return item3; } }

        public string item6 { get { return item4; } }

    }


    class TestDemo2

    {

        public int item1;

        public string item2;

        private int item3;

        private string item4;

        public int item5 { get { return item3; } }

        public string item6 { get { return item4; } }

    }


    [TestClass]

    public class DtoDataToDtoTest

    {

        [TestMethod]

        public void ObjectChangeDataTest()

        {

            TestDemo1 testDemo1 = new TestDemo1();

            TestDemo2 testDemo2 = DtoDataToDto.ObjectDataChange<TestDemo1, TestDemo2>(testDemo1);

            Assert.AreEqual(testDemo1.item1 , testDemo2.item1);

            Assert.AreEqual(testDemo1.item2, testDemo2.item2);

            Assert.AreNotEqual(testDemo1.item5, testDemo2.item5);

            Assert.AreNotEqual(testDemo1.item6, testDemo2.item6);

            Assert.AreEqual(testDemo2.item5 , 0);

            Assert.IsNull(testDemo2.item6);

        }

    }

2019年5月5日 星期日

laravel+bootstrap(composer install & npm install)

進入laravel之後,開始製作自己的頁面才發現Bootstrap Css沒有正確顯示,查了一下資料,雖然不是完全理解,不過我想大概是因為我沒有執行composer install和npm install,laravel需要外掛的一些元件沒有掛上去。

那麼開始執行程式吧!首先我沒有修改任何檔案,下面是cmd執行composer install的結果:

C:\AppServ\www\project\calibreBook
λ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Package manifest generated successfully.

到底安裝了什麼,其實我也搞不清楚,不過看到了php Unit,應該是和單元測試相關吧!其他東西就先不管了。

接著要執行npm install,這個時候cmd報錯了,npm不是內部或外部命令(我汗)。

查了一下,原來要執行npm前,還要先下Node.js,Node.js還有兩種版本LTS vs Current(狂汗),結論是LTS是長期維護版本,非前端專門的工程師,用這個就十分足夠了,Current裡面則會有新功能,專門的前端工程師可能會需要。

npm install執行時間非常長...(我汗),下面是執行完畢的結果(真的很長@@)
C:\AppServ\www\project\calibreBook
λ npm install
npm WARN deprecated browserslist@2.11.3: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN deprecated browserslist@1.7.7: Browserslist 2 could fail on reading Browserslist >3.0 config used in other tools.
npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5

> node-sass@4.12.0 install C:\AppServ\www\project\calibreBook\node_modules\node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.12.0/win32-x64-64_binding.node
Download complete  ] - :
Binary saved to C:\AppServ\www\project\calibreBook\node_modules\node-sass\vendor\win32-x64-64\binding.node
Caching binary to C:\Users\Amy\AppData\Roaming\npm-cache\node-sass\4.12.0\win32-x64-64_binding.node

> uglifyjs-webpack-plugin@0.4.6 postinstall C:\AppServ\www\project\calibreBook\node_modules\webpack\node_modules\uglifyjs-webpack-plugin
> node lib/post_install.js


> node-sass@4.12.0 postinstall C:\AppServ\www\project\calibreBook\node_modules\node-sass
> node scripts/build.js

Binary found at C:\AppServ\www\project\calibreBook\node_modules\node-sass\vendor\win32-x64-64\binding.node
Testing binary
Binary is fine

> gifsicle@3.0.4 postinstall C:\AppServ\www\project\calibreBook\node_modules\gifsicle
> node lib/install.js

  √ gifsicle pre-build test passed successfully

> mozjpeg@5.0.0 postinstall C:\AppServ\www\project\calibreBook\node_modules\mozjpeg
> node lib/install.js

  √ mozjpeg pre-build test passed successfully

> optipng-bin@3.1.4 postinstall C:\AppServ\www\project\calibreBook\node_modules\optipng-bin
> node lib/install.js

  √ optipng pre-build test passed successfully

> pngquant-bin@4.0.0 postinstall C:\AppServ\www\project\calibreBook\node_modules\pngquant-bin
> node lib/install.js

  √ pngquant pre-build test passed successfully
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.8 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.8: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 1598 packages from 753 contributors and audited 21494 packages in 93.949s
found 34 vulnerabilities (18 low, 11 moderate, 5 high)
  run `npm audit fix` to fix them, or `npm audit` for details

那麼bootstrap到底掛上了沒呢?我看到了bootstrap-sass和jquery,後面還要再試試看才知道。

<link rel="stylesheet" href="/css/app.css">

我開了測試檔案,把上方的這個掛上去就可以看到bootstrap的style了,這個應該是公用的css。

<script src="/js/app.js"></script>

上方的這個應該是公用的js,目前應該包括bootstrap和jquery的js。

到此bootstrap和jquery應該已經掛上了。

2019年4月28日 星期日

laravel web路徑更改+sql連線

安裝了laravel之後,cmd 進入laravel資料夾,執行php artisan serve,就可以看到執行網址<http://127.0.0.1:8000>,在瀏覽器執行這個網址就可以看到laravel在web.php中設定的首頁。

接著就要更改資料庫連線了。

在laravel資料夾下有個.evn檔案,更改檔案中mysql連線,在此之前不要忘記先去資料庫建一個新的DB。

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=資料庫名稱
DB_USERNAME=資料庫帳號
DB_PASSWORD=資料庫密碼

接下來要繼續建立資料表了,記得使用php artisan指令。
php artisan make:model {資料表名稱} --migration
或者php artisan make:model {資料表名稱} -m
參考網址:https://ithelp.ithome.com.tw/articles/10202964

這樣會在兩個地方產生檔案,一是app中產生一個model檔;一是database\migrations中產生一個migrate檔案。

在migrate檔案中function up{},填寫增加的欄位,欄位的屬性設定請參考下方網址。

參考網址:https://laravel.tw/docs/5.2/migrations#creating-tables

完畢之後在cmd中繼續執行php artisan migrate就會自動生成資料表與欄位了。

這時錯誤訊息告知:
In Connector.php line 67:

  could not find driver

查詢資料後修改php.ini,extension=php_pdo_mysql.dll。

cmd中成功顯示如下方:
C:\AppServ\www\project\calibreBook
λ php artisan migrate
Migration table created successfully.
Migrating: 2019_04_28_061541_create_admins_table
Migrated:  2019_04_28_061541_create_admins_table

假設要給預設的假資料,可以使用seed。
在cmd下執行php artisan make:seed Admin
cmd中成功顯示如下方:
C:\AppServ\www\project\calibreBook
λ php artisan make:seed AdminSeeder
Seeder created successfully.

首先是在cmd下執行php artisan make:seed,在database\seeds\下會產生一個seed檔案。
把要產生的假資料寫在public function run()中,並在DatabaseSeeder.php中加上$this->call(AdminSeeder::class)。

這樣資料庫連線、model、seed就全部完成了。




win10+mysql workbench安裝

上次我沒有安裝mysql admin,不過想想還是需要mysql的工具,查了一下決定用Workbench這套軟體。

首先先下載Workbench:
https://dev.mysql.com/downloads/file/?id=485462

mysql-workbench-community-8.0.16-winx64.msi這是下載下來的檔案。

不過安裝的時候,要求有visual C++ 2015,所以繼續下載visual C++ 2015。

https://www.microsoft.com/zh-tw/download/confirmation.aspx?id=48145

先安裝visual C++ 2015在繼續安裝Workbench就成功了,接著連線本地local的連線就ok了。


2019年4月15日 星期一

win10+php7的laravel安裝

composer安裝好了之後,接著終於到了重頭戲,Framework Laravel的掛載了。

查了許多的網頁,目前得到的結論,composer會把laravel需要的環境建置好,所以只要你的php+apache有裝好,接下來只要把laravel設在對外的資料夾就可以了。舉例:我的電腦目前掛的是AppServ那麼對外的資料夾就是www,所以只要把laravel掛在這個資料夾下就可以了。

那麼該怎麼做呢?

以我的電腦的路徑應該是C:/AppServ/www/project,我在www資料夾下加了project資料夾,想把laravel的相關專案放在這裡,所以我只要進cmd輸入下面的命令就可以了。

進入專案資料夾
cd C:/AppServ/www/project

建立一個laravel專案
composer create-project laravel/laravel --prefer-dist "專案名稱"

基本上這樣就OK了,不過這樣處理後,要叫這個專案首頁的網址看起來很糟糕,後面會繼續處理,目前先把網址貼出來。

專案實際位置
C:/AppServ/www/project/calibreBook

首頁網址
http://localhost/project/calibreBook/public/index.php

如果打這個網址,會出現calibreBook的檔案總管
http://localhost/project/calibreBook/

如果打這個網址則會出現not found
http://localhost/project/calibreBook/index.php

目前還不確定是要處理laravel內設的路由還是要處理本機的域名,之後將繼續處理。

最後備註一下,安裝的時候出現了warry!!不確定會不會有狀況,先記錄下來。

phpunit/phpunit suggests installing phpunit/php-invoker (^1.1)
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.




2019年4月14日 星期日

win10+PHP7安裝composer

正式安裝laravel之前,幾乎所有參考資料都要求先安裝composer。

那麼composer到底是什麼呢?

就我自己看完各式各樣的說明和文件後,我是這麼理解的,開發專案的時候,PHP經常性要加掛許多套件,比方laravel這種framework,為了維持開發環境與線上環境的一致性,下載各種套件的時候,不再各自找資源,而是統一利用composer下指令或是設定檔。

在本機開發時跑得好好的程式,上傳到測試環境或是正式環境之後就掛了,然後大家就很命苦的開始查詢各種設定檔,伺服器開放的權限等等各種各樣的問題,我想這樣的經驗應該或多或少都有吧!

composer應該就是為了統一套件版本及來源所使用的方法之一。

在window上加掛composer似乎很簡單,只要下載執行檔後,照著下一步設定就OK了。

下載網址:https://getcomposer.org/Composer-Setup.exe

如果說有什麼狀況的話,因為我的傻瓜安裝程式有兩個PHP版本,所以安裝composer的時候我特意選了PHP7這個版本(預設是PHP5版本)。

最後進入cmd輸入composer --version,如果可以看到版本就代表安裝成功了。

win10+php7掛載xDebug

再加掛Framework和composer之前,先把單步除錯的xDebug掛起來。雖然聽說Framework有類似的除錯工具,不過畢竟是用習慣的比較方便。

所以接下來就是先把xDebug掛載到PHP和NetBean可以使用。

一、下載xDebug

下載網址:https://xdebug.org/download.php

這裡要注意的地方就是xDebug的版本和PHP版本一定要相同,像我一樣掛傻瓜版或不記得PHP版本該怎麼辦呢?最簡單的方法,在首頁加上一行PHP函式phpinfo()。接著進入頁面就可以看到PHP版本還有Compiler。照著這兩個顯示的版本下載xDebug應該就可以了。

這裡還有一個參考資料提供,如果是像我一樣使用Apache的話就下TS版本,使用IIS的話下NTS版本。

如果和我一樣搞不定的話到下列網址把phpinfo()的網頁資訊全部貼上,就可以分析出應該下載哪個檔案了。

https://xdebug.org/wizard.php

二、將檔案掛到PHP

xDebug下載下來應該是一個.dll檔案,把檔案貼到PHP資料夾下的ext資料夾內。

接下來去修改php.ini檔案。

開啟檔案後先查詢有沒有xDebug的設定,沒有的話把下面的設定貼到最下方即可。

;xdebug
zend_extension="php_xdebug-2.7.1-7.1-vc14.dll"
xdebug.remote_enable=on
xdebug.remote_host=127.0.0.1 
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

zend_extension_ts:如果 PHP 安裝 TS 版本,則使用這個。

接著重啟Apache看看Xdebug是否正確加掛。

就我最後的經驗,直接把phpinfo()的資訊貼上去查,下載後貼到php\ext底下,修改php.ini檔案,如果沒有過就把zend_extension="php_xdebug-2.7.1-7.1-vc14.dll"改成zend_extension_ts="php_xdebug-2.7.1-7.1-vc14.dll"重開Apache再看一次。

另外為了防止外取定住畫面,最好使用無痕網頁看。Chome有這個功能。

三、掛載到NetBean

基本上我新增了一個專案試跑就直接成功了,不過提供一下我所查到的資料。

工具\選項\PHP\debugging,在這裡有和debug相關的設定。