applicazioni multi-tier che utilizzano L2S, WCF e Classe Base
-
27-09-2019 - |
Domanda
Un giorno ho deciso di costruire questa simpatica applicazione multi-livello con L2S e WCF. Il modello semplificato è: Database-> L2S-> Wrapper (DTO) -> applicazioni client. La comunicazione tra client e database si ottiene utilizzando oggetti di trasferimento dati che contengono oggetti entità come le loro proprietà.
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
}
Abbastanza semplice, non è vero ?. Ecco la cattura. Ognuna delle classi mappate contiene proprietà ID per sé così ho deciso di ignorare in questo modo
[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();
}
}
}
}
Wrapper per la divisione è abbastanza semplice così:
public class DivisionWrapper : BaseObjectWrapper<Division>
{
}
Ha funzionato abbastanza bene fino a quando ho mantenuto valori ID in classe mappata e la sua classe BaseObject lo stesso (che non è molto buon approccio, lo so, ma ancora), ma poi questo è successo:
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;
}
}
Quando si cerca di enumerare sulla in-memory di query si è verificata la seguente eccezione: membro della classe BaseObject.ID è mappata. Anche se sto indicando il tipo e sovrascrivendo i L2S proprietà ID non funziona. Qualche suggerimento?
Soluzione
Supponiamo che io trovato il problema. Quando si scrive
var q = table.Where(o => o.ID == tempWrapper.Entity.ID);
il compilatore implica che l'oggetto è di tipo BaseObject e quindi cerca di ottenere il suo valore ID dalla mappatura BaseObject ed è mappata. Il problema sembra essere risolto dichiarando esplicitamente il tipo:
var q = from Division div in _dc.GetTable<Division>()
where div.ID == tempWrapper.Entity.ID
select div;