0

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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • My understanding is that the term "Service" as it's commonly used today originated in [DomainDrivenDesign](https://martinfowler.com/bliki/DomainDrivenDesign.html). That doesn't mean people are using the term _correctly_ but I think that's where it came from. You may also be interested in [Naming Classes - How to avoid calling everything a "Manager"?](https://stackoverflow.com/q/1866794/1371329) – jaco0646 Mar 31 '22 at 14:12

1 Answers1

0

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) { }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Looks a little confusing. It would be great if you could give a small example of how it would look in code along with relations between scripts – Eugenio Uglov Apr 01 '22 at 22:57
  • @EugenioUglov please, see my updated answer – StepUp Apr 02 '22 at 18:06
  • Oh my experience is not enough to understand this logic. So, service exist just to separate logic from controler? And system must have inside manager – Eugenio Uglov Apr 04 '22 at 15:59
  • Can I name like UserSystem or ChatManager, will be som differences in scripts? – Eugenio Uglov Apr 04 '22 at 16:00
  • @EugenioUglov *So, service exist just to separate logic from controller?* yeah, and it assembles multiple classes to complete a task. *And system must have inside manager*, no, it is not the rule, but generally, in my opinion, you can call it like `Manager` – StepUp Apr 04 '22 at 19:12
  • *Can I name like UserSystem or ChatManager, will be som differences in scripts?* There is no strong naming conventions, however, in my view name with `system` should be the most important in hierarchy of `system, service and manager`. Then `service` and `manager` – StepUp Apr 04 '22 at 19:14
  • So far looks a little hard for me yet. Perhaps I can understand by examining this example. How could this class Car be reworked to display in the MVC pattern? And how will be the reletions implemented if I want to use the Go() and Stop() methods from any script? I want these methods to work when a keyboard key is pressed or a button on the screen is pressed. In addition, some time after calling the Go() method, the Stop() method should work. – Eugenio Uglov Apr 05 '22 at 12:06
  • @EugenioUglov as a good practice, it is better to ask a new question. So one post will have one question. By doing this it will simplify the future search of other users. Feel free to upvote and mark a reply as an answer. [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – StepUp Apr 05 '22 at 12:17
  • I was still hoping to understand the meanings of the suffixes from the suggested car example. I think it will be easier to understand the answer. P.S. How can I put multi line code here in a comment like you do with
    ? I was trying to use "`" but it formats code just to one line.
    – Eugenio Uglov Apr 05 '22 at 12:25
  • @EugenioUglov [Code Formating in Comments](https://meta.stackexchange.com/questions/77485/code-formating-in-comments) – StepUp Apr 06 '22 at 07:55
  • @EugenioUglov Try to write code with information that you have and then you can put your code at [code review](https://meta.stackexchange.com/questions/39260/how-do-i-post-code-in-comments) – StepUp Apr 06 '22 at 07:58
  • thank you, so I see that format doesn't work for code in comments. So I have uploaded this peudo code here: http://tpcg.io/WVNO4I Would like to know how I can separate it to Service, Manager, System. Thank you a lot! – Eugenio Uglov Apr 06 '22 at 16:24
  • @EugenioUglov it is completely another question. As a good practice, it is better to ask a new question. So one post will have one question. By doing this it will simplify the future search of other users and more users will have pay attention to your question. It is better to ask code review question at [code review stack exchange](https://codereview.stackexchange.com/). Feel free to upvote and mark a reply as an answer. [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – StepUp Apr 07 '22 at 06:33
  • Okay. Did it: https://codereview.stackexchange.com/questions/275590/change-code-from-oop-to-mvc-pattern – Eugenio Uglov Apr 07 '22 at 11:06