Activator.CreateInstance с универсальным хранилищем
Я пытаюсь поиграть с (как мне кажется,) фабрикой, которая создает хранилище в зависимости от перечисления, передаваемого методу. Выглядит так:
RepositoryFactory
public class RepositoryFactory
{
public IRepository<IEntity> GetRepository(FormTypes formType)
{
// Represents the IRepository that should be created, based on the form type passed
var typeToCreate = formType.GetAttribute<EnumTypeAttribute>().Type;
// return an instance of the form type repository
IRepository<IEntity> type = Activator.CreateInstance(typeToCreate) as IRepository<IEntity>;
if (type != null)
return type;
throw new ArgumentException(string.Format("No repository found for {0}", nameof(formType)));
}
}
IRepository
public interface IRepository <T>
where T : class, IEntity
{
bool Create(IEnumerable<T> entities);
IEnumerable<T> Read();
bool Update(IEnumerable<T> entities);
bool Delete(IEnumerable<T> entities);
}
FormTypes
public enum FormTypes
{
[EnumType(typeof(Form64_9C2Repository))]
Form64_9C2,
[EnumType(typeof(Form64_9BaseRepository))]
Form64_9Base
}
EnumExtensions
public static class EnumExtensions
{
/// <summary>
/// Get the Enum attribute
/// </summary>
/// <typeparam name="T">The attribute</typeparam>
/// <param name="enumValue">The enum</param>
/// <returns>The type to create</returns>
public static T GetAttribute<T>(this System.Enum enumValue)
where T : Attribute
{
FieldInfo field = enumValue.GetType().GetField(enumValue.ToString());
object[] attribs = field.GetCustomAttributes(typeof(T), false);
T result = default(T);
if (attribs.Length > 0)
{
result = attribs[0] as T;
}
return result;
}
}
Form64_9C2Repository
public class Form64_9C2Repository : IRepository<Form64_9C2>
{
public bool Create(IEnumerable<Form64_9C2> entities)
{
throw new NotImplementedException();
}
public bool Delete(IEnumerable<Form64_9C2> entities)
{
throw new NotImplementedException();
}
public IEnumerable<Form64_9C2> Read()
{
throw new NotImplementedException();
}
public bool Update(IEnumerable<Form64_9C2> entities)
{
throw new NotImplementedException();
}
}
IEntity
public interface IEntity { }
Form64_9C2 (заглушка)
public class Form64_9C2 : IEntity { }
Называя все это как:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Repository Factory Example \n\n");
Business.Factory.RepositoryFactory factory = new Business.Factory.RepositoryFactory();
// Get a 64 9C2 repository
var repo9c2 = factory.GetRepository(FormTypes.Form64_9C2);
Console.WriteLine(repo9c2);
}
}
Моя проблема моя type
всегда решает null
, Я ожидаю получить NotImplementedException
но вместо этого получаю ArgumentException
за отсутствие действительного formType.
До внедрения IRepository<T>
мой type
/repository
was successfully being created (working code here), any ideas? I'm only just getting started playing around with factories, generics, and the like - so if I'm doing something way wrong please advise!
1 ответ
Ваш код не работает по той же причине, по которой эта строка не компилируется:
IRepository<IEntity> repo = new Form64_9C2Repository();
В принципе IRepository<IEntity>
это не то же самое, что IRepository<Form64_9C2>
даже если Form64_9C2
инвентарь IEntity
,
Это могло бы сработать, если T
универсальный параметр на IRepository
интерфейс был covariant
:
public interface IRepository<out T> where T : class, IEntity
{
IEnumerable<T> Read();
}
Но, к сожалению, это будет означать, что он может появляться только как тип возвращаемого значения для методов, а не как параметр. Который не пойдет для вашего Update
, Delete
а также Create
методы. Конечно, вы можете определить такую структуру:
public interface IReadonlyRepository<out T> where T : class, IEntity
{
IEnumerable<T> Read();
}
public interface IRepository<T>: IReadonlyRepository<T> where T : class, IEntity
{
bool Update(IEnumerable<T> entities);
bool Delete(IEnumerable<T> entities);
bool Create(IEnumerable<T> entities);
}
и ваш GetRepository
метод вернуть IReadonlyRepository<IEntity>
,
Если это не работает для вас, вам понадобится дополнительный параметр, чтобы указать конкретный тип сущности, чтобы выполнить правильное приведение:
public IRepository<TEntity> GetRepository<TEntity>(FormTypes formType) where TEntity: class, IEntity
{
// Represents the IRepository that should be created, based on the form type passed
var typeToCreate = formType.GetAttribute<EnumTypeAttribute>().Type;
// return an instance of the form type repository
IRepository<TEntity> type = Activator.CreateInstance(typeToCreate) as IRepository<TEntity>;
if (type != null)
return type;
throw new ArgumentException(string.Format("No repository found for {0}", nameof(formType)));
}
}
и при вызове в дополнение к указанию типа хранилища вам нужно будет указать тип сущности:
var repo9c2 = factory.GetRepository<Form64_9C2>(FormTypes.Form64_9C2);