MVVM with WPF using LINQtoSQL in DAL along with BLL

My goal is to have an application that uses WPF and is a three-tier architecture. UI, BLL and DAL ... I would like to use MVVM, but I'm not sure how this works with three-tier architecture or if it is something completely different. Therefore, bearing in mind, I have a few questions:

1) LINQtoSQL: I read a lot on the Internet that say LINQ replaces your DAL and sees many articles that say that this is a bad idea. I think this is a bad idea, however, what am I doing here? What data types do I return to BLL? Iqueryable? ObservableCollection? I have no idea.

2) BLL: I would like to make this a service that runs on the server, so when I need to make changes, I donโ€™t need to redeploy the entire application, I just need to restart the service, but I donโ€™t know where to start.

3) With BLL, I think I'm confused about how data goes through all levels from DAL to interface.

I did a lot of research on the Internet and I have a bunch of things, but I have not seen anyone talking about a WPF application that uses MVVM with LINQ in DAL, using SQLMetal and BLL, which runs on the server. Can someone point me in the right direction? or maybe a book?

+7
linq-to-sql wpf mvvm
source share
4 answers

Mike,

Your question is really cool, I like it. Firstly, do not hesitate to experiment a little - each project is different, so there is no single rule that would correspond to all. Therefore, I would suggest just leaving DAL LINQ 2 SQL. This great tool will handle this, you wonโ€™t have to worry. Secondly - you mentioned 3 levels of architecture, but why is there no room for a model? Since all models are automatically generated (e.g. SQLMetal), you also don't need to worry about mappings. So, if you're not bored yet, let me answer all your 3 questions:

  • Skip DAL and carefully monitor your project - if you have a feeling that it lacks this layer, add it (it will contain LINQ2SQL queries). And the second part - you can return everything you want, but it will be convenient for you to use IEnumerable <> or IQueryable <> parameterized by your models.

  • My intuition tells me that you need WCF - in this case, you can completely wrap the whole (yes, this) whole business logic in a nice contract and implement it, no matter how you wish.

  • This is the simplest one :) Since your BLL level is actually the implementation of a contract (interface), you can create this interface to provide you with all the necessary data. For example:

Contract / Interface:

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

And that "all layers" that you talked about are reduced to a single LINQ2SQL query. If you need more logic, put it in this layer.

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

Ask if you have additional questions!

+2
source share

I will try to give some idea, although I am not an expert, I have dealt with these problems in the past.

  • LINQ to SQL is actually pretty good at what it should do, which replaces your DAL. But do not return IQueriable up to your BLL, as this will allow (or at least hint at the possibility) BLL directly request your DB. You must transfer the data object to the BLL and create the construction of the corresponding business object. Also note: LINQ itself can be used at any level (and is actually one of the best C # features). LINQ to SQL is the mechanism by which LINQ statements are passed to SQL queries.

  • BLL as a service is a natural choice. Provide a bottom-up interface to the presentation layer (WCF is a good option here).

  • BLL creates business objects based on data received from the DAL. To ensure good layer decoupling, you must use different classes for your DAL and BLL objects. Do not create a relationship between presentation level and data level.

+2
source share

Great question. I do not think that there is one place in which there are all the answers. I had very similar questions when we started a new project. MVVM is just a presentation template and does not care about all the details as you indicated. Laurent Bugnion has a good structure that glues everything together.

  • LINQ2SQL is cool, but can be cumbersome with VS08 designers. Take a look at http://plinqo.com/ to use with CodeSmith to create a DAL, and I think it will even do BLL with contracts. Another generation option is Oleg Sych T4 Templates. One problem we encountered with LINQ2SQL is a special data file. If you do not need to be modular, this is not a problem.

  • I agree that others have talked about data contracts and are looking at what Plinqo can generate. This can save you a lot of time.

  • Data will usually work in objects. Like others, make sure you keep between all layers, so you have no dependencies.

When you switch to the MVVM part, you will open a completely new can of worms. I donโ€™t think there are many more books or books on MVVM. This is still a somewhat new whim.

0
source share

Great question, I am on the slopes of the WCF / WPF child learning curve, so I am in a similar position. My 2 cents:

  • I did not get into Linq to SQL, I am an old school and used to write stored procedures and views. I am currently using them to populate DTO classes, that is, classes without methods, just properties for representing data. I know that this is probably behind the curve.

  • Make your BLL WCF services - put the contract and the service contract in your own assembly, then you can include it in your client, where they will become your model or part of it,

  • In your client application, include a link to the assembly containing service contracts and data contracts. Data contracts then become your model, your ViewModels models can wrap these models and set their properties (implement INotifyPropertyChanged for data binding).

I use O'Reilly books. Programming WCF services, training WCF services, and WPF programming, which I find pretty good. I don't know any books on MVVM, but there are a lot of downloads on the Internet.

0
source share

All Articles