{"id":1280,"date":"2018-06-21T23:47:43","date_gmt":"2018-06-21T23:47:43","guid":{"rendered":"http:\/\/sezeromer.com\/?p=1280"},"modified":"2023-02-26T23:48:22","modified_gmt":"2023-02-26T20:48:22","slug":"c-katmanli-mimari","status":"publish","type":"post","link":"https:\/\/sezeromer.com\/en\/c-katmanli-mimari\/","title":{"rendered":"C# N-Tier Architecture"},"content":{"rendered":"<p>Today, when I talk about a layered architect, I will go through an example at the same time. I will go through a sample frequently used in almost every major project.<\/p>\n<p><strong>Layered Architecture<\/strong> is a structure that enables our projects to be more compact, improve code readability, increase teamwork, and make error management easier. In fact, with this structure we have made the project writing a standard. This structure can be considered as a <strong>multi-layered architectural<\/strong> structure as it is formed from 3 main layers. But it actually stops on 3 main layers. These layers are;<\/p>\n<ul>\n<li>Data Layer<\/li>\n<li>Business Layer<\/li>\n<li>Presentation Layer<\/li>\n<\/ul>\n<p>Now let&#8217;s go on to explain these things.<\/p>\n<h2>Data Layer<\/h2>\n<p>This layer is also known by its name and we will deal with the database part. Database connections are made in this layer and classes are created for the tables in the database. Of course, we need to have our database for this. A sample database is available for this. I will use the <strong>Northwind<\/strong> database.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1283\" src=\"http:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/northwind.png\" alt=\"\" width=\"272\" height=\"604\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/northwind.png 272w, https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/northwind-135x300.png 135w\" sizes=\"(max-width: 272px) 100vw, 272px\" \/><\/p>\n<p>After adding the database, we will go to the code. For this we need to make a database connection first. After we write our link address, we open the link. Then we need to create a query to retrieve the data. We can extract data from different tables. That&#8217;s why we create such a function.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/ Ba\u011flant\u0131 adresimizi yaz\u0131p, ba\u011flant\u0131y\u0131 a\u00e7\u0131yoruz\r\n        public SqlConnection OpenConnection()\r\n        {\r\n            SqlConnection baglanti = new SqlConnection(\"Server=.;database=Northwind;trusted_connection=true\");\r\n            baglanti.Open();\r\n            return baglanti;\r\n        }\r\n        \/\/ Her sorgu olu\u015fturdu\u011fumuzda buradan yararlanaca\u011f\u0131z.\r\n        public SqlCommand CreateConnection(string sorgu)\r\n        {\r\n            SqlCommand sqlCommand = new SqlCommand(sorgu, OpenConnection());\r\n            return sqlCommand;\r\n        }<\/pre>\n<\/div>\n<p>We performed the database bind operations. We also made preparations to write queries when needed. Now we are going to work on which table in the database. If you are going to deal with all the tables, you have to add all the tables. I am adding this only because I will deal with the customer chart. I am adding this column to the database in the same way as it would be here. You can easily create it by typing &#8220;<strong>prop<\/strong>&#8220;. I later add a constructor by creating a <strong>Constructor<\/strong>.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public class Musteriler\r\n    {\r\n        public string MusteriID { get; set; }\r\n        public string SirketAdi { get; set; }\r\n        public string MusteriAdi { get; set; }\r\n        public string MusteriUnvani { get; set; }\r\n        public string Adres { get; set; }\r\n        public string Sehir { get; set; }\r\n        public string Bolge { get; set; }\r\n        public string PostaKodu { get; set; }\r\n        public string Ulke { get; set; }\r\n        public string Telefon { get; set; }\r\n        public string Faks { get; set; }\r\n\r\n        public Musteriler(string musteriID,string sirketAdi, string musteriAdi , string musteriunvani,\r\n            string adres, string sehir, string bolge, string postakodu,\r\n            string ulke,string telefon,string faks)\r\n        {\r\n            this.MusteriID = musteriID;\r\n            this.SirketAdi = sirketAdi;\r\n            this.MusteriAdi = musteriAdi;\r\n            this.MusteriUnvani = musteriunvani;\r\n            this.Adres = adres;\r\n            this.Sehir = sehir;\r\n            this.Bolge = bolge;\r\n            this.PostaKodu = postakodu;\r\n            this.Ulke = ulke;\r\n            this.Telefon = telefon;\r\n            this.Faks = faks;\r\n        }<\/pre>\n<\/div>\n<h2>Business Layer<\/h2>\n<p>In this layer, the main events begin. How we read the data from the database, what questions we will read, how we read the data, what we will do after reading the data. In fact, it provides a bridge between our database and our project.<\/p>\n<p>In this example I will just do data extraction. If you want to do other things later, we can go together. After creating a function called data check, I create a list so that I can keep the data in a list. Then I get the linking and query writing functions in the data layer to open the connection and query that we created in the data layer. Then I read the data. After I read the data, I am assigning a class called Customer. I add a customer list afterwards. That&#8217;s all my business here. You can perform basic <strong>CRUD<\/strong>\u00a0(Create &#8211; Read &#8211; Update &#8211; Delete ) operations on this layer.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">public List&lt;Musteriler&gt; TakeData()\r\n        {\r\n            \/\/ B\u00fct\u00fcn m\u00fc\u015fterileri bir liste i\u00e7inde tutaca\u011f\u0131m\r\n            List&lt;Musteriler&gt; musteriler = new List&lt;Musteriler&gt;();\r\n            Veri veritabani = new Veri();\r\n            SqlConnection connection = veritabani.OpenConnection();\r\n            SqlCommand sqlCommand = veritabani.CreateConnection(\"SELECT MusteriID,SirketAdi,MusteriAdi,\" +\r\n                \"MusteriUnvani,Adres,Sehir,Bolge,PostaKodu,Ulke,Telefon,Faks from Musteriler\");\r\n            SqlDataReader dataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);\r\n            while(dataReader.Read())\r\n            {\r\n                Musteriler musteri = new Musteriler(\r\n                    dataReader[\"MusteriID\"].ToString(),\r\n                    dataReader[\"SirketAdi\"].ToString(),\r\n                    dataReader[\"MusteriAdi\"].ToString(),\r\n                    dataReader[\"MusteriUnvani\"].ToString(),\r\n                    dataReader[\"Adres\"].ToString(),\r\n                    dataReader[\"Sehir\"].ToString(),\r\n                    dataReader[\"Bolge\"].ToString(),\r\n                    dataReader[\"PostaKodu\"].ToString(),\r\n                    dataReader[\"Ulke\"].ToString(),\r\n                    dataReader[\"Telefon\"].ToString(),\r\n                    dataReader[\"Faks\"].ToString());\r\n                musteriler.Add(musteri);\r\n            }\r\n\r\n            connection.Close();\r\n            return musteriler;\r\n      \r\n        }<\/pre>\n<\/div>\n<h2>Presentation Layer<\/h2>\n<p>In this layer, we present data to the user in our database. Dilerseniz mobile application, you do not want to do a console application in the windows form application. This is up to you. This is the layer where you will have more pages. We will reach the user in this layer. The user will start the data extraction process from here and will do basic operations here. I did a simple bit console application in this application, but as I said, you can do whatever you want.<\/p>\n<p>Here I first get help from my class at the Job Layer to take the data. We already did everything we needed to attract data at the business layer. I&#8217;m making a list. This listenin type will be in Customer style. To retrieve the Customer list from the business layer. Then we get the customers coming from the business layer to this list. We need to go through this list after we get it. We navigate with Foreach and print it on the screen. That&#8217;s our process.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;swift&quot;,&quot;mime&quot;:&quot;text\/x-swift&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:true,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">List&lt;Musteriler&gt; musteriListe = new List&lt;Musteriler&gt;();\r\n            Helper helper = new Helper();\r\n            musteriListe =  helper.TakeData();\r\n            foreach (Musteriler item in musteriListe)\r\n            {\r\n                Console.WriteLine(item.MusteriID + \" \" + item.SirketAdi + \" \" + item.MusteriAdi + \" \" + item.MusteriUnvani + \" \" +\r\n                    item.Adres + \" \" + item.Sehir + \" \" + item.Bolge + \" \" + item.PostaKodu + \" \" + item.Ulke + \" \" +\r\n                    item.Telefon + \" \" + item.Faks);\r\n            }\r\n            Console.ReadKey();<\/pre>\n<\/div>\n<p>Our display is as below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1286\" src=\"http:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari.png\" alt=\"\" width=\"1602\" height=\"816\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari.png 1602w, https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari-300x153.png 300w, https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari-768x391.png 768w, https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari-1024x522.png 1024w\" sizes=\"(max-width: 1602px) 100vw, 1602px\" \/><\/p>\n<p>The reason for the layered architecture is that each process is performed in different layers. I went through this project in one project and stored them in different folders. You can create different projects for each layer you are baking.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1287\" src=\"http:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari-2.png\" alt=\"\" width=\"390\" height=\"307\" srcset=\"https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari-2.png 390w, https:\/\/sezeromer.com\/wp-content\/uploads\/2018\/06\/katmanli-mimari-2-300x236.png 300w\" sizes=\"(max-width: 390px) 100vw, 390px\" \/><\/p>\n<p>If you want to reach the project you can reach <a href=\"http:\/\/If you want to reach the project you can reach here.\">here<\/a>.<\/p>\n<p>If you have any questions, you can contact me by email or comment.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Today, when I talk about a layered architect, I will go through an example at the same time. I will go through a sample frequently used in almost every major project. Layered Architecture is a structure that enables our projects to be more compact, improve code readability, increase teamwork, and make error management easier. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1289,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[500,493,23,507,506,496,501,498,494,499,495,476,504,502,503,13,505,497],"class_list":["post-1280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-ile-ilgili-yazilar","tag-business","tag-c-sharp","tag-c","tag-console","tag-crud","tag-data","tag-is","tag-katman","tag-katmanli","tag-layer","tag-mimari","tag-mobile","tag-northwind","tag-presentation","tag-standart","tag-sunum","tag-tabani","tag-veri"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1280"}],"collection":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":12,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":2595,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/posts\/1280\/revisions\/2595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media\/1289"}],"wp:attachment":[{"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sezeromer.com\/en\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}