簡體   English   中英

Autofac:如何使用多個類型參數注冊泛型?使用EFGenRepo <T,TKey>注冊IGenRepo <T,TKey>

[英]Autofac: How do you register a generic with multiple type arguments? Registering IGenRepo<T, TKey> with EFGenRepo<T, TKey>

我正在嘗試構建一個通用的repo並使用autofac進行測試。 我有以下界面:

public interface IGenRepo<T, TKey> where T : class
{
    IQueryable<T> Items { get; }
    T find(TKey pk);
    RepoResult delete(TKey pk);
    RepoResult create(T item);
    RepoResult update(T item);
    RepoResult save();
}

這是實現該接口的類:

public class EFGenRepo<T, TKey> : IGenRepo<T, TKey> where T : class
{
    private PortalEntities context = new PortalEntities();

    public IQueryable<T> Items { get { return context.Set<T>().AsQueryable<T>(); } }

    public T find(TKey pk){}
    public RepoResult delete(TKey pk){}
    public RepoResult create(T item){}
    public RepoResult update(T item){}
    public RepoResult save(){}
    private RepoResult save(T item){}
}

這是我正在使用的注冊:

cb.RegisterGeneric(typeof(EFGenRepo<>)).As(typeof(IGenRepo<>));

我在這一行得到的編譯錯誤是:

使用泛型類型“Domain.Concrete.EFGenRepo”需要2個類型參數。

我沒有經常使用autofac,但是當我刪除TKey泛型參數時,一切正常並且錯誤消息:“ 使用泛型類型'Domain.Concrete.EFGenRepo需要2個類型參數 ”已經消失了......使用T參數...有人可以告訴我如何正確設置它,更喜歡不改變我的IGenRepo接口和EFGenRepo類。

嘗試

RegisterGeneric(typeof(EFGenRepo<,>)).As(typeof(IGenRepo<,>));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM