60

Martin Fowler's book "Patterns of Enterprise Architecture" states: The easier question to answer is probably when not to use it. You probably don't need a Service Layer if your application's business logic will only have one kind of client - say, a user interface - and it's use case responses don't involve multiple transactional resources. [...] ...


35

Adding a service layer because you have evaluated the idea and concluded its the best approach: good Adding a service layer because that's what all the cool kids are doing: bad If your gut says you don't need one, then don't make one. One of the more disappointing developments in the programming world over the past 10 years or so is that it has become ...


25

In 'SOLID' the 'I' stands for Interface Segregation. The whole idea of this principle is to split large interfaces into smaller ones, more modular. In MVC service would normally have an interface that controller would rely upon. You don't want your controllers to know about concrete implementation of that service. Therefore, a bunch of services with one or ...


22

There are many factors that go into the decision of creating a service layer. I have created service layers in the past for the following reasons. Code that needs to be re-used by multiple clients. Third party libraries that we have limited licenses for. Third parties that need an integration point into our system. Centralizing duplicated business logic. ...


16

An anemic model is simply a data container. It doesn't contain behavior. (This might actually be considered a good thing in the functional paradigm.) The opposite of an anemic model is not a model injected full of domain services. You're describing two extremes -- both are bad. If you have an anemic model, you're not fully embracing what OOP offers. If you ...


15

Why do we even need it? You don't. Creating a separate database for each service helps to enforce domain boundaries, but it's only one approach. There's nothing stopping you from having all your services share the same database. As long as your services behave and don't do unexpected things to data owned by other services, you'll be fine. I don't know what ...


14

I think the word 'service' gets bandied around so much that it's meaning nowadays is very overloaded and confusing. There is a big difference between a 'service layer' and a 'web service [layer]'. A 'service layer' is an abstraction behind which you put the business logic that will be consumed regardless of the UI. That way the UI layer can be as simple ...


14

Yes. I have seen architectural diagrams of SOA (Service Oriented Architecture) where exactly this happens. A service should be written in such a way that it doesn't care if user is calling it, a service in the 'same layer', or it is being combined into a composite service. The Open Group SOA Reference Architecture diagram shows services calling services in ...


13

I was part of a team that successfully built a microservices architecture using a service bus. Initially, we believed that microservices and an event driven architecture would enable us to fix the underlying shared data ball-of-mud database. What we learned was that microservices and an event driven architecture required us to get rid of the underlying ...


10

Threads do have significant costs - VERY roughly - imagine 100K bytes per thread (they each need a stack for one thing), and they each place a slight burdon on operating system components (e.g. scheduler) which have to manage them all. Threads DO present a very simple model for managing async tasks. I'm a big fan of that approach. But if you are going to ...


8

Library Advantages: Lower overhead per call (only jump or even inlined) = may increase performance Simplest thing that could possibly work No risk of centralized service going down and impacting all consumers Service Advantages: Everyone gets upgrades immediately and transparently (unless versioned API offerred) Consumers cannot decompile the code Can ...


7

TL;DR Is it a good idea to use a kitchen-sink server for various services? There's no universally-correct answer to this question. It is fundamentally a business decision based on the budget you have and the problems you are trying to solve for. Arguments for Separating Services There are many reasons for separating services, but two of the biggest ...


7

It's not contradictory. Both proponents would like you to put your actual code in the domain object itself. ie. public class Order { private string status = "not bought"; public void Buy() { this.status = "bought"; } } vs ADM public class Order { public string Status = "not bought"; } public class BuyingService { public ...


6

Adding an interface (a service layer is a type of interface) takes time. A good one takes a lot of time to design and test. It is very important to get it right on the first try because changing it later breaks all the clients. Also, consider that you probably won't know what needs to be in that interface until you have a second client with slightly ...


6

Most Service Layers I've seen are a complete mess. Services tend to have a lot of different methods, 1500 LOC are not rare. The different methods have nothing in common, but do share code. This results in high coupling, low cohesion. It also violates OCP, because every time a new operation is needed, you have to modify the code instead of extending the code ...


6

Thoroughly construct and define an API, or set of different APIs if needed, then implement by just wrapping requests to this webapp (and/or packages which are used by this webapp). This will help you to "multithread" following development. You'll be able both build new tools, which will use your new API, and, at the same time, you can start gradually ...


6

I wouldn't say that a console application is totally different from a windows service. They are both hosts to execute code. That being said, there are some key differences: A service can run even if a user is not logged into the PC. A service can easily be configured to run in the context of a high-authority accounts such as Network Service or Local ...


6

Front end <--> API Service -> Service -> Repository -> DB Right. This's the basic design by segregation of concerns proposed by Spring Framework. So you are in the "Spring's right way". Despite Repositories are frequently used as DAOs, the truth is that Spring developers took the notion of Repository from Eric Evans' DDD. Repository interfaces will look ...


6

I know that these are two different methods but they are doing the same task No they aren't. One is linking a person to a company. The other links a company to a person. They may use the same implementation to achieve these two tasks, but they are not the same task. Let's assume though that the code in each method is identical. Is DRY violated? Yes, ...


6

It depends (but I guess for most real-world situations the answer is no, you risk to run into trouble). A general-purpose audit trail may collect data which, depending on the details, might be affected by laws of data security and privacy of the jurisdiction where your application will be used. If those laws will force you to keep the data only accessible ...


5

In the MVC pattern, eberything that is not part of the Controller or View is considered to be part of the Model side of the triad. This means that from an MVC perspective, your question is moot, because both your services and models are part of the MVC Model side of the triad. As most MVC frameworks provide data access services as part of their Model ...


5

In a DDD (Domain Driven Design) application using a conventional layered architecture, business logic goes into the domain layer. Now, please note this layer does not only contain the domain entities (Movie, Review), but also domain services (like a ReviewService class) and repositories. So, ReviewService is a business class as well. Your concern about ...


5

Do we need dependency injection in this case? It depends on what you try to accomplish. Dependency injection is needed in order to easily replace the underlying implementation. Common examples are: To replace actual implementation by stubs/mocks in a context of unit testing. To easily swap between several data access layers (for instance to deal with ...


5

Exceptional cases don't have to be exceptionally rare. Discovering that your desired username is already taken is an exceptional case in the workflow of "ask to create new user; new user is created". Throwing an exception here is absolutely fine and normal.


5

If the users of those "client PCs" have full administrative rights over their computer, then there is no technological way to prevent them from disabling or removing your service. And if there were, you would also be locked out if the service was no longer needed or if it malfunctions. If the users don't have administrative privileges, then it is just ...


5

The basic concepts are orthogonal, however, they are related. One has to do with the availability of your application, and the other has to do with the correctness of your application. Remember, there are differing levels of faults (also known as bugs). Fault Tolerance "Everything fails, all the time" -- Werner Vogels, Amazon CTO When you design for ...


4

In a complex system like you describe, I would say that the configuration of that system is at least as important, if not more so, than any individual component. Often in my experience, when a complex system I work on is not working, it is the configuration, not any individual component, that is responsible for the failure. An analogy I like to use is that ...


4

Well, I think RegistrationService is more intent revealing. One can easily speculate on what function can be found there such as registerUser(), ConfirmRegisteration(), ResetPassword(),...etc. To me most of them seem to fall logically in a RegisterionService class than a general UserService class. A service layer is supposed to contain functions that ...


4

There are two authorization models that are common: trusted subsystems and delegation. Trusted subsystems is an architecture in which the user creation & message creation services trust the API application to perform the user authentication & authorization. In this model, the API application is trusted (via network ACLs, tokens, x509 certificates, ...


4

Normally you view the logs independently. Too much log is a real problem. There is almost no reason to view an interleaved log. If you really need to log an "action" through all the services have them write to a central log syslog is a great example apache writes to one log mysql to another, and there's usually never a reason to interleave them. Your ...


Only top voted, non community-wiki answers of a minimum length are eligible