MVC5 中的结构图

Structuremap in MVC5

我在 MVC5 中使用结构图 3.1.2.138。 在我的 IoC-class 中,我正在尝试使用 Objectfactory,但它指出 Objectfactory 已过时,我应该使用 Container,但我找不到实现此工作的方法。

public static IContainer Initialize() {
        var BS_WebDataContextConnectionString = "Data Source=xxx;Initial Catalog=xxx;Integrated Security=True;MultipleActiveResultSets=True";

        ObjectFactory.Initialize(x =>
            {
                x.Scan(scan =>
                    {
                        scan.Assembly(Assembly.GetCallingAssembly());
                        scan.WithDefaultConventions();
                    });

                //DataContext
                x.For<iBS_WebDataContext>().Use<BS_WebDataContext>()
                    .Ctor<string>("connectionString")
                    .Is(BS_WebDataContextConnectionString);

                //Repositories
                x.For<IReadOnlyEvidenceTypeRepository>().Use<EF6ReadOnlyEvidenceTypeRepository>();
                x.For<IReadOnlyStatusCategoryRepository>().Use<EF6ReadOnlyStatusCategoryRepository>();



                //Services
                x.For<IEvidenceTypeService>().Use<EvidenceTypeService>();
                x.For<IStatusCategoryService>().Use<StatusCategoryService>();


            });
        return ObjectFactory.Container;

}

谁能给我一个例子,说明如何转换为容器方式

它会像这样完成(包括将您的配置移动到注册表中以保持它整洁。

WebsiteRegistry.cs

public class WebsiteRegistry : Registry
{
    public WebsiteRegistry()
    {
        x.Scan(scan =>
        {
            scan.Assembly(Assembly.GetCallingAssembly());
            scan.WithDefaultConventions();
        });

        //DataContext
        var BS_WebDataContextConnectionString = "Data Source=xxx;Initial Catalog=xxx;Integrated Security=True;MultipleActiveResultSets=True";
        x.For<iBS_WebDataContext>().Use<BS_WebDataContext>().Ctor<string>("connectionString").Is(BS_WebDataContextConnectionString);

        //Repositories
        x.For<IReadOnlyEvidenceTypeRepository>().Use<EF6ReadOnlyEvidenceTypeRepository>();
        x.For<IReadOnlyStatusCategoryRepository>().Use<EF6ReadOnlyStatusCategoryRepository>();

        //Services
        x.For<IEvidenceTypeService>().Use<EvidenceTypeService>();
        x.For<IStatusCategoryService>().Use<StatusCategoryService>();
    }
}

然后这样注册:

IContainer yourContainer = new Container();
yourContainer.Configure(c =>
{
    c.IncludeRegistry<WebsiteRegistry>();
});