集成StructureMap和实体框架

时间:2011-08-05 04:28:40

标签: .net entity-framework structuremap

我有

public interface IRepository<T> where T : EntityBase
{
}

及其实现,EfRepository就像

public partial class EfRepository<T> : IRepository<T> where T : BaseEntity
{
    ....
}

我的MVC Controller工厂类:

 public class IoCControllerFactory: DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return ObjectFactory.GetInstance(controllerType) as IController;
    }
}

所以在autofac中,我可以这样做:

builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).
        InstancePerHttpRequest();

如何在StructureMap中执行相同的操作?我不想生成所有存储库类并声明如下:

ObjectFactory.Initialize(x => {
    x.For<IRoleRepository>().Use<RoleRepository>();
    x.For<IWebSiteRepository>().Use<WebSiteRepository>();
    .....
}

我试过这个,但没有运气

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        //      BootStrapper.ConfigureDependencies();
        ObjectFactory.Initialize(x =>
        {

            x.For<IDatabaseFactory>().Use<DatabaseFactory>();

            x.Scan(y =>
            {
               y.AssemblyContainingType(typeof(IRepository<>));
                y.ConnectImplementationsToTypesClosing(typeof(IRepository<>)).
                    OnAddedPluginTypes(z => z.HybridHttpOrThreadLocalScoped());

            });

            x.For<IUnitOfWork>().Use<EFUnitOfWork>();

            //services

            x.For<ICategoryService>().Use<CategoryService>();

        });
        try
        {
            ObjectFactory.AssertConfigurationIsValid();
        }
        catch (StructureMapConfigurationException ex)
        {
            string msg = ex.ToString();
            throw;
        }

        ControllerBuilder.Current.SetControllerFactory(new IoCControllerFactory());
    }

我的服务类:

 public partial class CategoryService : ICategoryService
{
    private readonly IRepository<Category> _categoryRepository;      
    private readonly IUnitOfWork _uow;

    public CategoryService(IRepository<Category> categoryRepository, IUnitOfWork uow)
    {
        this._categoryRepository = categoryRepository;        
        this._uow = uow;
    }   

    public IList<Category> GetAllCategories(Func<Category, bool> expression)
    {
        return _categoryRepository.FindMany(expression).ToList();
    }

}

始终收到错误:

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily IoC.Repository.IRepository`1[[IoC.Model.Catalog.Category, IoC.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], IoC.Repository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

on the Controllerfactory class

Line 13:         protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
Line 14:         {
Line 15:             return ObjectFactory.GetInstance(controllerType) as IController;
Line 16:         }
Line 17:     }

Source File: E:\HCPanel\HCPanel.Web\IoCControllerFactory.cs    Line: 15

1 个答案:

答案 0 :(得分:1)

回答: StructureMap Auto registration for generic types using Scan

Advanced StructureMap: connecting implementations to open generic types

ObjectFactory.Initialize(x =>
{
     x.Scan(cfg => 
                  {
                      cfg.AssemblyContainingType(typeof(IRepository<>));
                      cfg.ConnectImplementationsToTypesClosing(typeof(IRepository<>))
                         .OnAddedPluginTypes(y => y.HybridHttpOrThreadLocalScoped())
                  });
});

编辑:

尝试:

ObjectFactory.Initialize(x =>
{
     x.Scan(cfg =>
                   {
                       cfg.RegisterConcreteTypesAgainstTheFirstInterface();
                   });
});