views:

38

answers:

2

Hi Experts,

I am new to linq and asp.net mvc and right now i am developing a project based on Asp.net mvc using Linq to sql as a data access technology.But i am very confuse about the way of using linq to sql classes.Is it good practice to create a new class for each entity on dbml file and map between them and use that newly created class to our view? If it is bad practice how can i validate the entities using Data Annotation.

+1  A: 

I am confused on your question. I am assuming you are making your dbmls automatically by dragging/dropping, so all of your classes should already be made. I generally only make a new Model in MVC if the provided classes do not provide what I need in them.

You can use strongly typed bindings in your views to your DBML types while still seperating the DAL. I generally put my DAL in a project called ProjectName.DomainModel and the web UI in ProjectName.WebUI and reference the ProjectName.DomainModel*. Now when I am making a new View, I can stronly-type it to the external library that has my dbml in it:

Inherits="System.Web.Mvc.ViewPage<ProjectName.DomainModel.SomeObject>"

Also, for things such as validation with annotation, I keep that seperate in my DAL, but I use partial classes (these are often called buddy classes that are separated so if I refresh my dbml, the validation stays. For example, if I have the SomeObject above that is in my dbml, I can add validation requiring a Name like this:

using System.ComponentModel.DataAnnotations;

namespace ProjectName.DomainModel
{
    [MetadataType(typeof(SomeObjectValidation))]
    public partial class SomeObject { }

    public class SomeObject
    {
        [Required(ErrorMessage = "Name is Required")]
        public string Name { get; set; }
    }
}

Now this validation will be separated from the dbml and always available - just make sure it is in the same namespace.

But if the dbml classes do not meet the needs of what you want in your Model, be sure to make a new Model in the MVC project itself, that it was it is for. When you do this, remember you can simply add in your dbml classes as properties of your new Model, therefore avoiding rewriting code you already have.

naspinski
This was better made a direct comment to the question itself instead as a standalone post/reply.
XIII
Thanks for reply.I have made dbml by dragging and dropping the table on it.When i read some blogs related to mvc good practice and i found it is better to use strongly typed view.But i have a view, through which i have to perform insertion on three different tables on those cases what can i do?Do i create custom class and bind this to view model?And how i can use data annotation validation on entities which are drag and drop to dbml.
Chandra Prakash
updated my answer now that the question is clarified
naspinski
+1  A: 

Linq To Sql classes best remain in the data layer and you pass on pocos around. On these you can make metadata classes which include the dataannotations. What you also see, a best practice, is in the UI layer (MVC) so called viewmodels being used. These are classes that map information from the passed around pocos to dedicated objects which are then used in the View.

A great tool to make all that mapping easier for you is AutoMapper.

I also strongly advice to read the book ASP.NET MVC 2 in action from Manning.

XIII