How to implement layered architecture?

fatih uyanık 80 Reputation points
2024-04-10T20:42:05.4866667+00:00

Hello

I started working on layered architecture to write some more professional code in C#. As a result of research on the internet, I saw that each person implements layered architecture differently. This caused confusion.

I use the MVVM pattern in my projects. For this, I created DAL, BL and PL layers respectively. If I'm not mistaken, PL should use the BL layer, and the BL layer should use the DAL layer, respectively.

Now I created database configurations and entities against tables with EF in the DAL layer. In the BL layer, I created jobs that would add, update and delete data using the structure in the DAL layer. I also use dependency injection. Accordingly, when I add the DAL layer to the dependency container on the PL side, it uses the DAL layer directly. Exactly what kind of structure should I set up? Can you share information about this?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,674 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,261 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,256 Reputation points Microsoft Vendor
    2024-04-11T06:32:47.64+00:00

    Hi,@ fatih uyanık. Welcome to Microsoft Q&A.

    It sounds like you're on the right track with your layered architecture using the MVVM pattern.

    Presentation Layer (PL):

    This layer contains your WPF views (XAML files) and their corresponding view models (C# classes).

    Views are responsible for displaying UI elements and interacting with the user.

    View models handle the logic and data binding between the views and the business logic layer (BL).

    Views should not contain any business logic; they should delegate that responsibility to the view models.

    Business Logic Layer (BL):

    This layer contains the business logic of your application, including validation, calculations, and data manipulation.

    It should be independent of any specific UI technology (e.g., WPF) and should focus solely on implementing business rules.

    The BL layer interacts with the data access layer (DAL) to retrieve and persist data.

    Data Access Layer (DAL):

    This layer is responsible for interacting with the database or any other data source.

    It contains classes that encapsulate database operations, such as querying, inserting, updating, and deleting data.

    The DAL layer abstracts the details of database access, allowing the BL layer to work with data in a more abstract and business-focused way.

    Dependency Injection (DI):

    Use DI to inject dependencies (e.g., services, repositories) into your view models and other components.

    Configure DI container at the composition root of your application (e.g., App.xaml.cs) to register dependencies and resolve them as needed.

    Inject dependencies into your view models using constructor injection, property injection, or method injection.

    Regarding the structure and setup of your project:

    DAL: Contains database configurations, entities (EF models), and data access classes (repositories, data services).

    BL: Contains business logic classes and interfaces. It should depend on the DAL but should not depend on the PL.

    PL: Contains views, view models, and any UI-related components. It depends on the BL but should not depend on the DAL directly.

    To ensure loose coupling and maintainability:

    Define interfaces in the BL layer for services and repositories.

    Implement these interfaces in the DAL layer.

    Use dependency injection to inject concrete implementations of these interfaces into your view models.

    The following are documents you can refer to.

    Implementing the MVVM Pattern (focus on structure)

    Create a simple data application with WPF and Entity Framework 6


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.