Otra de Capas
Publicado por x (445 intervenciones) el 28/02/2008 17:43:08
Se que no es la forma ideal, pero me ha funcionado, alguna idea para mejorar esto?, he estado pensando en utilizar xml, reflection, etc, de manera que se elimine la capa 1
//LIBRERIA/////////////////////////////////////////////////////////////////
public class MsSqlConnection
: ConnectionEx
{
public MsSqlConnection()
: base("mssqlserver", "mssqldatabase", "mssqlusername", "mssqlpassword", OnExceptionEventActions.CatchErrors, ExceptionCatchedEventHandler(RaisedException))
{ }
private static void RaisedException(object sender, Layers.Data.Core.Connection.ExceptionCatchedEventArgs e)
{
if ([expression])
{
e.Result = ExceptionCatchedResults.Ignore; // ExceptionCatchedResults.Cancel | ExceptionCatchedResults.Retry
}
else
{
string msg = e.CatchedException.Message;
throw new Exception(msg + " ::::::: " + e.SqlCommand);
}
}
}
---------------------------------------------------------------------------
public class Interface
: MsSqlInterface
{
public Interface(String objectname, MsSqlConnection connection)
: base(objectname, (ConnectionEx)connection)
{ }
}
//CAPA 1///////////////////////////////////////////////////////////////////
public class Employee
: Interface
{
public Employee()
: this(new MsSqlConnection())
{ }
public Employee(MsSqlConnection connection)
: base("employee", connection)
{ }
}
---------------------------------------------------------------------------
public class Plant
: Interface
{
public Plant()
: this(new MsSqlConnection())
{ }
public Plant(MsSqlConnection connection)
: base("plant", connection)
{ }
}
//CAPA 2/////////////////////////////////////////////////////////////////////
public sealed class Plants
{
public static DataTable GetPlantList()
{
return new Plant().Select();
}
public static DataRow GetPlant(long plantid)
{
SearchConditions sc = new SearchConditions();
sc.Add("id", SearchConditions.OperatorTypes.EqualTo, plantid.ToString());
return new Plant().GetRecord(sc);
}
public static DataTable Employees(long plantid)
{
SearchConditions sc = new SearchConditions();
sc.Add("plant_id", SearchConditions.OperatorTypes.EqualTo, plantid.ToString());
return new Employee().Select(sc);
}
public static bool CreatePlant(DataRow plantinfo, string[] employees)
{
bool result = false;
MsSqlConnection connection = new MsSqlConnection();
connection.Transaction.Start(true);
Plant plant = new Plant(connection);
plant.Insert(plantinfo);
if (result)
{
long plantid = plant.LastInsertID;
EMployee employee = new Employee(connection);
DataRow employeeinfo = employee.GetRecord(new SearchConditions("1=0"));
foreach(string name in employees)
{
employeeinfo["name"] = name;
result = employee.Insert(employeeinfo);
if (!result) break;
}
}
connection.Transaction.End(result, true);
return result;
}
}
// CAPA 3/////////////////////////////////////////////////////////////////////////
...
mygridview.DataSource = Plants.GetPlantList();
...
//LIBRERIA/////////////////////////////////////////////////////////////////
public class MsSqlConnection
: ConnectionEx
{
public MsSqlConnection()
: base("mssqlserver", "mssqldatabase", "mssqlusername", "mssqlpassword", OnExceptionEventActions.CatchErrors, ExceptionCatchedEventHandler(RaisedException))
{ }
private static void RaisedException(object sender, Layers.Data.Core.Connection.ExceptionCatchedEventArgs e)
{
if ([expression])
{
e.Result = ExceptionCatchedResults.Ignore; // ExceptionCatchedResults.Cancel | ExceptionCatchedResults.Retry
}
else
{
string msg = e.CatchedException.Message;
throw new Exception(msg + " ::::::: " + e.SqlCommand);
}
}
}
---------------------------------------------------------------------------
public class Interface
: MsSqlInterface
{
public Interface(String objectname, MsSqlConnection connection)
: base(objectname, (ConnectionEx)connection)
{ }
}
//CAPA 1///////////////////////////////////////////////////////////////////
public class Employee
: Interface
{
public Employee()
: this(new MsSqlConnection())
{ }
public Employee(MsSqlConnection connection)
: base("employee", connection)
{ }
}
---------------------------------------------------------------------------
public class Plant
: Interface
{
public Plant()
: this(new MsSqlConnection())
{ }
public Plant(MsSqlConnection connection)
: base("plant", connection)
{ }
}
//CAPA 2/////////////////////////////////////////////////////////////////////
public sealed class Plants
{
public static DataTable GetPlantList()
{
return new Plant().Select();
}
public static DataRow GetPlant(long plantid)
{
SearchConditions sc = new SearchConditions();
sc.Add("id", SearchConditions.OperatorTypes.EqualTo, plantid.ToString());
return new Plant().GetRecord(sc);
}
public static DataTable Employees(long plantid)
{
SearchConditions sc = new SearchConditions();
sc.Add("plant_id", SearchConditions.OperatorTypes.EqualTo, plantid.ToString());
return new Employee().Select(sc);
}
public static bool CreatePlant(DataRow plantinfo, string[] employees)
{
bool result = false;
MsSqlConnection connection = new MsSqlConnection();
connection.Transaction.Start(true);
Plant plant = new Plant(connection);
plant.Insert(plantinfo);
if (result)
{
long plantid = plant.LastInsertID;
EMployee employee = new Employee(connection);
DataRow employeeinfo = employee.GetRecord(new SearchConditions("1=0"));
foreach(string name in employees)
{
employeeinfo["name"] = name;
result = employee.Insert(employeeinfo);
if (!result) break;
}
}
connection.Transaction.End(result, true);
return result;
}
}
// CAPA 3/////////////////////////////////////////////////////////////////////////
...
mygridview.DataSource = Plants.GetPlantList();
...
Valora esta pregunta


0