﻿
{"id":16049,"date":"2020-05-27T15:35:50","date_gmt":"2020-05-27T15:35:50","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=16049"},"modified":"2020-05-27T15:35:50","modified_gmt":"2020-05-27T15:35:50","slug":"odata-action-function-yapilanmasi-ile-custom-metot-olusturma","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/odata-action-function-yapilanmasi-ile-custom-metot-olusturma\/","title":{"rendered":"OData &#8211; Action\/Function Yap\u0131lanmas\u0131 \u0130le Custom Metot Olu\u015fturma"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>OData ile url \u00fczerinden yap\u0131lan sorgulamay\u0131 kar\u015f\u0131layacak actionlar\u0131 belirlerken belli kurallar \u00e7er\u00e7evesinde hareket etmekteyiz. Bu kurallar\u0131n neler oldu\u011funu <a href=\"https:\/\/www.gencayyildiz.com\/blog\/odata-davranis-modellemesi-ve-attributelari\/\" target=\"_blank\" rel=\"noopener noreferrer\">OData Davran\u0131\u015f Modellemesi ve Attributelar\u0131<\/a> ve <a href=\"https:\/\/www.gencayyildiz.com\/blog\/odata-custom-route-yapilanmasi\/\" target=\"_blank\" rel=\"noopener noreferrer\">OData Custom Route Yap\u0131lanmas\u0131<\/a> ba\u015fl\u0131kl\u0131 makalelerde detayl\u0131ca incelemi\u015f bulunmaktay\u0131z. Bu i\u00e7eri\u011fimizde ise OData&#8217;n\u0131n bizlere sundu\u011fu kurallar\u0131 a\u015fmam\u0131z\u0131 ve custom metotlar olu\u015fturmam\u0131z\u0131 sa\u011flayacak olan Action\/Function yap\u0131lanmalar\u0131n\u0131 inceliyor olaca\u011f\u0131z.<\/p>\n<h3>OData Action\/Function Nedir?<\/h3>\n<p>Custom metot olu\u015fturmam\u0131z\u0131 sa\u011flayan yap\u0131lard\u0131r. Ba\u015fl\u0131ktan da anla\u015f\u0131ld\u0131\u011f\u0131 \u00fczere Action ve Function olmak \u00fczere ikiye ayr\u0131lmaktad\u0131rlar. Aralar\u0131ndaki fark\u0131 anlayabilmek i\u00e7in <em><a href=\"https:\/\/www.gencayyildiz.com\/blog\/yazilimda-side-effectyan-etki-kavrami-nedir\/\" target=\"_blank\" rel=\"noopener noreferrer\">Side Effect(Yan Etki)<\/a><\/em> kavram\u0131na hakim olunmas\u0131 i\u015fimizi kolayla\u015ft\u0131racakt\u0131r.<\/p>\n<p>K\u0131saca side effect; bir i\u015flem yap\u0131l\u0131rken kapsam\u0131 d\u0131\u015f\u0131ndaki bir de\u011fi\u015fkenin, de\u011ferin veya objenin de\u011fi\u015ftirilmesi durumudur. Salt olarak OData ile ilgili bir kavram olmaktan ziyade dolayl\u0131 yoldan OData&#8217;y\u0131 da ilgilendiren bir anlama sahiptir. \u0130\u015fte bu kavram\u0131n \u0131\u015f\u0131\u011f\u0131nda Action ve Function yap\u0131lanmas\u0131n\u0131 birbirlerinden ay\u0131rabilmekteyiz;<\/p>\n<table>\n<thead>\n<tr>\n<th>Action<\/th>\n<th>Function<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"margin-top: 0px;\" width=\"50%;\">Yan etkisi olan(side effect) OData operasyonlar\u0131 i\u00e7in kullan\u0131l\u0131r.<\/p>\n<p>Genel olarak <span style=\"vertical-align: top;\">POST<\/span>, <span style=\"color: red;\">PUT<\/span> ve <span style=\"color: red;\">DELETE<\/span> operasyonlar\u0131 i\u015flevsel olarak yan etkiye sahip olduklar\u0131ndan dolay\u0131 Action olarak tan\u0131mlanmal\u0131d\u0131rlar.<\/p>\n<p>Ayr\u0131ca OData protokol\u00fc \u00fczerinden complex type veri al\u0131\u015f veri\u015finde bulunulacaksa kesinlikle Action tercih edilmelidir.<\/p>\n<p>Veri ak\u0131\u015f\u0131nda g\u00f6nderilen datalar\u0131 &#8216;<span style=\"color: blue;\">ODataActionParameters<\/span>&#8216; t\u00fcr\u00fcnden parametreyle yakalamaktad\u0131r.<\/td>\n<td style=\"vertical-align: top;\">Yan etkisi olmayan(no side effect) OData operasyonlar\u0131 i\u00e7in kullan\u0131l\u0131r. \u0130deal olarak yap\u0131lacak olan t\u00fcm <span style=\"color: red;\">GET<\/span> i\u015flemlerinde kullan\u0131lmas\u0131 tavsiye edilir.<\/p>\n<p>Veri ak\u0131\u015f\u0131nda g\u00f6nderilen datalar\u0131 &#8216;<span style=\"color: blue;\">FromODataUri<\/span>&#8216; attribute&#8217;u ile i\u015faretlenmi\u015f parametreyle yakalamaktad\u0131r.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u015eimdi gelin s\u0131ras\u0131yla her iki yap\u0131lanmay\u0131 pratikte inceleyelim;<\/p>\n<h3 style=\"color: red;\">Action Yap\u0131lanmas\u0131yla Custom Metot Olu\u015fturma<\/h3>\n<p>Action yap\u0131lanmas\u0131yla olu\u015fturulacak metotlar\u0131n temel ay\u0131rt edici \u00f6zellikleri POST, PUT veya DELETE i\u015flevlerine sahip olmas\u0131d\u0131r. Dolay\u0131s\u0131yla t\u00fcm bu i\u015flevleri kar\u015f\u0131layacak POST t\u00fcr\u00fcnden istekler olu\u015fturacaksan\u0131z action yap\u0131lanmas\u0131n\u0131 tercih edebilirsiniz. Action yap\u0131lanmas\u0131 olu\u015fturulacak custom metodun parametre al\u0131p almamas\u0131na g\u00f6re ikiye ayr\u0131lmaktad\u0131r;<\/p>\n<h4 style=\"color: orange;\">Parametre Almayan Metot<\/h4>\n<p>Action ile parametre almayan metot geli\u015ftirebilmek i\u00e7in \u00f6ncelikle &#8216;Startup.cs&#8217; dosyas\u0131ndaki &#8216;Configure&#8217; metodu i\u00e7erisinde a\u015fa\u011f\u0131daki tan\u0131mlaman\u0131n yap\u0131lmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)\r\n        {\r\n            .\r\n            .\r\n            .\r\n            var builder = new ODataConventionModelBuilder();\r\n            builder.EntitySet&lt;Personel&gt;(&quot;Personel&quot;);\r\n            builder.EntitySet&lt;Kategori&gt;(&quot;Kategori&quot;);\r\n            builder.EntitySet&lt;Satis&gt;(&quot;Satis&quot;);\r\n\r\n            builder.EntityType&lt;Personel&gt;().Action(&quot;ToplamSatis&quot;)\r\n                .Returns&lt;int&gt;();\r\n\r\n            app.UseEndpoints(endpoints =&gt;\r\n            {\r\n                endpoints.MapODataRoute(&quot;odata&quot;, &quot;odata&quot;, builder.GetEdmModel());\r\n                endpoints.OrderBy().Expand().Select().MaxTop(50).Count();\r\n            });\r\n            .\r\n            .\r\n            .\r\n        }\r\n<\/pre>\n<p><strong><em>11.<\/em><\/strong> sat\u0131rdaki &#8216;EntityType&#8217; fonksiyonuna g\u00f6z atarsan\u0131z e\u011fer &#8216;Personel&#8217; entitysi ile ili\u015fkilendirilmi\u015f controller i\u00e7erisinde, &#8216;Action&#8217; metodu ile &#8216;ToplamSatis&#8217; isminde geriye int t\u00fcr\u00fcnden de\u011fer d\u00f6nd\u00fcrecek bir custom metot bildiriminde bulunulmu\u015ftur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class PersonelController : ODataController\r\n    {\r\n        .\r\n        .\r\n        .\r\n        &#x5B;HttpPost]\r\n        public IActionResult ToplamSatis(&#x5B;FromODataUri] int key)\r\n        {\r\n            var personel = _northwindContext.Personeller.Include(p =&gt; p.Satislar).FirstOrDefault(p =&gt; p.PersonelId == key);\r\n            return Ok(personel.Satislar.Count());\r\n        }\r\n    }\r\n<\/pre>\n<p>Bildirimde bulunulan metot ilgili controllerda &#8216;HttpPost&#8217; attribute&#8217;u ile i\u015faretlenerek tan\u0131mlanm\u0131\u015ft\u0131r. Burada tan\u0131mlanan &#8216;int key&#8217; parametresi a\u015fa\u011f\u0131da kullan\u0131l\u0131rken de g\u00f6rece\u011finiz \u00fczere metodun de\u011fil entitynin parametresidir.<\/p>\n<p><strong>Kullan\u0131m\u0131<\/strong><br \/>\nTan\u0131mlanan bu custom metodu kullanabilmek i\u00e7in <code style=\"color:red;\"><strong>https:\/\/localhost:5001\/odata\/<span style=\"color: blue;\">personel(1)<\/span>\/toplamsatis<\/strong><\/code> \u015feklinde bir url \u00fczerinden sorgulama yap\u0131lmas\u0131 gerekmektedir. Buradaki tan\u0131mlamada &#8216;EntityType&lt;Personel&gt;&#8217; metodu ile bir bildirim oldu\u011fundan dolay\u0131 &#8216;ToplamSatis&#8217; metoduna &#8216;Personel&#8217; entitysinin controller\u0131 \u00fczerinden eri\u015filmek mecburiyetindedir. Dolay\u0131s\u0131yla url&#8217;de mavi ile vurgulanan k\u0131s\u0131m ilgili controller&#8217;\u0131 tarif etmektedir. Ayr\u0131ca &#8216;<span style=\"color: blue;\">personel(1)<\/span>&#8216; ifadesindeki id de\u011feri yukar\u0131da tan\u0131mlama yaparken bahsedildi\u011fi \u00fczere olu\u015fturulan custom metodun parametresi de\u011fil entitynin parametresini ifade etmektedir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma.jpg\" alt=\"OData - Action\/Function Yap\u0131lanmas\u0131 \u0130le Custom Metot Olu\u015fturma\" width=\"430\" height=\"337\" class=\"aligncenter size-full wp-image-16097\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma.jpg 430w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-300x235.jpg 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/a><\/p>\n<p>E\u011fer ki entityde parametre almaks\u0131z\u0131n t\u00fcm entity nesneleri \u00fczerinde i\u015flem yapmak istiyorsan\u0131z a\u015fa\u011f\u0131daki gibi bir bildirimde bulunman\u0131z gerekmektedir;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            .\r\n            .\r\n            .\r\n            builder.EntityType&lt;Personel&gt;()\r\n                .Collection\r\n                .Action(&quot;ToplamSatis&quot;)\r\n                .Returns&lt;int&gt;();\r\n            .\r\n            .\r\n            .\r\n<\/pre>\n<p>Burada dikkat ederseniz &#8221;EntityType&lt;Personel&gt;&#8217; metodundan sonra t\u00fcm koleksiyon verileri \u00fczerinde \u00e7al\u0131\u015faca\u011f\u0131z anlam\u0131na gelen &#8216;Collection&#8217; propertysi \u00e7a\u011fr\u0131lmakta, ard\u0131ndan &#8216;Action&#8217; metoduyla operatif ad\u0131mlara devam edilmektedir.<\/p>\n<p>Hemen ard\u0131ndan bildirime uygun metodu ilgili controllerda tan\u0131mlayal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        .\r\n        .\r\n        .\r\n        &#x5B;HttpPost]\r\n        public IActionResult ToplamSatis()\r\n        {\r\n            var personel = _northwindContext.Personeller.Include(p =&gt; p.Satislar).ToList();\r\n            return Ok(personel.Sum(x =&gt; x.Satislar.Count));\r\n        }\r\n        .\r\n        .\r\n        .\r\n<\/pre>\n<p>\u0130\u015fte bu tarz bir metoda a\u015fa\u011f\u0131daki gibi istekte bulunulmas\u0131 yeterli olacakt\u0131r.<br \/>\n<code style=\"color:red;\"><strong>https:\/\/localhost:5001\/odata\/<span style=\"color: blue;\">personel<\/span>\/toplamsatis<\/strong><\/code><br \/>\nHaliyle bu url \u00fczerinden yap\u0131lan istekler \u00f6rne\u011fimize g\u00f6re herhangi bir personelin de\u011fil t\u00fcm personellerin toplam sat\u0131\u015f adedini bizlere getirecektir.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-1.jpg\" alt=\"OData - Action\/Function Yap\u0131lanmas\u0131 \u0130le Custom Metot Olu\u015fturma\" width=\"378\" height=\"336\" class=\"aligncenter size-full wp-image-16101\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-1.jpg 378w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-1-300x267.jpg 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/a><\/p>\n<h4 style=\"color: orange;\">Parametre Alan Metot<\/h4>\n<p>Benzer mant\u0131kla action yap\u0131lanmas\u0131yla olu\u015fturulan custom metoda a\u015fa\u011f\u0131daki gibi parametrelerde tan\u0131mlanabilmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            .\r\n            .\r\n            .\r\n            var builder = new ODataConventionModelBuilder();\r\n            builder.EntitySet&lt;Personel&gt;(&quot;Personel&quot;);\r\n            builder.EntitySet&lt;Kategori&gt;(&quot;Kategori&quot;);\r\n            builder.EntitySet&lt;Satis&gt;(&quot;Satis&quot;);\r\n\r\n            var toplamSatis = builder.EntityType&lt;Personel&gt;()\r\n                .Action(&quot;ToplamSatis&quot;)\r\n                .Returns&lt;int&gt;();\r\n            toplamSatis.Parameter&lt;int&gt;(&quot;nakliyeUcreti&quot;);\r\n            toplamSatis.Parameter&lt;string&gt;(&quot;satisTarihi&quot;);\r\n            .\r\n            .\r\n            .\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funa g\u00f6z atarsan\u0131z e\u011fer &#8216;nakliyeUcreti&#8217; ve &#8216;satisTarihi&#8217; olmak \u00fczere iki adet parametre alan ve geriye int t\u00fcr\u00fcnden de\u011fer d\u00f6nd\u00fcren &#8216;ToplamSatis&#8217; isimli metot bildiriminde bulunulmu\u015ftur. Bu bildirim neticesinde &#8216;Personel&#8217; controller&#8217;a gelip a\u015fa\u011f\u0131daki gibi bir metot tan\u0131mland\u0131\u011f\u0131nda;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        .\r\n        .\r\n        .\r\n        &#x5B;HttpPost]\r\n        public IActionResult ToplamSatis(&#x5B;FromODataUri] int key, ODataActionParameters @params)\r\n        {\r\n            var satislar = _northwindContext.Satislar.Where(s =&gt; s.PersonelId == key &amp;&amp; s.SatisTarihi.Value == DateTime.Parse(@params&#x5B;&quot;satisTarihi&quot;].ToString()) &amp;&amp; s.NakliyeUcreti.Value &gt;= int.Parse(@params&#x5B;&quot;nakliyeUcreti&quot;].ToString())).ToList();\r\n\r\n            return Ok(satislar.Count());\r\n        }\r\n        .\r\n        .\r\n        .\r\n<\/pre>\n<p><code style=\"color:red;\"><strong>https:\/\/localhost:5001\/odata\/<span style=\"color: blue;\">personel(1)<\/span>\/toplamsatis<\/strong><\/code> url&#8217;i \u00fczerinden istek yap\u0131labilecektir. Metot bildiriminde tan\u0131mlanan ilgili parametrelerin de\u011ferleri url&#8217;e yap\u0131lacak POST neticesinde body \u00fczerinden g\u00f6nderilecektir ve bu de\u011ferler &#8216;ODataActionParameters&#8217; t\u00fcr\u00fcnden parametreyle yakalanacakt\u0131rlar.<\/p>\n<p>\u015e\u00f6yle ki;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-2.jpg\" alt=\"OData - Action\/Function Yap\u0131lanmas\u0131 \u0130le Custom Metot Olu\u015fturma\" width=\"557\" height=\"424\" class=\"aligncenter size-full wp-image-16118\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-2.jpg 557w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-2-300x228.jpg 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-3.jpg\" alt=\"OData - Action\/Function Yap\u0131lanmas\u0131 \u0130le Custom Metot Olu\u015fturma\" width=\"1058\" height=\"155\" class=\"aligncenter size-full wp-image-16120\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-3.jpg 1058w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-3-300x44.jpg 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-3-1024x150.jpg 1024w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-3-768x113.jpg 768w\" sizes=\"auto, (max-width: 1058px) 100vw, 1058px\" \/><\/a><\/p>\n<p>Burada \u00f6nem arz eden bir hususa dikkatinizi \u00e7ekmek istiyorum. Yukar\u0131daki gibi tan\u0131mlad\u0131\u011f\u0131n\u0131z parametrelerde tarihsel veriler i\u00e7in DateTime vs. gibi struct t\u00fcrler belirtmek yerine &#8216;string&#8217; kullanman\u0131z\u0131 tavsiye ederim. Aksi taktirde &#8216;ODataActionParameters&#8217; parametresi de\u011ferleri yakalayamayacak ve null d\u00f6necektir.<\/p>\n<h3 style=\"color: red;\">Function Yap\u0131lanmas\u0131yla Custom Metot Olu\u015fturma<\/h3>\n<p>Function yap\u0131lanmas\u0131nda ise action&#8217;a nazaran sadece &#8216;GET&#8217; isteklerini kar\u015f\u0131layacak metotlar\u0131n bildirilmesi tercih edilmelidir. T\u0131pk\u0131 action&#8217;da oldu\u011fu gibi parametre alan ve almayan olmak \u00fczere ikiye ayr\u0131lmaktad\u0131r.<\/p>\n<h4 style=\"color: orange;\">Parametre Almayan Metot<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            .\r\n            .\r\n            .\r\n            var builder = new ODataConventionModelBuilder();\r\n            builder.EntitySet&lt;Personel&gt;(&quot;Personel&quot;);\r\n            builder.EntitySet&lt;Kategori&gt;(&quot;Kategori&quot;);\r\n            builder.EntitySet&lt;Satis&gt;(&quot;Satis&quot;);\r\n\r\n            builder.EntityType&lt;Personel&gt;()\r\n               .Function(&quot;ToplamSatis&quot;)\r\n               .Returns&lt;int&gt;();\r\n            .\r\n            .\r\n            .\r\n<\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere function ile custom metot bildiriminde actiondan tek fark\u0131 metot isminin &#8216;Function&#8217; isimli metot i\u00e7erisinde belirtilmesidir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        .\r\n        .\r\n        .\r\n        &#x5B;HttpGet]\r\n        public IActionResult ToplamSatis(&#x5B;FromODataUri] int key)\r\n        {\r\n            var satislar = _northwindContext.Satislar.Where(s =&gt; s.PersonelId == key).ToList();\r\n            return Ok(satislar.Count());\r\n        }\r\n        .\r\n        .\r\n        .\r\n<\/pre>\n<p>Ayriyetten tan\u0131mlamada da ilgili metodun GET t\u00fcr\u00fcnden oldu\u011fu bildirilmelidir. Bu i\u015flem neticesinde <code style=\"color:red;\"><strong>https:\/\/localhost:5001\/odata\/<span style=\"color: blue;\">personel(1)<\/span>\/toplamsatis<\/strong><\/code> \u015feklinde yap\u0131lacak GET iste\u011fi ilgili metodu tetikleyecektir. Burada &#8216;int key&#8217; parametresi actiondakiyle birebir ayn\u0131 mant\u0131ktad\u0131r ve yazd\u0131\u011f\u0131m\u0131z custom metodun de\u011fil entitynin parametresidir. E\u011fer ki entity&#8217;de parametre tan\u0131mlamak istemiyorsan\u0131z actiondaki gibi &#8216;Collection&#8217; propertysini kullanabilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            .\r\n            .\r\n            .\r\n            builder.EntityType&lt;Personel&gt;()\r\n               .Collection\r\n               .Function(&quot;ToplamSatis&quot;)\r\n               .Returns&lt;int&gt;();\r\n            .\r\n            .\r\n            .\r\n<\/pre>\n<h4 style=\"color: orange;\">Parametre Alan Metot<\/h4>\n<p>Function ile tan\u0131mlanan custom metotlarda da parametreler actiondakiyle birebir ayn\u0131 tan\u0131mlanabilmektedirler.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            .\r\n            .\r\n            .\r\n            var builder = new ODataConventionModelBuilder();\r\n            builder.EntitySet&lt;Personel&gt;(&quot;Personel&quot;);\r\n            builder.EntitySet&lt;Kategori&gt;(&quot;Kategori&quot;);\r\n            builder.EntitySet&lt;Satis&gt;(&quot;Satis&quot;);\r\n\r\n            var toplamSatis = builder.EntityType&lt;Personel&gt;()\r\n                .Function(&quot;ToplamSatis&quot;)\r\n                .Returns&lt;int&gt;();\r\n            toplamSatis.Parameter&lt;int&gt;(&quot;nakliyeUcreti&quot;);\r\n            toplamSatis.Parameter&lt;string&gt;(&quot;satisTarihi&quot;);\r\n            .\r\n            .\r\n            .\r\n<\/pre>\n<p>Lakin parametre de\u011ferlerini yakalayabilmek i\u00e7in a\u015fa\u011f\u0131daki gibi &#8216;FromODataUri&#8217; attribute&#8217;u ile i\u015faretlenmi\u015f fiziksel parametrelerin ilgili metot imzas\u0131nda tan\u0131mlanmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        .\r\n        .\r\n        .\r\n        &#x5B;HttpGet]\r\n        public IActionResult ToplamSatis(&#x5B;FromODataUri] int key, &#x5B;FromODataUri] int nakliyeUcreti, &#x5B;FromODataUri] string satisTarihi)\r\n        {\r\n            var satislar = _northwindContext.Satislar.Where(s =&gt; s.PersonelId == key &amp;&amp; s.SatisTarihi.Value == DateTime.Parse(satisTarihi) &amp;&amp; s.NakliyeUcreti.Value &gt;= nakliyeUcreti).ToList();\r\n            return Ok(satislar.Count());\r\n        }\r\n        .\r\n        .\r\n        .\r\n<\/pre>\n<p>Bu \u015fekilde function yap\u0131lanmas\u0131 ile olu\u015fturulan metotlar\u0131 tetikleyebilmek i\u00e7in<br \/>\n<code style=\"color:red;\"><strong>https:\/\/localhost:5001\/odata\/<span style=\"color: blue;\">personel(1)<\/span>\/toplamsatis<span style=\"color:blue;\">(nakliyeUcreti=10, satisTarihi='1997-01-01 00:00:00.000')<\/span><\/strong><\/code> \u015feklinde bir url kullan\u0131lmas\u0131 gerekmektedir.<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-4.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-4.jpg\" alt=\"OData - Action\/Function Yap\u0131lanmas\u0131 \u0130le Custom Metot Olu\u015fturma\" width=\"664\" height=\"347\" class=\"aligncenter size-full wp-image-16139\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-4.jpg 664w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/05\/OData-ActionFunction-Yap\u0131lanmas\u0131-\u0130le-Custom-Metot-Olu\u015fturma-4-300x157.jpg 300w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/a><\/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, OData ile url \u00fczerinden yap\u0131lan sorgulamay\u0131 kar\u015f\u0131layacak actionlar\u0131 belirlerken belli kurallar \u00e7er\u00e7evesinde hareket etmekteyiz. Bu kurallar\u0131n neler oldu\u011funu OData Davran\u0131\u015f Modellemesi ve Attributelar\u0131 ve OData Custom Route Yap\u0131lanmas\u0131 ba\u015fl\u0131kl\u0131 makalelerde detayl\u0131ca incelemi\u015f bulunmaktay\u0131z.&#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":15889,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3516],"tags":[3549,3546,3548,3550,3553,3551,3554,3552,3540,3542,3547,3541],"class_list":["post-16049","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata","tag-c-side-effect","tag-c-side-effect-nedir","tag-c-yan-etki-nedir","tag-no-side-effect","tag-odata-action","tag-odata-action-vs-function","tag-odata-custom-metot","tag-odata-function","tag-side-effect","tag-side-effect-nedir","tag-side-effests","tag-yan-etki"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/16049","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=16049"}],"version-history":[{"count":67,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/16049\/revisions"}],"predecessor-version":[{"id":16140,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/16049\/revisions\/16140"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/15889"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=16049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=16049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=16049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}