2006-11-14 14:33:33 +00:00

100 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Csla;
using System.Data;
using Csla.Data;
using System.IO;
using log4net;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Volian.CSLA.Library
{
[Serializable()]
public class VEReadOnlyListBase<T, C> : ReadOnlyListBase<T, C>
where T : VEReadOnlyListBase<T, C>
where C : VEReadOnlyBase<C>, new()
{
#region Log4Net
protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Authorization Rules
public static bool CanGetObject()
{
// TODO: customize to check user role
//return ApplicationContext.User.IsInRole("");
return true;
}
#endregion
#region Generic Factory Methods
public static T Get(int id)
{
return DataPortal.Fetch<T>(new Criteria(id));
}
protected VEReadOnlyListBase()
{ /* require use of factory methods */ }
#endregion
#region Generic Data Access
[Serializable()]
protected class Criteria
{
private int _id;
public int Id
{
get { return _id; }
}
public Criteria(int id)
{
_id = id;
}
}
protected void DataPortal_Fetch(Criteria criteria)
{
this.RaiseListChangedEvents = false;
// if (log.IsDebugEnabled) log.Debug(this.GetType().ToString());
try
{
using (SqlConnection cn = Database.VEPROMS_SqlConnection)
{
using (SqlCommand cm = cn.CreateCommand())
{
SetupSelect(cm, criteria);
SqlDataReader drRaw = cm.ExecuteReader();
using (SafeDataReader dr = new SafeDataReader(drRaw))
{
IsReadOnly = false;
while (dr.Read())
{
CreateItem(dr);
}
IsReadOnly = true;
}
}
}
}
catch (Exception ex)
{
//System.Diagnostics.StackFrame[] sf = new System.Diagnostics.StackTrace().GetFrames();
//string sMsg = string.Format("{0}.{1} Exception: {2} \r\n Message: {3}\r\n Inner Exception:{4}",
// this.GetType().ToString(), sf[0].GetMethod().Name,
// ex.GetType().ToString(), ex.Message, ex.InnerException);
//System.Diagnostics.EventLog.WriteEntry("VEReadOnlyListBase", sMsg);
//Console.WriteLine(sMsg);
if(log.IsErrorEnabled)log.Error("Fetch Error", ex);
}
this.RaiseListChangedEvents = true;
}
#endregion
#region Item Specific Virtual Data Access
protected virtual void SetupSelect(SqlCommand cm, Criteria criteria) { ;}
protected virtual void CreateItem(SafeDataReader dr) { ;}
#endregion
}
}