views:

53

answers:

4

Hello,

Several "parts" (a WinForms app for exmaple) of my project use a DAL that I coded based on L2SQL. I'd like to throw in several WebApps into the mix, but the issue is that the DAL "offers" much more data than the WebApps need. Way more.

Would it be OK if I wrapped the data that the websites need within a web-service, and instead of the website connecting directly to the DAL it would go through the web-service which in turn would access the DAL?

I feel like that would add a lot of overhead, but on the other hand, I definitely don't like the feeling of knowing that the WebApps have the "capabilities" of accessing much more data than they actually need.

Any input would be greatly appreciated. Thank you very much for the help.

+2  A: 

You can either create web services, or add a repository layer that presents only the data that your applications require. A repository has the additional benefit of being a decoupling layer, making it easier to unit test your application (by providing a mock repository).

If you plan on eventually creating different frontends (say, a web UI and a WPF or Silverlight UI), then web services make a lot of sense, since they provide a common data foundation to build on, and can be accessed across tiers.

Robert Harvey
I fully agree with this, however the "repository" link does not talk at all about making your Repo `AsQueryable`. A Queryable repository allows you to better construct your db calls from multiple applications without having to rewrite your existing stuff. (providing of course that you're willing to do the initial rewrite for existing db calls within your application). This is why a Repository layer goes well with a Service layer... different services for different apps.
rockinthesixstring
@rock: The decision to return `IQueryable` objects will depend on how much control you want the consumer to have. Generally, `IQueryable` objects will have access to other linked objects in the Linq to SQL object tree, and allows additional Linq queries to be executed against them. While this generally provides for a thinner and more powerful repository layer to be created (and the lazy loading is nice) it is not always ideal to do it this way, for control and security reasons. Sometimes, all you need is a list or a single entity object.
Robert Harvey
@Robert, yes this is very true, however can you not add the security and control in the Service Layer? (this is what I do anyways.)
rockinthesixstring
@rock: Not all applications have a service layer. But yes, it can be done there.
Robert Harvey
+1  A: 

If your data access layer were pulling all data as IQueryable, then you would be able to query your DAL and drill down your db calls with more precision.

See the very brief blog entry I wrote on Repository and Service layers using Linq to SQL. My article is built around MVC but the concept of Repository and Service layers would work just fine with WebForms, WinForms, Web Services, etc.

Again, the key here is to have your Repository or your Dal return an object AsQueryable whereby you wait until the last possible moment to actually commit to requesting data.

Your structure would look something like this

Domain Layer
    Repository Layer (IQueryable)
        Service layer for Web App
            Website
        Service layer for Desktop App
            Desktop App
        Service layer for Web Services 
            Web Service

Inside your Service layer is where you customize the specific calls based on the application your developing for. This allows for greater security and configuration on a per-app basis while maintaining a complete repository that doesn't need to be modified until you swap out your ORM (if you ever decide you need to swap out your ORM)

rockinthesixstring
A: 

There is nothing inherently wrong with having more than you need in this case. The entire .NET 4 Client Profile contains over 50MB of assemblies, classes, etc. I might use 5% of it in my entire career. That doesn't mean I don't appreciate having all of it available in case I need it.

If you plan to provide the DAL to developers that should not have access to portions of the data, write a wrapper or derive a new DAL. I would avoid the services route unless you're confident you can accommodate for the overhead.

Dave Swersky
I partially disagree. If @Isaac is saying that the "Data" is way more than what the web services need... then he's right to not want to query for that much information when the majority of it is useless. Yes the .NET framework is large and has a lot more than required for the average app... but you're not polling for that entire 50MB file on every single API call in the same way that you're hitting the database.
rockinthesixstring
It depends on what he means by "more data than is needed." If the *projections* include too much data, then yes he'd want to derive a new DAL. If there are queries that would be unnecessary, having them there will cost nothing unless the library will be used by developers that should not have access to the extraneous data.
Dave Swersky
yes very true. Unnecessary queries are neither here nor there. Additional data can be a huge performance hit... depends on how the current DAL is structured.
rockinthesixstring
A: 

Sounds like you are on the right track. If many applications are going to use the this data you gain a few advantages by having services with DTOs.

  1. If the domain model changes, just the mapping to the DTO needs to change. You can isolate the consuming application from these changes.
  2. Less data over the wire
  3. You can isolate you applications from the implementation of the DAL.
  4. You can expose different services (maybe different DTOs) for different applications if it is necessary to restrict what parts of the object model should be exposed.
Christopherous 5000