Hello friends
according to the 3rd chapter of the book, I have defined following class:
Code:
[Serializable()]
public abstract class AdminBaseEOList<T> : BaseBOList<T> where T : AdminBaseEO, new()
{
public bool Save(ValidationErrors validationErros, long id)
{
// check if this object has any item
if (this.Count > 0)
{
using (TransactionScope ts = new TransactionScope())
{
using (lqAdminDataContext db = new lqAdminDataContext())
{
if (this.Save(db, ref validationErros, id))
{
// commit transaction if update was successful
ts.Complete();
return true;
}
else
{
return false;
}
}
}
}
else
{
// No items in list so return true
return true;
}
}
public bool Save(lqAdminDataContext db, ref ValidationErrors validationErrors, long id)
{
foreach (AdminBaseEO genericEO in this)
{
if (genericEO.DBAction == DBActionEnum.Save)
{
if (!genericEO.Save(db, ref validationErrors, id))
{
return false;
}
else
{
if (genericEO.DBAction == DBActionEnum.Delete)
{
if (!genericEO.Delete(db,ref validationErrors, id))
{
return false;
}
}
else
{
throw new Exception("Unknown DBAction");
}
}
}
}
return false;
}
}
now I want to write a generic class that can be used for any table that exist in the database. note that in my program every table has its own dbml file in 'lq[tablename]DataContext' pattern.
I writed following class but there is a problem: abstract classes
I receive and error message: " Should be non-abstract with a public parameterless constructor". I can't emit 'abstract' keyword because it is necessary and unavoidable.
Code:
[Serializable()]
public abstract class myEOL<A, B> : BaseBOList<A>
where A : myBaseEO<B>
where B : DataContext, new()
{
}
here is definition of MyBaseEO class
Code:
[Serializable()]
public abstract class myBaseEO<T> : BaseBO where T : DataContext, new()
{
#region Properties
public enum DBActionEnum
{
Save,
Delete
}
// note: value of 0 for ID indicates new record
// Property declaration
public DBActionEnum DBAction { get; set; }
/// <summary>
/// Default Constructor
/// </summary>
public myBaseEO()
: base()
{
// Default the action to save
DBAction = DBActionEnum.Save;
}
#endregion
#region Methods
public bool Save(ref ValidationErrors validationErrors, long id)
{
if (DBAction == DBActionEnum.Save)
{
// begin database transaction
using (TransactionScope ts = new TransactionScope())
{
// create the DataContext
using (T db = new T())
{
// now save the record
if (this.Save(db, ref validationErrors, id))
{
// commit transaction if update successful
ts.Complete();
return true;
}
else
{
return false;
}
}
}
}
else
{
throw new Exception("DBAction not save.");
}
}
public abstract bool Save(T db, ref ValidationErrors validationErrors, long id);
#region Validate
protected abstract void Validate(T db, ref ValidationErrors validationErrors);
#endregion
#region Init
public abstract void Init();
#endregion
#region Delete
protected abstract void DeleteForReal(T db);
protected abstract void ValidateDelete(T db, ValidationErrors validationErrors);
public bool Delete(ref ValidationErrors validationErros, long id)
{
if (DBAction == DBActionEnum.Delete)
{
// begin database transaction
using (TransactionScope ts = new TransactionScope())
{
// create the DataContext
using (T db = new T())
{
this.Delete(db, ref validationErros, id);
if (validationErros.Count == 0)
{
// commit the trancsaction since the delete was successful
ts.Complete();
return true;
}
else
{
return false;
}
}
}
}
else
{
throw new Exception("DBAction not delete.");
}
}
internal virtual bool Delete(T db, ref ValidationErrors validationErrors, long id)
{
if (DBAction == DBActionEnum.Delete)
{
// check if the record can be deleted. There may be referential
// integrity rules preventing it from being deleted
ValidateDelete(db, validationErrors);
if (validationErrors.Count == 0)
{
this.DeleteForReal(db);
return true;
}
else
{
// the record can not be deleted
return false;
}
}
else
{
throw new Exception("DBAction not delete.");
}
}
#endregion
#region IsNewRecord
public bool IsNewRecord()
{
return ID == 0;
}
#endregion
#region UpdateFailed
protected void UpdateFailed(ref ValidationErrors validationErrors)
{
validationErrors.Add("This record was updated by someone else while you where editing it. Your changes were not saved.");
}
#endregion
#endregion
}
here is definition of BaseEO class:
Code:
[Serializable()]
public abstract class BaseBO
{
#region Properties
public long ID { get; set; }
public string DisplayText
{
get
{
return GetDisplayText();
}
}
protected abstract string GetDisplayText();
#endregion
#region Methods
public abstract bool Load(long id);
protected abstract void MapEntityToCustomProperties(IBaseEntity entity);
public void MapEntityToProperties(IBaseEntity entity)
{
if (entity != null)
{
this.MapEntityToCustomProperties(entity);
}
}
#endregion
}
how can I solve this problem?