1

我的项目 MVC4 Unity 中有这个配置和驱动程序的通用配置。

引导程序.cs

namespace MyProyect.Web
{
    public static class Bootstrapper
    {
        public static void Initialise()
        {
            var container = BuildUnityContainer();
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }

        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();

            container.RegisterType<MyProyect.Model.DataAccessContract.ICountryDao, MyProyect.DataAccess.CountryDao>();
            container.RegisterType<MyProyect.Model.DataAccessContract.IContactDao, MyProyect.DataAccess.ContactDao>();

            return container;
        }
    }
}

全球.asax:

    protected void Application_Start()
    {
        ...  
        Bootstrapper.Initialise();
        ...  
    }

GenericModelBinder.cs:

namespace MyProyect.Web.ModelBinder
{
    public class GenericModelBinder : DefaultModelBinder//, IValueProvider
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var resolver = (Unity.Mvc4.UnityDependencyResolver)DependencyResolver.Current;
            if (modelType == null)
            {
                return base.CreateModel(controllerContext, bindingContext, null);
            }

            return resolver.GetService(modelType);
        }
    }
}

现在我需要解决解决方案中另一个项目中的依赖关系。我的问题是如何才能识别另一个项目中的 Unity 设置?我目前有这个类,但当前配置没有引入 MVC 项目。

namespace MyProyect.Model.ListData
{
    public abstract class ListDataGenericResolver
    {
        protected T ResolverType<T>()
        {
            IUnityContainer container = new UnityContainer();
            UnityServiceLocator locator = new UnityServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => locator);

            //return unity.Resolve<T>();
            return (T)container.Resolve(typeof(T));
        }
    }
}

这是一个如何使用的例子ListDataGenericResolver

namespace MyProyect.Model.ListData.Types
{
    public class CountryListData : ListDataGenericResolver, IGetListData
    {
        private readonly ICountryDao countryDao;
        private string defaultValue;

        public CountryListData(object defaultValue)
        {
            // Resolver
            this.countryDao = this.ResolverType<ICountryDao>();
            this.defaultValue = defaultValue == null ? string.Empty : defaultValue.ToString();
        }

        public IList<SelectData> GetData()
        {
            var data = this.countryDao.GetAllCountry(new Entity.Parameters.CountryGetAllParameters());
            return data.Select(d => new SelectData
                {
                    Value = d.CountryId.ToString(),
                    Text = d.Description,
                    Selected = this.defaultValue == d.CountryId.ToString()
                }).ToList();
        }
    }
}

谢谢你。

4

1 回答 1

1

这是我如何做到这一点的。

在 Application_Start 中,我创建了统一容器。我有一个自定义库,用于我通过 NuGet 导入的所有 MVC 项目,所以我调用它的 configure 方法,然后调用另一个项目的 Configure() 方法。您可以简单地省略自定义库并在此处添加该代码。所有这些都让我的 Application_Start 保持整洁。

protected void Application_Start()
{
    // Standard MVC setup
    // <removed>

    // Application configuration 
    var container = new UnityContainer();
    new CompanyName.Mvc.UnityBootstrap().Configure(container);
    new AppName.ProjectName1.UnityBootstrap().Configure(container);
    new AppName.ProjectName2.UnityBootstrap().Configure(container);

    // <removed>
}

这是自定义 MVC 库的 UnityBootstrap 类的代码

namespace CompanyName.Mvc
{
    /// <summary>
    /// Bootstraps <see cref="CompanyName.Mvc"/> into a Unity container.
    /// </summary>
    public class UnityBootstrap : IUnityBootstrap
    {
        /// <inheritdoc />
        public IUnityContainer Configure(IUnityContainer container)
        {
            // Convenience registration for authentication
            container.RegisterType<IPrincipal>(new InjectionFactory(c => HttpContext.Current.User));

            // Integrate MVC with Unity
            container.RegisterFilterProvider();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            return container;
        }
    }
}

然后,在其他项目中,我有一个 UnityBootstrap,它是从 Application_Start 调用的:

项目名称1:

namespace AppName.ProjectName1
{
    public class UnityBootstrap : IUnityBootstrap
    {
        public IUnityContainer Configure(IUnityContainer container)
        {
            return container.RegisterType<IDocumentRoutingConfiguration, DocumentRoutingConfiguration>();
        }
    }
}

ProjectName2: - 你可以在这里看到,这个依赖于另一个库中的一些其他项目,它正在调用它们的 Configure() 方法来设置它们......

namespace AppName.ProjectName2
{
    public class UnityBootstrap : IUnityBootstrap
    {
        public IUnityContainer Configure(IUnityContainer container)
        {
            new CompanyName.Security.UnityBootstrap().Configure(container);
            new CompanyName.Data.UnityBootstrap().Configure(container);

            container.RegisterSecureServices<AuthorizationRulesEngine>(typeof(UnityBootstrap).Assembly);

            return container
                .RegisterType<IAuthorizationRulesEngine, AuthorizationRulesEngine>()
                .RegisterType<IDateTimeFactory, DateTimeFactory>()
                .RegisterType<IDirectoryInfoFactory, DirectoryInfoFactory>()
                .RegisterType<IDirectoryWrapper, DirectoryWrapper>()
                .RegisterType<IEmailService, EmailService>()
                .RegisterType<IEntryPointService, EntryPointService>();
        }
    }
}

这是上面代码中使用的 IUnityBootstrap 接口(供您参考)

/// <summary>
/// Defines a standard interface for bootstrapping an assembly into a Unity container.
/// </summary>
public interface IUnityBootstrap
{
    /// <summary>
    /// Registers all of the assembly's classes to their public interfaces and performs any other necessary configuration.
    /// </summary>
    /// <param name="container">The Unity container instance to configure.</param>
    /// <returns>The same IUnityContainer object that this method was called on.</returns>
    IUnityContainer Configure(IUnityContainer container);
}

我希望这能够帮到你。

于 2013-04-18T18:43:58.023 回答