0
我想將我的DI映射中的代碼從Unity轉換爲結構映射,但我似乎無法讓它工作。我正在使用存儲庫模式,像在卡斯Manzur拉希德在這裏找到收縮的發現在這裏http://shrinkr.codeplex.com/任何幫助,將不勝感激!統一到結構圖幫助
統一編碼:
....
private static readonly Func<LifetimeManager> perRequest =() => new PerRequestLifetimeManager();
....
IBuildManager buildManager = container.Resolve<IBuildManager>(); RegisterRepositories(buildManager, container);
....
private static void RegisterRepositories(IBuildManager buildManager, IUnityContainer container)
{
Type genericRepositoryType = typeof(IRepository<>);
IEnumerable<Type> repositoryContractTypes = buildManager.PublicTypes.Where(type => (type != null) && type.IsInterface && type.GetInterfaces().Any(interfaceType => interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition().Equals(genericRepositoryType))).ToList();
foreach (Type repositoryImplementationType in buildManager.ConcreteTypes.Where(implementationType => repositoryContractTypes.Any(contractType => contractType.IsAssignableFrom(implementationType))))
{
foreach (Type repositoryInterfaceType in repositoryImplementationType.GetInterfaces())
{
container.RegisterType(repositoryInterfaceType, repositoryImplementationType, perRequest());
}
}
}
我所試圖做的是註冊每個繼承IRepository接口和HttpContextScoped相應的實施。我所有的回購接口繼承IRepository ,我只是不想註冊每個像這樣:對於()。HttpContextScoped()。使用();對於()HttpContextScoped()使用()。;我認爲也許我的初始問題不是很清楚,對此感到遺憾。 –
Paul
2010-08-26 15:16:49