# .NET and More > WPF, WCF, WF >  Using WCF as a Data Access Layer

## chris128

OK first of all, this whole layered/tiered model of designing programs is quite new to me so apologies if I'm using the wrong terminology anywhere (and please correct me).

I'm writing this job logging system (yes still, I had a month off ok!  :Smilie:  ) and I was originally planning to make it work like this:
WPF client application accesses SQL Database but also uses WCF service on a server for some central features such as sending email and tracking scheduled jobs.

However I am now thinking of making it more like this:
WPF client application requests data from the WCF service and the WCF service does the reading/writing from the database and returns the results to the WPF app.

So in effect I believe this makes the WCF service my data access layer, so all of the methods that access the database that would have originally been in my WPF app will now be methods in the WCF service (that are exposed via contracts etc).

Does this sound like a decent plan? I'm a little concerned about the scalability, I mean I dont know how well a system like this would work if I had 50 workstations running this WPF app that were all constantly running these various methods from the WCF service. The WCF service will be hosted within a windows service by the way and will be using TCP as the transport method.

Just looking for some thoughts/advice on how to implement something like this and whether or not its a good idea.

Thanks
Chris

----------


## chris128

Well after doing some more research it seems like quite a few people are using WCF services in this way so hopefully im on the right track...

----------


## DeanMc

Wish I could help you buddy but unfortunately I haven't a clue when it comes to WCF

----------


## chris128

No worries  :Smilie:  I'm hoping Mendhak noticies this thread soon :P

----------


## DeanMc

I think he is in Cambodia!?

----------


## chris128

Bummer haha you know for how long?

Hopefully we have some other WCF users on here then!

----------


## Hamish

I'm not a WCF expert, but I have played around with it, and I came across several examples that use it just like you plan to.
From my personal experience in general, I think 50 workstations should not be too much of a problem, but of course it depends on the server machine and how often these clients 'consume' (still think it's a strange name, but it's the official WCF name I think) the service. If you refresh a screen every second, there's going to be a big load on the server. If you refresh the same screen every 10 seconds, it's going to be a lot less, obviously.

----------


## mendhak

Back!

And sounds good! That's probably the most common use WCF has - acting as a DAL or BL.  Don't forget to manually increase the number of concurrent connections in the config file.

----------


## mendhak

I should also add - it's quite common to use the WCF as a BL layer as well, with it returning entities.  For example, a GetRecords() entities could return a List<Record> object.  Now, it doesn't mean that Record has to sit in the WCF's project, it can be a class library that the project references.  At the same time, the client project can also reference that class library.  WCF is smart enough to know that when you reference the WCF service, whether the class being returned from a method is a known type or not.  

This means that your client application will know that the type returned from the method is List<Record>, which it already knows about, so you can use it fully.

The reason it's preferable to use it to return entities rather than datasets is because it reduces the overhead.  Datasets are big.

----------


## chris128

Yeah I was planning on returning custom business object classes instead of datasets  :Smilie: 
JMC has been filling me in on how this whole DAL/BL stuff works (http://www.vbforums.com/showthread.php?t=570048) and so I think I'm going to have separate class libraries for each of the layers and just reference them in the WCF service as you say.

As for the connection limit change you mentioned - is the default limit quite low or something?
EDIT: Just checked and it seems that it is set to 10 connections by default... which will certainly need changing! Thanks for the heads up

----------


## guyjasper

hey chris, i'm also doing this same kind of project. i have a WCF service act as DAL. have you had issues about data integrity? how do you manage you r database connections? are they created and destroyed for each call to WCF? 

and how do you handle locking of database records so that each client gets the latest data that has  been changed?

----------


## chris128

Hey,
I haven't used WCF for a while now, though I do plan on using it for the next major project I'm starting. I think I was just creating one SqlConnection object in my WCF service and each call used that same SqlConnection. Not sure if that is the best way to do it but I think that's what I was doing when I used it before. As for locking records, I think I just used SyncLock (because if I remember rightly each WCF call comes in on its own thread, but dont quote me on that...) to lock parts of my code where I needed to update more than one database record in one go so that other calls had to wait for that to complete before they could access/modify the data.
Sorry I cant be a bit more specific..

----------


## guyjasper

> Hey,
> I haven't used WCF for a while now, though I do plan on using it for the next major project I'm starting. I think I was just creating one SqlConnection object in my WCF service and each call used that same SqlConnection. Not sure if that is the best way to do it but I think that's what I was doing when I used it before. As for locking records, I think I just used SyncLock (because if I remember rightly each WCF call comes in on its own thread, but dont quote me on that...) to lock parts of my code where I needed to update more than one database record in one go so that other calls had to wait for that to complete before they could access/modify the data.
> Sorry I cant be a bit more specific..


thanks for the reply Chris. i don't think it is the best design to just use a single connection object. when i was debugging my web service, i had 2 clients calling the same function on the web service. they were both able to consume the function at the same time, a new instance of my Service Contract object for each call of my client. when a DataReader has already been opened for a connection, you won't be able to use the same connection unless the DataReader is closed. i already asked same question in another thread and someone pointed me about Concurrency Handling. have you have any thoughts on this  :Smilie:

----------

