1

I m inserting data using controller,

SignUpcontroller.cs

[HttpPost]
public ActionResult Index(SignUpModel sm)
{

using(DataClassesDataContext dc= new DataClassesDataContext())
{
Dummytable dm= new Dummytable();
{
dm.Name=sm.password;
}
//then conncetion string and submit
}
}

and redirection

My question is, is it correct to write this code in the controller module or do i need to write it in models module, if i need to write it in models module then how to define the setter help me out

4 Answers 4

3

It is better practice to move all data access code in a data access layer. So simply put this code in a separate class that you could reference and call from your controller. For example you could define an interface that will define the different operations:

public interface IRepository
{
    void Insert(SignUpModel model);
}

and then have a specific implementation that is working with the data access technology you are using (EF for example):

public class RepositoryEF : IRepository
{
    public void Insert(SignUpModel model)
    {
        using(DataClassesDataContext dc= new DataClassesDataContext())
        {
            Dummytable dm = new Dummytable();
            dm.Name = sm.password;
        }
    }
}

and the next step is to have your controller take this repository as constructor dependency:

public class SomeController : Controller
{
    private readonly IRepository repo;
    public SomeController(IRepository repo)
    {
        this.repo = repo;
    }

    [HttpPost]
    public ActionResult Index(SignUpModel sm)
    {
        this.repo.Insert(sm);

        ...
    }
}

Now all that's left is pick up some DI framework and wire up the dependencies.

This way you have a clear separation between your controller logic and the data access layer. This would allow you to unit test the various layers of your application in separation.

Sign up to request clarification or add additional context in comments.

4 Comments

in signupmodel.cs where i have written the getter setter? is it ok to include on more class in that module?
You could define the interface and the implementation in a separate .cs files.
is interface is compulsory needed?
Problem with inserting: dc.Dummytables.InsertOnSubmit(dm);
0

first question is Where is your DataAccessLayer ?

So the better practise is write the codes in another one class for read and write database values .

The controller have only for UI logics

and you can use Interface for increase reusability reason and Unit Testing .

enter image description here

1 Comment

I have used DataClasses.dbml
0

This is not common to use for vital data storing/accessing like signup. Consider web security tools and don't work directly with this type of data. Or change your meaning to work with common data.

1 Comment

I m new with MVC i just want to know where should i put that code?
0

for the Implementation to work i jst give memory of class to interface with the help of casting from the controller

[HttpPost]
public ActionResult Index(SignUpModel sm)
{
ISignUpModel ISign= (ISignUpModel)this.sm;
ISign.Insert(sm);
}

thanks everyone else, because of you all i learn this :)

and at the SignUpModel.cs, is normal "Interface named as ISignUp with Insert method" Implementation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.