2011-09-28 19 views
0

我試圖創建一個通用的存儲庫模式,我可以於swtich ORM垃圾技術,但試圖與IOC容器掛鉤掛鉤通用資源庫使用IOC容器

public interface IRepository<T> 
{ 
     //members 
} 

對於NHbernate這工作得很好,當我偶然發現了這個問題

public class FNHRepository<T>: IRepository<T> 
{ //members } 

但對於實體我不得不添加where子句

public class EFRepository<T> : IRepository<T> where T : class 
{ 

如果我omiting的「where子句」我不能夠做

dbset = DataContext.Set<T>() ; 

只說引用類型可以用來爲T

問題是當我試圖鉤IRepository與國際奧委會,類,我正在以下execption

:像這樣

.RegisterType(typeof(IRepository<>), typeof(EFRepository<>)); 

如果implemeting類是有其中T我不能掛鉤接口

xception是:InvalidOperationException - 當前類型System.Web.Mvc.IControllerFactory是一個接口,無法構建。你是否缺少類型映射?

任何幫助appriciated,我想這與團結和Autofac並得到同樣的錯誤

+0

嘗試添加相同限制你的界面。 – jgauffin

+0

我原來是這樣開始的,但是當我添加'where T:class'時,ioc hooking throwing error – Madavan

+0

什麼樣的錯誤? – jgauffin

回答

0

不確定統一,但你應該能夠約束就好註冊一個泛型類。

builder.RegisterGeneric(typeof(EFRepo...)) 
    .AsImplementedInterfaces(); 

的問題,雖然是這樣的:沒有在接口上同樣的限制,這是註定要失敗:

container.Resolve<IRepo<int>>(); 
+0

我添加了builder.RegisterAssemblyTypes()。AsImplementedInterfaces();仍然得到錯誤,我認爲錯誤消失了,但仍然存在 – Madavan