[RESOLVED] N-Tier / SOA questions
I'm creating an N-Tier (or at least I think its N-Tier!) solution where I have the following:
WPF client side application (presentation layer)
WCF service hosted in a windows service (data access layer)
SQL database (data layer)
Now bearing in mind this is the first time I've tried to design anything like this, I might be going about it completely the wrong way...
I've created 3 examples classes that would be my 'business objects' (im not quite sure where that fits in within the layers I listed above, because these business objects would be defined in a DLL that would be used by both the presentation layer and the data access layer). You can see them here:
http://i135.photobucket.com/albums/q.../BOClasses.jpg
So then in my WCF service (data access layer) I would have methods like this that would be exposed to the WPF client:
http://i135.photobucket.com/albums/q...AL-Methods.jpg
These are just examples but you get the idea. Is this the right way to do something like this?
I saw one design that someone had posted somewhere that basically looked to me like it merged the Business Objects and the DAL together because in their class that represented an order, they had properties like you would expect for Order Number etc etc but then they also had methods like "GetOrders" within there too.
I dunno I'm still very new to this type of design but I thought that the idea of the business objects was that they just represented business items, such as orders etc, so I thought they would just purely hold properties/fields and no methods or anything like that.
Just wanted your thoughts and suggestions on whether im on the right track or not :)
Thanks
Chris
Re: N-Tier / SOA questions
No, the web service should do very little work. All it should do is make calls to your service layer. It's the service layer that does the work. You might also want to add an extra service layer on the client side, between the UI and the web service. That way you can perform validation before invoking the web service instead of after.
You fire up an application, i.e. a presentation layer. It might be a WPF app, an ASP.NET app or whatever. The presentation layer makes requests for business logic services from your client-side service layer. Examples may be to list all the Thing entities or to save a new Thing entity. The client-side service layer performs any validation required, like ensuring that all required fields have been populated in a new Thing. The service layer can then reject the request and notify the presentation layer of the problem, or it can can invoke the web service and either pass data to it, request data from it or both. The web service then simply passes the request on to the server-side service layer. The server-side service layer will also apply any business logic required to the request, like validation. If the request is valid it will then invoke the data access layer, which will then either save data to the data layer, get data from the data layer or both. The server-side service layer will then apply any required transformation to the retrieved data and pass it back to the web service, which passes it back to the client-side service layer, which transforms it as required and passes it back to the presentation layer, which displays it to the user.
Now, in that scenario there is some redundancy with the client-side and server-side service layers. As such you might choose to omit either of them but the advantage of having the two is it modularises your application to the degree where you could remove everything from the web service up and replace it with a simple presentation layer and it would all still work. Likewise you could remove everything from the web service down and replace it with a simple data source and it would all still work. The point of this modularisatin is NOT to make your app work better as is, but rather to make it more testable and to make it more robust in the face of change.
So, the presentation layer is responsible presentation only. It doesn't know what the data means or where it comes from, only how to display it. The repository layer is responsible for data access only. It doesn't know what the data means or where it goes, only how to retrieve and save it. It's the service layer where all the intelligence resides. The service layer knows what the data means, how it should look and how it will be used. That intelligence is your business logic. It includes things like validation and transformation. In your case the web service would be dumb too. It is responsible ONLY for passing requests from the client-side to the server side.
One final point to note is that multiple layers will need to have a reference to your entity layer because entities are getting passed through pretty much all layers.
Re: N-Tier / SOA questions
Yeah I understand that but when I say the WCF service is what does most of the work, I mean thats where the various methods from different 'layer' DLLs are called and used. Surely you don't mean literally having a separate service for each of these layers because then I would end up with a WPF client application on each workstation, a service running on each workstation, a WCF service on a server, a service for business logic on another or the same server, a service for the DAL on another or the same server, and then the SQL database on another or the same server.
When I talk about a service I'm talking about a windows service (as in the ones you see in the Services MMC snap in).
I assume when you talk about a service layer you just mean a DLL? This is the thing I think I'm struggling to grasp - what exactly a layer is.
For example, lets look at your client-side service layer that you mentioned that provides validation. If this is just a DLL that contains various functions that validate data then surely its still the presentation layer that actually calls and uses these functions? So if I have a form in my WPF application where users can create a new job, the WPF application itself would have a reference to the client-side validation service layer DLL and would have something like this (assuming my service layer DLL is called App1SL):
vb Code:
Private Sub SubmitJobBtn_Click(e, Sender) blah blah blah
If App1SL.Jobs.ValidateNewJob(JobNametxt.Text, AssignedTotxt.Text, DueDatedtp.Value, etc etc) = False Then
Messagebox.Show("Invalid data was entered")
End If
End Sub
So although the actual function code is contained within a 'service layer' DLL its still the presentation layer that is executing this code, which is the same situation as the WCF service and the various layers that it would execute. This is what I meant when I said the WCF service would do a lot of the work, not that all the code would actually be written in the WCF service but that it would be the WCF service that executes the code from the various layer DLLs.
Am I still way off the mark? :)
Re: N-Tier / SOA questions
I think you may be confusing a web service with a service layer. What I mean is:
Presentation layer on client (WPF)
Service layer on client
Transport layer on server (WCF web service)
Service layer on server
Repository layer on server (LINQ to SQL, LINQ to Entities, T-SQL, etc.)
Entity layer (LINQ to SQL, Entity Framework, NHibernate, custom classes, etc.)
Data layer on server
I haven't specified that the entity layer is on the server because all the other layers would require a reference to the entity classes.
All the smarts is in the two service layers. The only work the web service would do is passing calls from one service layer to the other. It would obviously have to pack and unpack data to be sent over the wire but that's not something you have to implement yourself. That's what WCF does for you.
Re: N-Tier / SOA questions
N.B. There is only one instance of each server component. Every client deals with the one web service and then the layers below that are single instances too.
Re: N-Tier / SOA questions
Yeah but can you just define what a service layer actually is? It can just be a DLL cant it that my presentation layer uses right? Like in the example code I posted in my last post. If not then how do you actually use this service layer, how do you tell this service layer to do some work if its not just a library that the presentation layer has a reference to?
Re: N-Tier / SOA questions
Each layer is a conceptual separation of responsibility. Generally it's a good idea if the physical separation matches. Yes, the service layer would be a DLL. It would contain one or more classes that have methods to get data and save data, performing the required validation and transformation along the way.
Re: N-Tier / SOA questions
right, that makes sense then. So the example code I posted is on the right track yeah?
and my solution structure looks like the right sort of thing?
Quote:
Solution - App1
==Project - App1BL (business logic)
==Project - App1BO (business objects used by the client and the business logic)
==Project - App1Client (WPF desktop client application)
==Project - App1Server (WCF service that uses the DAL, BO and BL to provide data to respond to client requests with)
==Project - App1ServerService (Windows service that just hosts the WCF service)
==Project - App1DAL (data access layer, accesses the SQL database)
Thanks for your patience :)
EDIT: Also, as for the physical separation, this is how I was planning to have it:
On Client Workstations
Presentation Layer
Client-side business logic/validation layer
Business Objects/Entities
On Server 1
Server-side business logic/validation service layer
Data access layer
Business Objects/Entities
On Server 2
SQL Database
Server 1 and Server 2 could be the same server if necessary
Re: N-Tier / SOA questions
That looks like it except that your Server should not use the DAL and the BL. The Server uses the BL and the BL uses the DAL. Each layer should only know about the one below it.
Also, with regards to this:
Quote:
So although the actual function code is contained within a 'service layer' DLL its still the presentation layer that is executing this code
That's not really true. Yes, the presentation layer has a reference to the service layer and it does call methods of the service layer, but it's not "executing" those methods. If you're my boss and you ask me to do a job, I go and do it and report the result to you, did you "execute" the job? No, you just asked me to do and I did it. You made the request and made use of the result but you have no idea of what happened in between. The same goes for your code. The presentation layer asks the service layer to perfrom a task but its the service layer that performs it. The same goes for all the other interactions. The service layer asks the repository layer to get some data but the how that gets done is of no concern to the service layer. It just makes use of the result. This is all conceptual stuff but it's important to have that stuff straight so that you can create a clean implementation.
Re: N-Tier / SOA questions
Quote:
Originally Posted by
jmcilhinney
That looks like it except that your Server should not use the DAL and the BL. The Server uses the BL and the BL uses the DAL. Each layer should only know about the one below it.
Thanks, I guess I kind of knew that but seeing it written down helps to clarify it.
Quote:
Originally Posted by
jmcilhinney
Yes, the presentation layer has a reference to the service layer and it does call methods of the service layer, but it's not "executing" those methods.
Yeah I guess my terminology is just a bit wrong - I just meant that because a DLL on its own cant do anything, its the EXE file that my WPF presentation layer runs in that will actually make the DLL do something... (still a bad explanation but I do understand it now)
Quote:
Originally Posted by
jmcilhinney
This is all conceptual stuff but it's important to have that stuff straight so that you can create a clean implementation.
Which is why I'm spending so much time trying to make sure I understand it before I actually start writing any more code. Speaking of which, from what I've read it seems that most people recommend starting a project like this at the data layer and working your way up, so I assume creating the necessary tables etc in the SQL database is the first thing to do? If so then I guess I need to do some more thorough planning as I currently have just a vague idea of what tables/fields I will need in the database..
Re: N-Tier / SOA questions
You could use a bottom-up approach, which most people would do, or you could use a top-down approach, which would be favoured by those into test-driven development.
Re: N-Tier / SOA questions
OK cool well I guess I'll try and do it the way that most people do and start from the bottom (data) and work my way up.
Thanks for your input :)
Chris
Re: [RESOLVED] N-Tier / SOA questions
Not that I'm saying you should or shouldn't do it but TDD actually makes a lot of sense. It didn't to me at first but, especially with ASP.NET MVC lately, it actually fits really well. Separating applications into well-defined layers actually facilitates TDD. It's just not really practical otherwise.
Re: [RESOLVED] N-Tier / SOA questions
Excuse my ignorance but what does TDD stand for?
EDIT: according to google its: A style of programming where tests for a new feature are constructed before any code is written. Code to implement the feature is then written
So I assume its Test Driven Development.
http://upload.wikimedia.org/wikipedi...evelopment.PNG
Re: N-Tier / SOA questions
Quote:
Originally Posted by
jmcilhinney
you could use a top-down approach, which would be favoured by those into test-driven development.
You decide what your app, or a component thereof, is supposed to do first and then you write a test for that. Initially the test will fail, so you then write some code to make the test pass. Once that's done you refactor the code to optimise it.
It seems odd at first but it actually makes a lot of sense, although it requires that you do things in certain ways or it just becomes impractical. For instance, as I said earlier, using interfaces to represent each layer makes your code more testable. If your repository layer implements a repository interface then you can test your service layer using a mock or fake repository that implements the same interface but doesn't actually interact with the database. This lets you isolate the functionality of the service layer from the functionality of the repository layer, so you know that any issues that arise are issues with the service layer. It reduces noise and lets you concentrate on one feature or unit of functionality at a time.
I have to admit, TDD is much easier when the tools provide refactoring support so it tends to be easier in C# than in VB and easier again with the help of ReSharper or a similar tool.