开发者

Integrating StructureMap and Entity Framework

I have

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

and its implementation, EfRepository is like

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

my MVC Controller factory class:

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

So in autofac I can do something like this:

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

How can I do the same in StructureMap? I don't want to have to to ge开发者_JS百科nerate all repository classes and declare like below:

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

I have tried this, but no luck

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

my service class:

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

}

always get the error:

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


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

Edit:

Try:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜