1

私のプロジェクト MVC4 Unity にはこの構成があり、ドライバーの一般的な構成があります。

Bootstrapper.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;
        }
    }
}

Global.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