﻿
{"id":27074,"date":"2024-07-21T16:01:07","date_gmt":"2024-07-21T16:01:07","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=27074"},"modified":"2024-07-21T16:01:07","modified_gmt":"2024-07-21T16:01:07","slug":"net-8-global-using-ile-anlik-type-tanimlama","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/net-8-global-using-ile-anlik-type-tanimlama\/","title":{"rendered":".NET 8 &#8211; global using \u0130le Anl\u0131k Type Tan\u0131mlama"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>C# ile yapt\u0131\u011f\u0131m\u0131z geli\u015ftirmelerde bazen anl\u0131k olarak elimizdeki verileri b\u00fct\u00fcnsel olarak temsil edebilecek ve sadece o an i\u00e7in kullanaca\u011f\u0131m\u0131z bir t\u00fcre ihtiyac\u0131m\u0131z olabiliyor. Bu tarz durumlarda bir class, record ya da struct tan\u0131m\u0131ndan ziyade o anda \u00fcretip, kullan\u0131p ve bir daha herhangi bir noktada ad\u0131 ge\u00e7meyecek bir t\u00fcr olu\u015fturabilmek ne g\u00fczel olurdu de\u011fil mi? <em><strong>Ee iyi de hoca bu i\u015fler i\u00e7in <a href=\"https:\/\/www.gencayyildiz.com\/blog\/cta-anonymous-type\/\" rel=\"noopener\" target=\"_blank\">anonymous types<\/a> yap\u0131lanmas\u0131 var ya la i\u015fte!<\/strong><\/em> diye s\u00f6ylenebilirsiniz. Evet, bu yap\u0131lanma anl\u0131k olarak eldeki verileri ihtiyac\u0131m\u0131z olan formatta temsil edebilmemizi sa\u011fl\u0131yor sa\u011flamas\u0131na ama her ne kadar referans t\u00fcrl\u00fc bir nesne olu\u015fturmu\u015f olsak da \u00f6zellikle bir t\u00fcr\/type\/referans olarak davran\u0131\u015f sergilemedi\u011fi i\u00e7in anl\u0131k ihtiya\u00e7larda pekte i\u015fimize yarad\u0131\u011f\u0131n\u0131 s\u00f6yleyemeyiz. \u015e\u00f6yle ki, anonymous type olarak bir veriyi olu\u015fturdu\u011fumuzda bu verinin bir t\u00fcr\u00fc olmad\u0131\u011f\u0131 i\u00e7in metot d\u00f6n\u00fc\u015f\u00fcmlerinde ya object ya da dynamic olarak return etmek mecburiyetinde kal\u0131yoruz.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass Consumer\r\n{\r\n    public dynamic GetConsumer()\r\n    {\r\n        return new\r\n        {\r\n            Name = &quot;XConsumer&quot;,\r\n            Id = &quot;12345667&quot;\r\n        };\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Mesela yukar\u0131da oldu\u011fu gibi dynamic olarak return etti\u011fimizde, bu metodun kullan\u0131ld\u0131\u011f\u0131 noktada tip g\u00fcvenli\u011finden istifade edemedi\u011fimiz i\u00e7in geli\u015ftirici a\u00e7\u0131s\u0131ndan ciddi zorluklar s\u00f6z konusu olabiliyor. \u0130\u015fte bu sebepten dolay\u0131 o anl\u0131k \u00fcretilip kullan\u0131labilir referans deyince ak\u0131llardan anonymous type&#8217;\u0131 \u00e7\u0131karabiliriz.<\/p>\n<p>Bu durumda gel gelelim akla birde <a href=\"https:\/\/www.gencayyildiz.com\/blog\/tag\/tuple\/\" rel=\"noopener\" target=\"_blank\">tuple<\/a> nesneler gelebilir. Evet tuple nesneler, yap\u0131sal olarak eldeki verileri ihtiya\u00e7 format\u0131nda temsil edebilen amma velakin bunu yaparken ortaya bir referanstan ziyade toplu eri\u015fim g\u00f6sterilebilir bir veri yap\u0131s\u0131 ortaya koyan yap\u0131lard\u0131r.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass Consumer\r\n{\r\n    public (string Name, string Id) GetConsumer()\r\n    {\r\n        return (&quot;XConsumer&quot;, &quot;12345667&quot;);\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Tuple olarak yarat\u0131lan bir nesnenin field&#8217;lar\u0131na tip g\u00fcvenli bir \u015fekilde eri\u015fim g\u00f6sterilebildi\u011fi i\u00e7in, evet, belki bu s\u00f6ylentiyi tuple nesneler i\u00e7in yapsayd\u0131n\u0131z hakl\u0131 olabilirdiniz.<\/p>\n<p>Velhas\u0131l kelam&#8230; C# programlama dili art\u0131k bu ihtiyac\u0131 .NET 8 ile gelmi\u015f olan<br \/>\n<code>global using<\/code> \u00f6zelli\u011fi sayesinde giderebilmektedir. Bizler bu yeni gelen \u00f6zellik sayesinde anl\u0131k olarak ve ba\u015fka hi\u00e7bir yerde kullan\u0131lmayacak referans davran\u0131\u015f\u0131na sahip bir t\u00fcr olu\u015fturabiliyor ve ihtiya\u00e7 do\u011frultusunda kullanabiliyoruz. Tabi esas\u0131nda bu \u00f6zellik ile \u00fcretilen referans\u0131n, \u00f6z\u00fcnde yine bir tuple nesne olarak \u015fekillendirildi\u011fini ba\u015ftan s\u00f6ylemekte fayda g\u00f6rmekteyim.<\/p>\n<p>\u015eimdi olay\u0131 \u015f\u00f6yle \u00f6rneklendirelim;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass PersonHelper\r\n{\r\n    public ? GetPerson()\r\n    {\r\n        string name = &quot;Gen\u00e7ay&quot;;\r\n        string surname = &quot;Y\u0131ld\u0131z&quot;;\r\n        int age = 32;\r\n        return ...\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Yukar\u0131daki kod blo\u011funa g\u00f6z atarsak e\u011fer &#8216;PersonHelper&#8217; i\u00e7erisindeki &#8216;GetPerson&#8217; metodunun belli ba\u015fl\u0131 verileri geriye d\u00f6nd\u00fcrmesi gerekti\u011fini g\u00f6zlemliyoruz. Burada, return s\u00fcrecinde t\u00fcm verileri ta\u015f\u0131yabilecek bir class ya da record referans\u0131 tasarlayabilece\u011fimiz gibi bir struct&#8217;ta olu\u015fturabiliriz. Ama bu i\u015flem uygulama seviyesinde sade ve sadece bu noktada ger\u00e7ekle\u015ftirilecekse ve ba\u015fka bir yerde bu referansa ihtiya\u00e7 duyulmayacaksa i\u015fte o taktirde tuple t\u00fcr\u00fcnden ya da .NET 8 ile gelmi\u015f olan <code>global using<\/code>&#8216;den istifade edebiliriz.<\/p>\n<p>\u015e\u00f6yle ki;<\/p>\n<p>Tuple ile \u00e7\u00f6z\u00fcm;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass PersonHelper\r\n{\r\n    public (string name, string surname, int age) GetPerson()\r\n    {\r\n        string name = &quot;Gen\u00e7ay&quot;;\r\n        string surname = &quot;Y\u0131ld\u0131z&quot;;\r\n        int age = 32;\r\n        return (name, surname, age);\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Bu muhtemelen bu g\u00fcne kadar a\u015fina oldu\u011fumuz \u00e7\u00f6z\u00fcmd\u00fcr.<\/p>\n<p><code>global using<\/code> ile \u00e7\u00f6z\u00fcm;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nglobal using Person = (\r\n    string Name,\r\n    string Surname,\r\n    int Age\r\n    );\r\n<\/pre>\n<\/div>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass PersonHelper\r\n{\r\n    public Person GetPerson()\r\n    {\r\n        string name = &quot;Gen\u00e7ay&quot;;\r\n        string surname = &quot;Y\u0131ld\u0131z&quot;;\r\n        int age = 32;\r\n        return new()\r\n        {\r\n            Name = name,\r\n            Surname = surname,\r\n            Age = age\r\n        };\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Bu ise i\u015fte mevzu bahis olan ve .NET 8 ile gelmi\u015f olan yeni bir davran\u0131\u015fsal \u00e7\u00f6z\u00fcmd\u00fcr. Burada <code>global using<\/code> ile tan\u0131mlanm\u0131\u015f olan &#8216;Person&#8217; t\u00fcr\u00fc uygulama seviyesinde her noktadan eri\u015filebilmekte ve \u00f6z\u00fcnde tuple davran\u0131\u015f sergilemektedir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2024\/07\/NET-8-global-using-Ile-Anlik-Type-Tanimlama.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2024\/07\/NET-8-global-using-Ile-Anlik-Type-Tanimlama.png\" alt=\".NET 8 - global using \u0130le Anl\u0131k Type Tan\u0131mlama\" width=\"703\" height=\"150\" class=\"aligncenter size-full wp-image-27080\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2024\/07\/NET-8-global-using-Ile-Anlik-Type-Tanimlama.png 703w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2024\/07\/NET-8-global-using-Ile-Anlik-Type-Tanimlama-300x64.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/a>B\u00f6ylece bizler bazen metotlarda geriye d\u00f6n\u00fclecek de\u011ferleri sadece o an\/o metot i\u00e7in olu\u015fturmak mecburiyetinde kald\u0131\u011f\u0131m\u0131z durumlarda <code>global using<\/code> ile anl\u0131k olarak ba\u015fka hi\u00e7bir yerde kullan\u0131lmayacak olan, \u00f6z\u00fcnde tuple t\u00fcrleri bu \u015fekilde yaratabiliyoruz. Tabi tuple t\u00fcr oldu\u011fu i\u00e7in kullan\u0131rken de new operat\u00f6r\u00fc yerine a\u015fa\u011f\u0131daki gibi tuple davran\u0131\u015f\u0131n\u0131 da bizzat uygulayabilirsiniz.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    internal class PersonHelper\r\n    {\r\n        public Person GetPerson()\r\n        {\r\n            string name = &quot;Gen\u00e7ay&quot;;\r\n            string surname = &quot;Y\u0131ld\u0131z&quot;;\r\n            int age = 32;\r\n            return (name, surname, age);\r\n        }\r\n    }\r\n<\/pre>\n<\/div>\n<p><u>Bu arada yanl\u0131\u015f anla\u015f\u0131lmas\u0131n, ba\u015fka hi\u00e7bir yerde kullan\u0131lmayacak derken isterseniz kullanabilirsiniz ama durumsal olarak bu yenili\u011fin bahsedilen tarz senaryolara \u00f6zg\u00fc davran\u0131\u015f\u0131n\u0131 vurgulamak i\u00e7in bu \u015fekilde bir tan\u0131mda bulunuyorum.<\/u><\/p>\n<p>Ayr\u0131ca ifade etmek istedi\u011fim bir durum daha var ki, o da <code>global using<\/code> ile olu\u015fturulan t\u00fcrlerin uygulama seviyesinde a\u015fa\u011f\u0131daki gibi ayr\u0131 bir dosyada tan\u0131mlanmas\u0131d\u0131r.<\/p>\n<p>Misal <em>global-using-types.cs<\/em>;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nglobal using Person = (\r\n    string Name,\r\n    string Surname,\r\n    int Age\r\n    );\r\n\r\nglobal using PersonFacebookStatistic = (\r\n    int Id,\r\n    long LikeCount,\r\n    long PostCount,\r\n    long FollowersCount\r\n    );\r\n\r\nglobal using PersonSalaryGraphicStatistic = (\r\n    string PersonType,\r\n    decimal SalaryRate\r\n    );\r\n\r\n.\r\n.\r\n.\r\n<\/pre>\n<\/div>\n<p>Bu \u015fekilde yap\u0131lan \u00e7al\u0131\u015fmalar\u0131n hem y\u00f6netilebilirlikleri daha da kolayla\u015ft\u0131r\u0131lmakta hem de derli toplu bir proje seyri s\u00f6z konusu olmaktad\u0131r. <\/p>\n<p>T\u00fcm bu anlat\u0131lanlar\u0131n yan\u0131nda C# 10 ile gelmi\u015f olan <a href=\"https:\/\/www.gencayyildiz.com\/blog\/c-10-global-using-directive\/\" rel=\"noopener\" target=\"_blank\">Global Using Directive<\/a> \u00f6zelli\u011fine de g\u00f6z atman\u0131z\u0131 tavsiye etmekteyim. Bu iki \u00f6zellik esas\u0131nda ayn\u0131 davran\u0131\u015fsal temele dayanmakta ve biz C# geli\u015ftiricilerinin icraat\u0131n\u0131 zenginle\u015ftirmektedir \ud83d\ude42<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, C# ile yapt\u0131\u011f\u0131m\u0131z geli\u015ftirmelerde bazen anl\u0131k olarak elimizdeki verileri b\u00fct\u00fcnsel olarak temsil edebilecek ve sadece o an i\u00e7in kullanaca\u011f\u0131m\u0131z bir t\u00fcre ihtiyac\u0131m\u0131z olabiliyor. Bu tarz durumlarda bir class, record ya da struct tan\u0131m\u0131ndan&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":26539,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4912,12],"tags":[4915,5076,5075],"class_list":["post-27074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-8","category-c-sharp-c","tag-net-8","tag-c-global-using","tag-global-using"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/27074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=27074"}],"version-history":[{"count":5,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/27074\/revisions"}],"predecessor-version":[{"id":27081,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/27074\/revisions\/27081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/26539"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=27074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=27074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=27074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}