views:

38

answers:

1

I am trying to convert this code in my DI mapping from Unity to Structuremap but I cannot seem to get it to work. I am using Repository pattern like the one in found in shrinkr by Kazi Manzur Rashid found here http://shrinkr.codeplex.com/ Any help would be appreciated!

Unity Code:

....

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

I don't know Unity, but I'm guessing you are trying to make requests for IRepository return a ProductRepository.

In StructureMap, the code is a bit simpler:

var container = new Container(x => {
  x.Scan(scan =>
  {
      scan.TheCallingAssembly();
      scan.ConnectImplementationsToTypesClosing(typeof(IRepository<>));
    });
});
Joshua Flanagan
What I am trying to do is register each Interface that inherits IRepository<T> and the corresponding implementation in HttpContextScoped. All my repo Interfaces inherit IRepository<T> and I just did not want to have to register each like so: For<ISiteDonationRepository>().HttpContextScoped().Use<SiteDonationRepository>(); For<IUserRepository>().HttpContextScoped().Use<UserRepository>(); I think maybe my inital question was not very clear, sorry about that.
Paul