views:

742

answers:

4

Hey guys I'm hoping someone can get my pointed in the correct direction regarding these new technologies. I'm still in college and unfortunately our school doesn't teach any proprietary technologies(ex. MSFT). I've got decent experience with C# however when it comes to an enterprise level app with all this new stuff, I'm a bit lost.

My goal is to have an app that is using WPF and is a 3 tier architecture. UI, BLL, and DAL...I'd like to use the MVVM but I'm not sure how that works with a 3 tier architecture or if it is something entirely different. So with that in mind, I have a few questions:

1) LINQtoSQL: I've read a lot online that say LINQ replaces your DAL and seen many articles that say this a bad idea. I'm thinking it's a bad idea, however, what do I put in here? What are the datatypes i am returning to the BLL? IQueryable? ObservableCollection? I have no clue.

2) The BLL: I'd like to make this a service that runs on a server, so that when I need to make a change I don't need to redeploy the whole app, I just need to restart the service. But, I'm not sure where to start on this.

3) With the BLL, I guess I'm confused on how the data is going through all the layers from the DAL all the way to the Interface.

I've done lots of research online, and have got bits and pieces of things, however I haven't seen anyone talk about a WPF application that is using MVVM with LINQ in the DAL using SQLMetal and a BLL thats running on a server. Can anyone point me in the right direction? or maybe a book to get?

Thanks!

+2  A: 

I'll try to provide some insight, though I'm not an expert, I've tackled these issues in the past.

  1. LINQ to SQL is actually pretty good at what it's supposed to do - which is replace your DAL. But don't return an IQueriable upwards to your BLL, as that would enable (or at least hint to the possibility) the BLL to query your DB directly. You should transfer the data object to the BLL and let it construct a matching business object. Also note: LINQ in and of itself can be used in any layer (and in fact is one of the best features of C#). LINQ to SQL is the mechanism by which LINQ statements get transated to SQL queries.

  2. BLL as a service is a natural choice. Provide an upward interface to the presentation layer (a WCF service is a good option here).

  3. The BLL generates business objects based on data it receives from the DAL. In order to provide for good decoupling of layers, you should use different classes for your DAL and BLL objects. Don't create a dependency between your presentation layer and your data layer.

Aviad P.
+2  A: 

Mike,

your question is really cool, I like it. Firstly, feel free to experiment a bit - each and every project is different, so there's no single rule, which fits everywhere. That's why I would suggest just leaving DAL to LINQ 2 SQL. this great tool will handle it, you don't have to worry. Secondly - you mentioned 3 Tier Architecture, but why isn't there place for the Model? Since all models are generated automatically (e.g. SQLMetal), you don't have to worry about mappings either. So, if you're not bored yet, let me answer all of your 3 questions:

  1. Skip DAL and observe your project carefuly - if you have a feeling, that it's lacking this layer - add it (it will contain LINQ2SQL queries). And the second part - you can return whatever you wish, but it will be most convenient for you to use IEnumerable<> or IQueryable<> parametrized with your models.

  2. My intuition tells me, that you're going to need WCF - in this case you should be able to wrap whole (yes, that's true) whole business logic in a nice Contract and implement however you wish.

  3. This is the easiest one :) Since your BLL layer is actually an implementation of some Contract (Interface), you can design that Interface to provide you with all data you need. For example:

Contract/Interface:

IEnumerable<User> GetTallUsersOver40();
IEnumerable<User> GetShortUsersOver60();
...

And that 'all the layers' you were talking about shrink to a single LINQ2SQL query execution. If you need more logic - place it in this layer.

I want to use MVVM, what now? The answer is simpler than you think - just prepare your views and view models and simply consume your BLL Contract/Interface implementaion.

Please ask if you have further questions!

Piotr Justyna
So is the "Model" in Model-View-ViewModel essentially the BLL that I will wrap in WCF?
Mike
Not exactly. Please think of a Model like of a separate layer made just to map (or literally "model") your persistent data. In short: Model is a set of classes designed for modeling your database. Since LINQ 2 SQL does modeling for you (this is simply brilliant), please don't worry about this - you have all modeling classes provided :) if you have further questions - please don't hesitate to ask them.
Piotr Justyna
Hmm... I guess I'm having trouble envisioning the layers. Let's see if I have this right. On the top, we've got the View which has an associated .cs file that ideally no code goes in there. The view binds to the ViewModel which is essentially an adapter for the View. (Now this is where I start getting lost), since the model is taken care of by LINQtoSQL, Do i put a reference/connect to the WCF BLL in each ViewModel? And then lastly, have the BLL reference the DAL? And the dataContext goes in the DAL?
Mike
Yes, you're 100% right. Each ViewModel can reference WCF BLL (you can think of something like a ViewModel base class for every VM in your project), BLL references DAL (but obly if you do need one - in other case - just query your persisted data directly from BLL via LINQ). The last thing (DataContext) - if you're talking about your views DataContexts, they should defenitely point to instances of your ViewModels. Pleas feel free to ask more questions :)
Piotr Justyna
I do have to have a DAL as that is not up to me. So my next question is, we put the .dbml file in the DAL; whats the proper way to return data all the way back to the viewmodel/interface? From query to display...
Mike
Hi Mike. I believe it's a far better idea to put dbml file inside your Model layer. then, if you really have to implement DAL, please consider creating base CRUD methods for each of your DB Table : e.g. Table 'Users' have its DAL class UsersDAL with basic operations: Insert, Select, Update, Delete. Since you have DAL created, you can use its methods inside BLL to create some logic for your tables (e.g. 'GetMostActiveUsers' method sounds good here). ViewModel just uses BLL's methods. It's really that simple :)
Piotr Justyna
A: 

Great question. I don't think there is any one place that has all the answers. I had very similar questions when we were starting a new project. MVVM is really just a presentation pattern and doesn't care of all the details like you listed. Laurent Bugnion has a good framework that glues everything together as well.

  1. LINQ2SQL is cool but can get cumbersome with the VS08 designers. Take a look at http://plinqo.com/ to use with CodeSmith to generate the DAL and I think it will even do the BLL with contracts as well. Another generating option is Oleg Sych T4 templates One issue we ran into with LINQ2SQL is the singular datacontext. If you don't need to be modular this isn't an issue.

  2. I agree with what the others said about data contracts and look at what Plinqo can generate. It may save you a lot of time.

  3. The data will work it's way up in objects usually. Like the others said make sure you keep a seem between all the layers so you don't have dependencies.

When you get to the MVVM part you will open a whole new can of worms. I don't think there are many or any books out on MVVM yet. It is still a somewhat new fad.

nportelli
A: 

Great question, I'm on the nursery slopes of the WCF/WPF learning curve so I'm in a similar position to you. My 2 cents:

  1. Haven't got into Linq to SQL, I'm old school and used to writing stored procedures and views. I'm currently using these to populate DTO classes - that is, classes with no methods, just properties to represent the data. I know I'm probably behind the curve on this.

  2. Make your BLL a WCF service - put the service contract(s) and data contract(s) in their own assembly, you can then include this in your client, where they become your model, or part of it.

  3. In your client application, include a reference to the assembly containing the service contracts and data contracts. The data contracts then become your model, your ViewModels can wrap these models and expose their properties (implement INotifyPropertyChanged for databinding).

I'm using the O'Reilly books Programming WCF Services, Learning WCF Services and Programming WPF which I'm finding pretty good. I don't know of any books specifically about MVVM but there's loads of stuff on the web.

upsidedowncreature
Why to reference the BLL assembly and not a WCF Web Service? Why is WCF necessary then?
Piotr Justyna