2016-04-05 15 views
1

Arabirim, ICrudService ve arabirimi uygulayan bir hizmetim var CrudService. Bağımlılık enjeksiyon amaçları için Unity'yi kullanmaya çalışıyorum ama türün nasıl kaydedileceğini anlayamıyorum.Birlik Konteynır Bağımlılığı Enjeksiyonu - Bunu Nasıl Kaydederim?

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); 
    } 
} 

Ve son olarak, ben bile inşa etmek alamayan benim UnitConfig.cs ...

: Burada
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); 
} 

benim CrudService: İşte

benim ICrudService olduğunu
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>>(); 
    } 
} 

UnityConfig.cs'de neyi yanlış yapıyorum?

cevap

1

Ne elde edeceğiniz, T'un bilinmediğini belirten bir C# derleyicisi hatasıdır. Bu, Unity:

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

Müthiş olarak genel türleri kaydetmenin yoludur. Teşekkür ederim. – halterdev

İlgili konular