Hi all, I have a question about an AbstractFactory class I need and I am using NHibernate version 2.0.1.GA to reach the goal.
I want to have a factory for different classes based on IFactory. At the moment it looks like this:
Code:
public class AbstractFactory<T> where T : IFactory
{
private static AbstractFactory<T> instance = null;
public static AbstractFactory<T> Instance
{
get
{
if (instance == null)
{
instance = new AbstractFactory<T>();
}
return instance;
}
}
public T Get(string name)
{
T inst = default(T);
// look if there is already an instance with
// the wanted name.
foreach (T t in items)
{
if (t.Name.Equals(name))
{
inst = t;
break;
}
}
if (inst == null)
{
// create instance of T
object obj = System.Activator.CreateInstance(typeof(T));
inst = (T)obj;
inst.Name = name;
items.Add(inst);
}
return inst;
}
private IList<T> items = new List<T>();
}
And I want to use it like this:
Code:
ExchangableContainer container = new ExchangableContainer();
// saving of a new created instance of ExchangableContainer must fail,
// because we need to set some members of the class.
ClassTester<ExchangableContainer>.SaveMustFail(container);
// define a new container type
ExchangableContainerType type = AbstractFactory<ExchangableContainerType>.Instance.Get("Restmüll");
type.Capacity = 1000;
container.Type = type;
ClassTester<ExchangableContainer>.SaveMustSucceed(container);
So in the end I need to have one Factory for some Types. Each of this type should be saved in a different table e.g. list_<objname>. What kind of Nhibernate.Attributes do I need to use? If you need more details, just ask.
thanks,
ac