Is it correct to use an object to call a method where it will be the parameter?

Asked

Viewed 81 times

3

I am creating a small project in without Entity Framework and I have a question at the time of passing an object as parameter, it is correct to pass the same object that calls the method as parameter?

Code:

[HttpPost]
public ActionResult Edit(Movie movie)
{
    movie.EditMovie(movie);
    return RedirectToAction("Filmes");
}

This is the method that will receive the object

public void EditMovie(Movie film)
{
    Conn conexao = new Conn();

    string strUpdate = "UPDATE tbMovie SET movieName=@nameMovie, movieYear=@yearMovie WHERE movieId=@idMovie";
    MySqlCommand cmd = new MySqlCommand(strUpdate, conexao.ConectionData());
    
    cmd.Parameters.Add("@idMovie", MySqlDbType.VarChar).Value = film.MovieId;
    cmd.Parameters.Add("@nameMovie", MySqlDbType.VarChar).Value = film.MovieName;
    cmd.Parameters.Add("@yearMovie", MySqlDbType.VarChar).Value = film.MovieYear;
    cmd.ExecuteNonQuery();

    conexao.ConectionData();
}

1 answer

3


Maybe, but almost always not, probably has a mistake of design there.

You should see why you have a parameter in this method if the intention is to modify the object itself. Or see why it has a necessary parameter and by chance it can be the object itself.

Looking over the impression that gives is that this parameter does not make sense.

Now, if the design is wrong and you can not do anything about, and pass as argument the object itself solves the problem contentedly, can not say that is wrong, within the restrictions imposed.

With editing you can talk more. Really this parameter does not make the slightest sense, the method should not have one and should only use the this as an object.

The code seems to have other problems, but it’s not the focus of the question and I can’t say, so I won’t talk about.

This is very basic, so before using MVC and doing something more sophisticated my suggestion is to study the fundamentals of programming. No house standing without foundation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.