0

我在 MVC5 中使用结构图 3.1.2.138。在我的 IoC 类中,我正在尝试使用 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;

}

谁能给我一个如何将其转换容器方式的示例?

4

1 回答 1

0

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

网站注册.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>();
});
于 2015-02-04T01:12:39.027 回答