<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Safiye Kaytan on Medium]]></title>
        <description><![CDATA[Stories by Safiye Kaytan on Medium]]></description>
        <link>https://medium.com/@safiyekaytan?source=rss-37b7cc400e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xGFhuPqA3ZB-RJGspTHFXg.jpeg</url>
            <title>Stories by Safiye Kaytan on Medium</title>
            <link>https://medium.com/@safiyekaytan?source=rss-37b7cc400e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:32:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@safiyekaytan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating an MVC project with .net n-tier architecture and Data-First approach]]></title>
            <link>https://medium.com/@safiyekaytan/creating-an-mvc-project-with-net-n-tier-architecture-and-data-first-approach-f0c18991f746?source=rss-37b7cc400e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0c18991f746</guid>
            <dc:creator><![CDATA[Safiye Kaytan]]></dc:creator>
            <pubDate>Sun, 30 Mar 2025 21:03:34 GMT</pubDate>
            <atom:updated>2025-04-10T06:03:41.636Z</atom:updated>
            <content:encoded><![CDATA[<h3>Creating an MVC project with .net n-tier architecture and DataFirst approach</h3><p>Hi,</p><p>When developing a project with .net, when I use sql server, I use 2 approaches: data first and code first.</p><p>In this article, I will explain data first.</p><p>Data first is a project development method where we import the models and contexts of the tables that are already in the database into our project. It provides flexibility in the data section, we can easily transfer any changes in the tables to our project.<br>I am sharing the steps I follow step by step regarding the automatic creation process of classes such as Context and Model while connecting these projects that I developed using C# to SQL Server Management Studio (MSSQL).<br>We will use N-Tier architecture in our .Net project. I will talk about it at the end of the article.</p><p>So, we can start creating the project.</p><p><strong>Step — 1</strong></p><p>First, select the Asp .Net Core Web App (Model-View-Controller) project template from Visual Studio.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*swfy-9SiQcbmo-uY5MHdLQ.png" /></figure><p>If you cannot see the specified template, you should install the necessary dependencies from ‘Visual Studio Installer’.</p><p>You can create your project with the default settings by saying Next, Next, but generally the latest .NET version is used for other packages to work without errors.</p><p><strong>Step — 2</strong></p><p>After successfully creating your .NET project, we will install the ‘Entity’ packages to work with SQL. You can reach the area where we will install the packages by typing ‘Manage Nuget Package for Solutions’ in the search bar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Ug3nS2B2eTrmMsEE6bndg.png" /></figure><p>The packages we need:<br>Microsoft.EntityFrameworkCore<br>Microsoft.EntityFrameworkCore.Design<br>Microsoft.EntityFrameworkCore.SqlServer<br>Microsoft.EntityFrameworkCore.Tools</p><p>You can also download these packages from the Package Manager Console as “Install-Package PackageName”.</p><p><strong>Step — 3</strong></p><p>It’s time to transfer our previously created tables in SQL Server to our project. For this, we will use the Scaffold Context command.</p><p>Don’t forget! If you want to use the Scaffold Context command again for any changes in your tables, you must add -Force to the end.</p><p>The reason we use Force can be thought of as ‘overwrite’. You cannot use this option, existing files will not be overwritten and you will get an error. The command prevents conflicts.</p><p>You need to paste the scaffold-context command below into the package manager console.</p><p>If you are not working on localhost, you should also add the UserId and Password variables to the connection string section.</p><p>Remember, if there are any errors in your project, the command will not work. I recommend you to build first and then run the command.</p><p><strong><em>Scaffold-DbContext “Server=localhost\SQLEXPRESS;Database=User;TrustServerCertificate=True;MultipleActiveResultSets=True;Integrated Security=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -ContextDir Context -Context UserContext -Tables UserInfoTable, SchoolTable</em></strong></p><p>“OutputDir Models” indicates the path to your Model folder.</p><p>“-ContextDir Context -Context UserContext” indicates the Context path.</p><p>“-Tables UserInfoTable, SchoolTable” if you can pull the tables you want from the Database, if you do not specify a table name, all tables will come.</p><p>If the command ran successfully, the context and models should look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y9bLDlZVIlHJCeg-J6-stg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8iBdjCjlC71x1FhwGjxx6A.png" /></figure><p>With the data-first approach, we transferred the models of the tables in the database to our project. If you have successfully reached this step, you can use the tables with linq queries. If you want to increase the performance of our project by using an N-tiered architecture, I recommend that you review the other steps.</p><p><strong>Step — 4</strong></p><p>We will create the files Repository, Service, IRepository, IServices.<br>Create 3 folders named Repositories, Services, Interfaces in the project main directory. (Folding structure may vary).<br>Create two folders named IRepository and IServices under the Interfaces folder.<br>The items we will create will increase according to the number of databases, we will continue as if we were working with a single database.<br>Create the file UserRepository.cs under the Repositories folder.<br>Create the file UserService.cs under the Services folder.<br>Create the file IUserRepository.cs under the IRepository folder.<br>Create the file IUserService.cs under the IServices folder.<br>Your folder structure should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*BZjM4BjDw1iwTvd2T_cNyw.png" /></figure><p>I will show the contents of the Service and Repository classes later.</p><p><strong>Step —5</strong></p><p>Add connection strings to appsetting.json file.</p><p>Your connection string may change depending on the authentication method. We will use ConnectionString_User assuming you are working on localhost.</p><p>At this point, my suggestion is that if you are not working on localhost, you should keep your username and password hash.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ec8OrbEa0NRYw-wEAEstxA.png" /></figure><p><strong>Step — 6</strong></p><p>Repository:</p><p>The name of the structure we will create in our Repository class is Constructor Injection. This is a type of Dependency Injection (DI). In this type of DI, dependencies are provided through the constructor of the class. For example, here the UserRepository class is dependent on the UserContext object and this dependency is provided from outside with the constructor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iWVr6VJKxGLlYQSWvDyBug.png" /></figure><p><strong>Step — 7</strong></p><p>Service</p><p>The public UserService(IUserRepository repository) constructor takes an object of type IUserRepository and assigns this object to a field of the class, _repository.</p><p>Thanks to this structure, the UserService class gets the IUserRepository object it needs directly from the constructor, thus making the class more flexible and testable by taking its dependencies from outside.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nshjz8DCSrWbH0ICSujDFg.png" /></figure><p><strong>Step — 8</strong></p><p>Program.cs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YAc5vzGH4fcIuOM5jf-RSw.png" /></figure><p>We define repository, irepository, service iservice injections by calling the connection string we wrote in Appsetting.json.</p><p>Our project is ready to be built.</p><p><strong>Finally</strong></p><p>After the project is successfully compiled, you can perform select-insert-update-delete operations. For this, I will briefly talk about the n-tier architecture.</p><p>N-Tier Architecture is a design approach that divides a software system into independent layers. Each layer performs a specific task and tries to be minimally dependent on other layers. The aim is to increase modularity, reduce code duplication, increase testability and provide ease of maintenance.<br>N-Tier Architecture ensures that security vulnerabilities are isolated at one point in the application and do not spread to other layers. This structure:<br>Prevents security threats (SQL Injection, XSS, etc.).<br>Protects sensitive data (passwords, personal information).<br>Centralizes authorization and authentication processes.<br>Makes the code more controllable and traceable.<br>A typical N-tier architecture in .NET applications consists of the following layers:<br>1- Presentation Layer:<br>View, Controller<br>2- Business Logic Layer (Repository):<br>UserRepository.cs<br>3- Business Logic Layer (Service):<br>UserService.cs<br>4- Data Access Layer:<br>UserContext.cs<br>5- Entities/Models Layer:<br>SchoolTable.cs, UserInfoTable.cs</p><p>In summary: In the Repository, we only perform select-insert-update-delete operations. In the Service, we process the data returned from these methods and try to send the data to the controller in the cleanest way possible. In an mvc project, we try to keep the methods in the controller short. The purpose here is to send the data coming from the service directly to the view. We introduce our method names to the interface classes we create. However, do not forget to remove ‘async’ at this stage. When we use the ‘await’ keyword in Repository and Service, we cannot put the ‘async’ keyword that we added to the method in interface classes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0c18991f746" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>