You might find this entity factory appealing.
Here is the factory
public class CmsEntityFactory
{
private CmsEntityFactory()
{
}
//the factory method
public static IEntity2 GetEntity(string entityName, params object[] args)
{
string assemblyName = "EDS.DNN.CMS, Version=0.11.2004.0, Culture=neutral, PublicKeyToken=null";
string rootNameSpace = "EDS.DNN.CMS.EntityClasses.";
Assembly asm = Assembly.Load(assemblyName);
return (IEntity2)asm.CreateInstance(String.Concat(rootNameSpace, entityName),
true, BindingFlags.Default, null, args, null, null);
}
//method to fetch referenced assembly names
public static void GetReferencedAssemblyNames()
{
Assembly asm = Assembly.GetExecutingAssembly();
AssemblyName[] names = asm.GetReferencedAssemblies();
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i].FullName);
}
}
}
** Here are some unit tests**
[TestFixture()]
public class UnitTests
{
public UnitTests()
{
}
[Test]
public void GetNewEntity()
{
CmsContentEntity myContentEntity = CmsEntityFactory.GetEntity("CmsContentEntity", null) as CmsContentEntity;
Assert.IsNotNull(myContentEntity, "myContentEntity is null");
}
[Test]
public void GetNewEntityById()
{
CmsContentEntity myContentEntity = CmsEntityFactory.GetEntity("CmsContentEntity", new object[]{123}) as CmsContentEntity;
Assert.IsNotNull(myContentEntity, "myContentEntity is null");
}
[Test]
public void GetNewEntityWithValidator()
{
CmsContentValidator validator = new CmsContentValidator();
CmsContentEntity myContentEntity = CmsEntityFactory.GetEntity("CmsContentEntity",
new object[]{123,validator}) as CmsContentEntity;
Assert.IsNotNull(myContentEntity, "myContentEntity is null");
}
[Test]
public void GetReferencedAssemblyNames()
{
CmsEntityFactory.GetReferencedAssemblyNames();
}
}
Hope they help. The thing to notice is that the Factory returns IEntity2 and uses reflection to create an instance of the entity. When it comes out of the factory as IEntity 2, I cast it to CmsContentEntity. If the object returned isnt a CmsContentEntity, then the assertion would fail. If the assertion wasnt there, you could check to see what kind of entity it really is.
EDIT This might explain it better:
[Test]
public void GetNewEntityWithValidator()
{
// get an entity using the factory
IValidator validator = new CmsContentNotesValidator();
IEntity2 myEntity = CmsEntityFactory.GetEntity("CmsContentNotesEntity",
new object[]{456,validator});
Console.WriteLine(myEntity.GetType().Name);
Console.WriteLine((myEntity as CmsContentNotesEntity).NoteID.ToString());
// get another entity using the same objects
validator = new CmsContentValidator();
myEntity = CmsEntityFactory.GetEntity("CmsContentEntity", new object[]
{123,validator});
Console.WriteLine(myEntity.GetType().Name);
Console.WriteLine((myEntity as CmsContentEntity).ContentID.ToString());
}