2016-04-05 10 views
1

Ich habe eine Schnittstelle, ICrudService, und einen Dienst, der die Schnittstelle, CrudService implementiert. Ich versuche, Unity für die Abhängigkeitseinfügung zu verwenden, aber ich kann nicht sehen, wie man den Typ registriert.Unity Container Dependency Injection - Wie würde ich dies registrieren?

Hier ist meine ICrudService:

public class CrudService<T> : ICrudService<T> where T : DelEntity, new() 
{ 
    protected IRepo<T> repo; 

    public CrudService(IRepo<T> repo) 
    { 
     this.repo = repo; 
    } 

    public IEnumerable<T> GetAll() 
    { 
     return repo.GetAll(); 
    } 

    public T Get(int id) 
    { 
     return repo.Get(id); 
    } 

    public virtual int Create(T item) 
    { 
     var newItem = repo.Insert(item); 
     repo.Save(); 
     return newItem.Id; 
    } 

    public void Save() 
    { 
     repo.Save(); 
    } 

    public virtual void Delete(int id) 
    { 
     repo.Delete(repo.Get(id)); 
     repo.Save(); 
    } 

    public void Restore(int id) 
    { 
     repo.Restore(repo.Get(id)); 
     repo.Save(); 
    } 

    public void BatchDelete(int[] ids) 
    { 
     foreach (var id in ids) 
     { 
      repo.Get(id).IsDeleted = true; 
     } 

     repo.Save(); 
    } 

    public void BatchRestore(int[] ids) 
    { 
     foreach (var id in ids) 
     { 
      repo.Get(id).IsDeleted = false; 
     } 

     repo.Save(); 
    } 

    public IEnumerable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false) 
    { 
     return repo.Where(predicate, showDeleted); 
    } 
} 

Und schließlich meine UnitConfig.cs, die ich nicht einmal zu bauen bekommen kann ...

:

public interface ICrudService<T> where T: DelEntity, new() 
{ 
    int Create(T item); 
    void Save(); 

    T Get(int id); 
    IEnumerable<T> GetAll(); 
    IEnumerable<T> Where(Expression<Func<T, bool>> func, bool showDeleted = false); 
} 

Hier mein CrudService ist

public class UnityConfig 
{ 
    #region Unity Container 
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() => 
    { 
     var container = new UnityContainer(); 
     RegisterTypes(container); 
     return container; 
    }); 

    /// <summary> 
    /// Gets the configured Unity container. 
    /// </summary> 
    public static IUnityContainer GetConfiguredContainer() 
    { 
     return container.Value; 
    } 
    #endregion 

    /// <summary>Registers the type mappings with the Unity container.</summary> 
    /// <param name="container">The unity container to configure.</param> 
    /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
    /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> 
    public static void RegisterTypes(IUnityContainer container) 
    { 
     // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. 
     // container.LoadConfiguration(); 

     // TODO: Register your types here 
     container.RegisterType<ICrudService<T>, CrudService<T>>(); 
    } 
} 

Was mache ich falsch in UnityConfig.cs?

Antwort

1

Was Sie erhalten, ist ein C# -Compilerfehler, der besagt, dass T unbekannt ist. Auf diese Weise können generische Typen in Unity registriert werden:

container.RegisterType(typeof(ICrudService<>), typeof(CrudService<>)); 
+0

Ehrfürchtig. Vielen Dank. – halterdev