Warm tip: This article is reproduced from serverfault.com, please click

Naming convention for objects in multi-layer application with EF6, domain, DTO, ViewModel

发布于 2019-07-11 19:16:42

I'm starting new project with multiple layers and multiple client types (WebApp, WebAPI, Xamarin and Windows). I want to try "Clean Architecture".

My previous applications had no application layer, my view models were directly coupled with an Entity Framework DbContext. I also have very little experience with ASP.NET and web development.

Yes, this is a question similar to these:

Naming conventions for model in multi layer application

File naming convention for layers

but I'm not asking how to organize my architecture, more about using objects with same class names from different layers in one file or eventually changing my convention to use UserBO naming in Domain layer for example.

The problem

So, in the Persistence layer I have POCO class called "User".

namespace Persistence.Entities
{
    public class User
    {
        // (...)
    }
}

In the Domain layer I also have class called "User".

namespace Domain.Entities
{
    public class User
    {
        // (...)
    }
}

In the Windows and Android clients I'll create ViewModels, but I can just name it "UserVM" and this is not a problem. In WebAPI there will be "UserDTO", so no problem too. But in the Application layer I want to create some method that uses two classes with same names:

using System.Collections.Generic;

namespace Application
{
    public class Users
    {
        public List<Domain.Entities.User> GetConnectedUsers()
        {
            List<Persistence.Entities.User> usersFromDb = new List<Persistence.Entities.User>();
            List<Domain.Entities.User> usersToReturn = new List<Domain.Entities.User>();

            // take users from database, make domain objects

            return usersToReturn;
        }
    }
}

I don't want to write Persistence.Entities.User and Domain.Entities.User in my Application layer.

What can I do to deal with this?


So far I figured out, that I can use alias for using in C# like this:

using Persistence = Persistence.Entities;
using Domain = Domain.Entities;

namespace Application
{
    public class Users
    {
        public List<Domain.User> GetConnectedUsers()
        {
            List<Persistence.User> usersFromDb = new List<Persistence.User>();
            List<Domain.User> usersToReturn = new List<Domain.User>();

            // take users from database, make domain objects

            return usersToReturn;
        }
    }
}

Maybe there is some better way dealing with this?

Or maybe I should add some suffix to my Domain layer objects? UserBO (for BusinessObject). Will this be correct? Sorry if this is stupid question, but as I wrote - I have no experience in development with that many layers.

I think that last thing I would do is naming change for my database entities because there will be much more tables than domain objects, so names short as possible will be more useful in Persistence layer than in Domain layer.

Questioner
Kamil
Viewed
0
SvendK 2020-06-22 21:09:32

Short answer: I call my business layer "services", so a service that provice services around the User object I would call a UserService. The object you instantiate from the business/service layer is not "a" User, but a UserBO or UserService. The model/DTO object I'd either actually call a User, a UserModel or a UserDTO. I don't like abbrevations like BO, and I also do like the Service wording, so that's my preference. YMMV :-)