2016-01-04 36 views
5

我想玩弄(我认为是)一个工厂,根据传递给该方法的枚举创建一个存储库。看起来是这样的: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。

enter image description here

此前实施IRepository<T>type/repository已成功创建(工作代码here),任何想法?我只是开始玩弄工厂,仿制药等 - 所以如果我在做一些错误的方式,请告诉我!

回答

5

您的代码不针对此线不编译完全相同工作的原因所在:

IRepository<IEntity> repo = new Form64_9C2Repository(); 

基本上IRepository<IEntity>是不一样的IRepository<Form64_9C2>即使Form64_9C2实现IEntity

这可以工作,如果IRepository接口上T泛型参数为covariant

public interface IRepository<out T> where T : class, IEntity 
{ 
    IEnumerable<T> Read();  
} 

但不幸的是,这将意味着它只能出现在返回类型的方法,而不是作为参数。哪一个是你的UpdateDeleteCreate方法的禁用。当然,你可以定义这样的结构:

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); 
+0

哇,只是让'出T'变化很大 - 尽管我真的不明白为什么:(不幸的是我希望能够解决存储库的所有CRUD ,而不仅仅是我看到你提供了一个这样做的方式,但通过添加一个additio最终参数。我真的希望将我的存储库及其关联的实体保留在一起,以避免调用具有冲突实体的某个存储库。感谢+1,我马上会接受,假设我没有发现更类似于我真正希望完成的事情。 – Kritner