What's the best way to design classes that have CRUD functionalities? This is what I've done. I code, say for example, an Advertising class
Code:
using System;
namespace Agta.ManilaBulletin.Data
{
/// <summary>
/// Represents an advertising information.
/// </summary>
public class Advertising
{
private int id;
private string fileName;
private string description;
private bool isInactive;
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="fileName"></param>
/// <param name="description"></param>
/// <param name="isInactive"></param>
public Advertising(int id, string fileName, string description, bool isInactive)
{
this.Id = id;
this.FileName = fileName;
this.Description = description;
this.IsInactive = isInactive;
}
/// <summary>
/// Creates a new instance of Advertising.
/// </summary>
public Advertising() : this (0, "", "", false) {}
/// <summary>
/// Gets or sets the Id of the Advertising.
/// </summary>
public int Id
{
get { return id; }
set { id = value; }
}
/// <summary>
/// Gets or sets the file name of the Advertising.
/// </summary>
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
/// <summary>
/// Gets or sets the description of the Advertising.
/// </summary>
public string Description
{
get { return description; }
set { description = value; }
}
/// <summary>
/// Gets or sets the status of the Advertising. Sets
/// a boolean value of for it's status. Either active or
/// inactive.
/// </summary>
public bool IsInactive
{
get { return isInactive; }
set { isInactive = value; }
}
}
}
and extend a class having a IDataAccess interface with Create() method (just for now, could be added few more methods like Update(), Read(), etc.)
Code:
using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using Agta.ManilaBulletin.Utilities;
namespace Agta.ManilaBulletin.Data
{
/// <summary>
///
/// </summary>
public class ObservableAdvertisingAccess : Advertising, IObservable, IDataAccess
{
private bool changed = false;
private ArrayList obs = new ArrayList();
private Configuration cfg;
private ISessionFactory factory;
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="fileName"></param>
/// <param name="description"></param>
/// <param name="isInactive"></param>
public ObservableAdvertisingAccess(int id, string fileName,
string description, bool isInactive) :
base (id, fileName, description, isInactive)
{
cfg = new Configuration();
cfg.AddAssembly("webartiData");
factory = cfg.BuildSessionFactory();
}
/// <summary>
/// Creates a new instance of ObservableAdvertisingAccess.
/// </summary>
public ObservableAdvertisingAccess() : this (0, "", "", false) {}
/// <summary>
///
/// </summary>
public void Create()
{
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
session.Save(this);
transaction.Commit();
session.Close();
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
public void AddObserver(IObserver o)
{
if (o == null)
throw new NullReferenceException();
if (!obs.Contains(o))
obs.Add(o);
}
/// <summary>
///
/// </summary>
/// <param name="o"></param>
public void RemoveObserver(IObserver o)
{
obs.Remove(o);
}
/// <summary>
///
/// </summary>
public void NotifyObservers()
{
foreach (IObserver o in obs)
o.Update(this);
}
/// <summary>
///
/// </summary>
public void SetChanged()
{
changed = true;
}
/// <summary>
///
/// </summary>
public void ClearChanged()
{
changed = false;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool HasChanged()
{
return changed;
}
}
}
But isn't it a bit redundant to have Configuration at the constructor of my class? What if I have another ObservableAccess class? Should I add the Configuration setup on the constructor again?
And oh, it hangs when I unit test it. It doesn't hang though if I have the Configuration setup on the Create method. Any ideas?