后缀服务,管理器,系统有什么区别?

发布于 2025-01-18 09:42:07 字数 526 浏览 2 评论 0原文

我很好奇服务、管理器和系统之间的区别。

我一直试图避免使用此类后缀。但在我开始使用 MVC 后,我需要创建一个单独的类,以免每次从任何脚本调用函数时都访问控制器。系统提示我使用 Service 类从任何脚本中引用它。据我了解,使用控制器作为单独的实体仅用于处理事件(我理解正确吗?): 如何从另一个控制器使用一个控制器在MVC中

从上面链接的示例中,我有一个SpeakerController,它接受事件并将其定向到执行某些功能的SpeakerService。

为什么需要命名为SpeakerService,而不仅仅是Speaker?从OOP原理的名称中,我一直只看到类的名词,没有后缀。

我期望有一个更具可读性的程序,其名称不言而喻。我尝试过使用 Manager 和 Service 后缀,但我不明白它们的概念必要性

I'm curious about the difference between Service, Manager and System.

All the time I tried to avoid such suffixes. But after I started working with MVC, I needed to create a separate class so as not to access the controller every time from any script to invoke a function. I was prompted to use the Service class to refer to it from any script. And as I understood using controllers as separate entities only for handling events (Did I understand correctly?):
How to use a controller from another controller in MVC

From the example on the link above, I have a SpeakerController which accepting an events and directing it to SpeakerService that performs certain functions.

Why it was necessary to name SpeakerService, and not just Speaker? From the names of the OOP principle, I have always seen only a noun for the class without suffixes.

I expect a more readable program where the name speaks for itself. I have tried using the Manager and Service suffixes but I don't understand their conceptual necessity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果 2025-01-25 09:42:07

当我们编写软件时,我们需要一种分离类的方法。如果我们将所有类放在一个文件夹/项目中,那将是一团糟。
所以几乎所有程序都必须:

  • 将状态/数据保存在某处。它可以是数据库。我们想要创建用于映射的类,例如 SQL 表到 POCO 类。我们将此层称为模型。
  • 应用一些业务规则模型。该层汇集了管理器或方法来完成完整的任务,例如计算城市之间的最近距离或用多种成分制作咖啡。我们将此层称为Service
  • 按业务规则向用户显示处理的数据。我们将此层称为View

所以对我来说:

  • 服务应该用来组装多个管理器来完成一个完整的任务。
  • 管理者用于将一些业务逻辑应用于一个具体模型。例如聊天系统UserManager可以有以下方法,例如GetUserByIdGetOnlineUsers()AddUser ()UserLogin()UserLogoff()

更新:

我在 ASP.NET MVC 中看到了服务。这是一个简单的示例:

这是一个模型:

public class Blog
{
    public string Name { get; set; }

    public IEnumerable<Post> Posts { get; set; }

    public IEnumerable<Comment> Comments { get; set; }
}

和视图模型:

public class BlogViewModel
{
    public string Name { get; set; }

    public IEnumerable<PostViewModel> Posts { get; set; }

    public IEnumerable<CommentViewModel> Comments { get; set; }
}   

这些是存储库:

public interface IPostRepository
{
    IEnumerable<Post> GetById();
}

public interface ICommentRepository
{
    IEnumerable<Comment> GetById();
}

public interface IBlogRepository
{
    IEnumerable<Blog> GetById();
}   

以及我们的服务:

public class BlogService
{
    IPostRepository _postRepository;
    ICommentRepository _commentRepository;
    IBlogRepository _blogRepository;

    public BlogService(IPostRepository postRepository, 
        ICommentRepository commentRepository, IBlogRepository blogRepository)
    {
        _postRepository = postRepository;
        _commentRepository = commentRepository;
        _blogRepository = blogRepository;
    }

    // here a place to accomplish the whole task
    public BlogViewModel Get()
    {
        BlogViewModel blogViewModel = new BlogViewModel();
        blogViewModel.Comments = _mapper.Map<IEnumerable<CommentViewModel>>(
            _commentRepository.GetById());
        blogViewModel.Posts = _mapper.Map<IEnumerable<PostViewModel>>(
            _postRepository.GetById());
        return new BlogViewModel();
    }        
}

和​​控制器方法:

public BlogViewModel Get() 
{
    return _blogService.Get();
}   

使用管理器的示例

让我们创建一个聊天应用程序。 oop 模型可能如下所示:

User 类:

public class User
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string NickName { get; set; }

    public Status Status { get; set; }

    IEnumerable<Topic> Topics { get; set; }
}

Message 类:

public class Message 
{
    public int Id { get; set; }

    public string Content { get; set; }

    public DateTime DateCreated { get; set; }
}

Topic 类:

public class Topic
{
    public IEnumerable<User> Participants { get; set; }

    public IEnumerable<Message> Messages { get; set; }

    public void AddMessage(Message message) { }

    public void RemoveMessage(int id) { }

    public void AlterMessage(Message message) { }

    public void AddUser(User user) { }

    public void RemoveUser(int id) { }

    public void BlockUser(User user) { }

    public void InviteUser(User user) { }
}

Status 枚举:

public enum Status { 
    Online, 
    Busy, 
    Available
}

ChatSystem 类:

public class ChatSystem
{
    public IEnumerable<Topic> Topics { get; set; }

    public void AddTopic(Topic topic) { }

    public void RemoveTopic(int id) { }

    public void AlterTopic(Topic topic) { }

    public void Login(string email, string password) { }
    public void Logoff() { }

}

UserManager 类:

public class UserManager
{
    public IEnumerable<User> AllUsers { get; set; }

    public User GetById() 
    { 
        return new User(); 
    }

    public Status GetUserStatus()
    {
        return new User().Status;
    }

    public void AddUser(User user) { }

    public void RemoveUser(User user) { }

    public void SendEmail(User user) { }
}

When we write software, we need a way to separate classes. If we put all classes in one folder/project, it would be a complete mess.
So almost all programs have to:

  • save state/data somewhere. It can be database. We want to create classes for mapping, e.g. SQL tables to POCO classes. Let's call this layer as Model.
  • apply some business rules model. This layer brings together managers or methods to finish a complete task such as E.g. Calculate the nearest distance between cities or make a coffee from many ingredients. Let's call this layer as Service.
  • show handled data by business rules to user. Let's call this layer as View.

So for me:

  • services should be used to assemble multiple managers to finish a complete task.
  • managers are used to apply some business logic for one concrete model. E.g. UserManager for Chat System can have the following methods such as GetUserById, GetOnlineUsers(), AddUser(), UserLogin(), UserLogoff()

UPDATE:

I saw services offen in ASP.NET MVC. So this is a simple example:

This is a model:

public class Blog
{
    public string Name { get; set; }

    public IEnumerable<Post> Posts { get; set; }

    public IEnumerable<Comment> Comments { get; set; }
}

And view models:

public class BlogViewModel
{
    public string Name { get; set; }

    public IEnumerable<PostViewModel> Posts { get; set; }

    public IEnumerable<CommentViewModel> Comments { get; set; }
}   

These are repositories:

public interface IPostRepository
{
    IEnumerable<Post> GetById();
}

public interface ICommentRepository
{
    IEnumerable<Comment> GetById();
}

public interface IBlogRepository
{
    IEnumerable<Blog> GetById();
}   

and our service:

public class BlogService
{
    IPostRepository _postRepository;
    ICommentRepository _commentRepository;
    IBlogRepository _blogRepository;

    public BlogService(IPostRepository postRepository, 
        ICommentRepository commentRepository, IBlogRepository blogRepository)
    {
        _postRepository = postRepository;
        _commentRepository = commentRepository;
        _blogRepository = blogRepository;
    }

    // here a place to accomplish the whole task
    public BlogViewModel Get()
    {
        BlogViewModel blogViewModel = new BlogViewModel();
        blogViewModel.Comments = _mapper.Map<IEnumerable<CommentViewModel>>(
            _commentRepository.GetById());
        blogViewModel.Posts = _mapper.Map<IEnumerable<PostViewModel>>(
            _postRepository.GetById());
        return new BlogViewModel();
    }        
}

and controller method:

public BlogViewModel Get() 
{
    return _blogService.Get();
}   

An example of using manager

Let's create a chat application. The oop model could look like this:

User class:

public class User
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string NickName { get; set; }

    public Status Status { get; set; }

    IEnumerable<Topic> Topics { get; set; }
}

Message class:

public class Message 
{
    public int Id { get; set; }

    public string Content { get; set; }

    public DateTime DateCreated { get; set; }
}

Topic class:

public class Topic
{
    public IEnumerable<User> Participants { get; set; }

    public IEnumerable<Message> Messages { get; set; }

    public void AddMessage(Message message) { }

    public void RemoveMessage(int id) { }

    public void AlterMessage(Message message) { }

    public void AddUser(User user) { }

    public void RemoveUser(int id) { }

    public void BlockUser(User user) { }

    public void InviteUser(User user) { }
}

Status enum:

public enum Status { 
    Online, 
    Busy, 
    Available
}

ChatSystem class:

public class ChatSystem
{
    public IEnumerable<Topic> Topics { get; set; }

    public void AddTopic(Topic topic) { }

    public void RemoveTopic(int id) { }

    public void AlterTopic(Topic topic) { }

    public void Login(string email, string password) { }
    public void Logoff() { }

}

UserManager class:

public class UserManager
{
    public IEnumerable<User> AllUsers { get; set; }

    public User GetById() 
    { 
        return new User(); 
    }

    public Status GetUserStatus()
    {
        return new User().Status;
    }

    public void AddUser(User user) { }

    public void RemoveUser(User user) { }

    public void SendEmail(User user) { }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文