aplicaciones de varios niveles utilizando L2S, WCF y base de la Clase
-
27-09-2019 - |
Pregunta
Un día decidí construir esta aplicación de varios niveles bonita utilizando L2S y WCF. El modelo simplificado es: base de datos-> L2S-> Paquete de oferta (DTO) -> Aplicación de Cliente. La comunicación entre el cliente y la Base de datos se consigue mediante el uso de objetos de transferencia de datos que contienen objetos de entidad como sus propiedades.
abstract public class BaseObject
{
public virtual IccSystem.iccObjectTypes ObjectICC_Type
{
get { return IccSystem.iccObjectTypes.unknownType; }
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_ID", AutoSync = AutoSync.OnInsert, DbType = "BigInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order = 1)]
public virtual long ID
{
//get;
//set;
get
{
return _ID;
}
set
{
_ID = value;
}
}
}
[DataContract]
public class BaseObjectWrapper<T> where T : BaseObject
{
#region Fields
private T _DBObject;
#endregion
#region Properties
[DataMember]
public T Entity
{
get { return _DBObject; }
set { _DBObject = value; }
}
#endregion
}
Bastante simple, ¿verdad ?. Aquí está la trampa. Cada una de las clases asignadas contiene en sí la propiedad ID, así que decidí anularlo como esto
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Divisions")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class Division : INotifyPropertyChanging, INotifyPropertyChanged
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=1)]
public override long ID
{
get
{
return this._ID;
}
set
{
if ((this._ID != value))
{
this.OnIDChanging(value);
this.SendPropertyChanging();
this._ID = value;
this.SendPropertyChanged("ID");
this.OnIDChanged();
}
}
}
}
Envoltura para la división es bastante sencillo así:
public class DivisionWrapper : BaseObjectWrapper<Division>
{
}
Se trabajó bastante bien, siempre y cuando me quedé valores de ID en clase mapeada y su clase BaseObject el mismo (que no es muy buena aproximación, lo sé, pero aún así), pero luego ocurrió esto:
private CentralDC _dc;
public bool UpdateDivision(ref DivisionWrapper division)
{
DivisionWrapper tempWrapper = division;
if (division.Entity == null)
{
return false;
}
try
{
Table<Division> table = _dc.Divisions;
var q = table.Where(o => o.ID == tempWrapper.Entity.ID);
if (q.Count() == 0)
{
division.Entity._errorMessage = "Unable to locate entity with id " + division.Entity.ID.ToString();
return false;
}
var realEntity = q.First();
realEntity = division.Entity;
_dc.SubmitChanges();
return true;
}
catch (Exception ex)
{
division.Entity._errorMessage = ex.Message;
return false;
}
}
Al tratar de enumerar sobre la consulta en memoria se produjo la siguiente excepción: miembro de la clase BaseObject.ID es sin asignar. A pesar de que estoy indicando el tipo y sustituyendo los L2S propiedad ID no funciona. ¿Alguna sugerencia?
Solución
Supongamos que yo encontré el problema. Al escribir
var q = table.Where(o => o.ID == tempWrapper.Entity.ID);
el compilador implica que el objeto es de tipo BaseObject y por lo tanto intenta su valor de identificador del mapeo BaseObject y es sin asignar. El problema parece estar resuelto por la que se declara explícitamente el tipo:
var q = from Division div in _dc.GetTable<Division>()
where div.ID == tempWrapper.Entity.ID
select div;