Usuario
Modificacion de clase usuario

Pregunta
-
Hola, tengo una apliccion desarrollada en 4 capas(presentacion, negocio, dato, y entidades), y tengo la clase usuario, esta se encuentra dentro de la capa de entidades , y quiero agregarle un campo ubicacion, donde tendria que agregarle.
public
class Usuario{ public Usuario(){} public Usuario(string login, string password,string nombre, int vendedor, bool tieneVendedor,bool activo){_login = login;
_password = password;
_nombre = nombre;
_vendedor = vendedor;
_tieneVendedor = tieneVendedor;
_activo = activo;
}
asi mismo que parametros insertaria o modificaria dentro del metodo de esta clase. Este metodo esta dentro de la capa de datos. Saludos
public class UsuarioDA : DataAccessBase{
#region Constructor /// <summary> /// Class constructor /// </summary> public UsuarioDA(){
// Nothing for now. } #endregion#region
Public Methods /// <summary> /// insert new row in the table /// </summary> /// <param name="businessObject">business object</param> /// <returns>true of successfully insert</returns> public bool Insert(Usuario businessObject){
SqlCommand sqlCommand = new SqlCommand();sqlCommand.CommandText =
"[usp_Usuario_Insert]";sqlCommand.CommandType =
CommandType.StoredProcedure; SqlTransaction sqlTran; // Use connection object of base class MainConnection.Open();sqlTran = MainConnection.BeginTransaction();
sqlCommand.Connection = MainConnection;
sqlCommand.Transaction = sqlTran;
try {sqlCommand.Parameters.Add(
new SqlParameter("@Login", SqlDbType.VarChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Login));sqlCommand.Parameters.Add(
new SqlParameter("@Password", SqlDbType.VarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Password));sqlCommand.Parameters.Add(
new SqlParameter("@Nombre", SqlDbType.VarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Nombre));sqlCommand.Parameters.Add(
new SqlParameter("@Activo", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Activo));sqlCommand.ExecuteNonQuery();
//if (businessObject.Vendedor > 0) //{ sqlCommand.CommandText = "[usp_Vendedor_Insert]";sqlCommand.Parameters.Clear();
//int intCodigoVendedor = 0; sqlCommand.Parameters.Add(new SqlParameter("@Codigo", SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Vendedor));sqlCommand.Parameters.Add(
new SqlParameter("@Nombre", SqlDbType.VarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Nombre));sqlCommand.Parameters.Add(
new SqlParameter("@Externo", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, false)); if (businessObject.TieneVendedor)sqlCommand.Parameters.Add(
new SqlParameter("@Activo", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, true)); else sqlCommand.Parameters.Add(new SqlParameter("@Activo", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, false));sqlCommand.ExecuteNonQuery();
businessObject.Vendedor =
Convert.ToInt32(sqlCommand.Parameters[0].Value);sqlCommand.CommandText =
"[usp_Usuario_Update]";sqlCommand.Parameters.Clear();
sqlCommand.Parameters.Add(
new SqlParameter("@Login", SqlDbType.VarChar, 10, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Login));sqlCommand.Parameters.Add(
new SqlParameter("@Password", SqlDbType.VarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Password));sqlCommand.Parameters.Add(
new SqlParameter("@Nombre", SqlDbType.VarChar, 100, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Nombre));sqlCommand.Parameters.Add(
new SqlParameter("@Vendedor", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Vendedor));sqlCommand.Parameters.Add(
new SqlParameter("@Activo", SqlDbType.Bit, 1, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.Activo));sqlCommand.ExecuteNonQuery();
//} sqlTran.Commit(); return true;}
catch(Exception ex){
sqlTran.Rollback();
throw new Exception("Usuario::Insert::Error occured.", ex);}
finally {MainConnection.Close();
sqlCommand.Dispose();
}
}
Todas las respuestas
-
Hola,
Mirá, si querés agregarle un campo "ubicación" a la tabla, también lo vas a tener que agregar en la clase. No sé qué tipo de dato es, pero si es un int, tenés que agregarle a la clase una línea así:
int _ubicacion;
Después, en el lugar que más te guste del constructor (por ejemplo, después de "_activo = activo"), podés agregarle "_ubicacion = ubicacion", y agregar "ubicacion" como parámetro del constructor. Te quedaría:
public Usuario(string login, string password,string nombre, int vendedor, bool tieneVendedor,bool activo, int ubicacion)
Por lo que veo de tus clases de acceso a datos, vas a tener que modificar las stored procedures para que recuperen, inserten y actualicen los valores del campo "ubicacion".
Finalmente, tendrías que modificar la clase de acceso a datos en sí misma para que agregue un parámetro cuando llames a las stored procedures que modificaste.
Espero que te sea de ayuda. Cualquier duda, consultá de nuevo.
Saludos,
Nicolás.
- Propuesto como respuesta Nicolás Colombo jueves, 12 de febrero de 2009 19:16