-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Design issue
PostPosted: Wed May 24, 2006 10:10 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
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?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 4:34 am 
Newbie

Joined: Wed Oct 12, 2005 10:28 pm
Posts: 10
please do not add your nhibernate configuration class in every domain constructor. Please design it using singleton pattern.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 12:13 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
also, in this scenario (even with the use of singleton pattern) your interfaces are going to force you to implement the Create() method in each class. imho, it would be more efficient to create an AbstractObservableAccess class that implements the basics of CRUD. then for other class-specific data access needs you can simply extnd the Access class for that particular poco... that violates the "favor composition over inheritance" rule but in this case, i think it makes things easier to maintain...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 5:22 am 
Beginner
Beginner

Joined: Tue May 23, 2006 12:53 am
Posts: 34
I can't use an abstract observable. coz I have something like
Code:
using System;

namespace Agta.ManilaBulletin.Data
{
   /// <summary>
   ///
   /// </summary>
   public class Advertising
   {
      private int id;
      private string fileName;
      private string description;
      private bool isInactive;

      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() {}

      public virtual int Id
      {
         get { return id; }
         set { id = value; }
      }

      public virtual string FileName
      {
         get { return fileName; }
         set { fileName = value; }
      }

      public virtual string Description
      {
         get { return description; }
         set { description = value; }
      }

      public virtual bool IsInactive
      {
         get { return isInactive; }
         set { isInactive = value; }
      }
   }
}

and extend a class from it implementing an IDataAccess and an IObservable interfaces. So, I have...
Code:
using System;
using System.Collections;
using NHibernate;

namespace Agta.ManilaBulletin.Data
{
   /// <summary>
   ///
   /// </summary>
   public class ObservableAdvertisingAccess : Advertising, IObservable
   {
      private bool changed = false;
      private ArrayList obs = new ArrayList();

      private static ObservableAdvertisingAccess instance = new ObservableAdvertisingAccess();
      public static ObservableAdvertisingAccess Instance
      {
         get { return instance; }
      }

      /// <summary>
      /// Creates a new instance of ObservableAdvertisingAccess.
      /// </summary>
      public ObservableAdvertisingAccess() {}

      public void Create()
      {
         ISessionFactory factory = ConfigurationSingleton.Instance.BuildSessionFactory();
         ISession session = factory.OpenSession();
         ITransaction transaction = session.BeginTransaction();
         session.Save(this);
         transaction.Commit();
         session.Close();
      }

      public void Delete()
      {
      }

      public void Update()
      {
      }

      public void AddObserver(IObserver o)
      {
         if (o == null)
            throw new NullReferenceException();
         if (!obs.Contains(o))
            obs.Add(o);
      }

      public void RemoveObserver(IObserver o)
      {
         obs.Remove(o);
      }

      public void NotifyObservers()
      {
         foreach (IObserver o in obs)
            o.Update(this);
      }

      public void SetChanged()
      {
         changed = true;
      }

      public void ClearChanged()
      {
         changed = false;
      }

      public bool HasChanged()
      {
         return changed;
      }
   }
}

So, in this case I have
Code:
using System;
using NHibernate;
using NHibernate.Cfg;

namespace Agta.ManilaBulletin.Data
{
   /// <summary>
   ///
   /// </summary>
   public class ConfigurationSingleton
   {
      private static Configuration cfg = new Configuration();
      public static Configuration Instance
      {
         get { return cfg; }
      }

      /// <summary>
      /// Creates a new instance of ConfigurationSingleton.
      /// </summary>
      private ConfigurationSingleton()
      {
         cfg.AddAssembly("webartiData");
      }
   }
}

But still it has errors, it says
Code:
TestCase 'Agta.ManilaBulletin.Test.ObservableAdvertisingAccessTest.TestCreate'
failed: NHibernate.MappingException : Unknown entity class: Agta.ManilaBulletin.Data.ObservableAdvertisingAccess
   at NHibernate.Impl.SessionFactoryImpl.GetPersister(Type theClass)
   at NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)
   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   d:\ian\projects\csharp\webarti\webartidata\data\observableadvertisingaccess.cs(38,0): at Agta.ManilaBulletin.Data.ObservableAdvertisingAccess.Create()
   d:\ian\projects\csharp\webarti\webartidata\test\observableadvertisingaccesstest.cs(31,0): at Agta.ManilaBulletin.Test.ObservableAdvertisingAccessTest.TestCreate()

TestFixture failed:


1 passed, 1 failed, 0 skipped, took 3.84 seconds.

When I unit test it. Any help? Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 11:02 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
actually you can use an Abstract observable, you just have to re-write much of your model. but if you want to implement your common CRUD functions in each class than you should continue with your existing strategy.

as for the second question, you are missing a mapping file. Since you are inheriting Advertising in the ObservableAdvertisingAccess class, I am guessing that you might not have marked your Advertising.hbm.xml class as "Embedded Resource." to be on the saef side, you should check to make sure that all of your .hbm.xml files are marked as Embedded Resource.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.