Work with MVC 2.0 and a model in a separate assembly

I am new to MVC, and although there is a lot (and I mean a lot) of information that is very useful - it is very difficult to understand how to achieve my exact requirements using MVC 2.0.

I would like to configure the solution as follows:

  • Providing a web interface using the MVC 2.0 project.
  • Use the Linq to SQL class project to save data.
  • I have two separate code modules that will need to access the Linq to SQL model above, so I won’t be able to include my Linq model in SQL directly in the MVC project.
  • I also have a business logic layer in front of my Linq to SQL project.

My questions:

  • How to configure part of the model of my MVC application to point to my Linq to SQL project through my BLL?
  • How to check web applications? Can I use MVC 2.0 model validation? If not, what are the alternatives?
  • Finally (and a little to the side) - What is a ViewModel and how does it differ from the model?

So many questions. But this is an exciting new technology and problems with accessing data to the side, everything else that I have to deal with very quickly, and I think that MVC 2.0 is fantastic.

Thanks for any pointers you can provide.

+4
source share
2 answers

How to configure part of the model of my MVC application to point to my Linq to SQL project through my BLL?

Usually a repository template is used for this. The controller has a link to your repository - the repository returns your domain objects from your database. The MVC application does not know if LINQ to SQL exists.

How to check web applications? Can I use MVC 2.0 model validation? If not what alternatives?

Put view models in the MVC project. These review models may be closely aligned with your domain models, but their problem should be the presentation model. Put your data annotations to check on these view models - the MVC framework will automatically provide validation on these view models decorated with data annotations. It connects, so you can use alternatives, but with MVC 2 it baked quite well, and this includes client-side validation.

Finally (and a little to the side) - What is the ViewModel and how is it different from the model?

I partially answered this above. the shape of your domain models may not be the way you need it to reflect your views. Viewing models are great for bridging this gap. Also, even if the form exactly matches the view model, it’s still a good idea, so you can put the UI verification code and other presentation metadata there (since you don’t want something to be related to the presentation logic in your domain model) .

Here is a link to view model models .

Hope this helps.

+4
source
  • You can add a reference to objects exhibited from your BLL assembly and use them as models.

  • If you want to add validation to generated classes, use buddy classes .

  • ViewModel is a collection of model data in the form of an arbitrary form. There is exactly one for each view, since the goal of the ViewModel is to accurately draw the data needed by a particular view in a convenient and concise way.

An example would be a view containing both Order data and OrderDetail. ViewModel can contain internal links to repositories and business objects for each type. ViewModel properties combine data from these objects.

ViewModels will be useful in your case also because you want your models to be in a separate assembly. You can apply the DataAnnotations properties to the ViewModel for validation. You would make raw business object models intrinsic to your ViewModels and set up public methods for retrieving and storing data.

+1
source

All Articles