私はウェブサイトasp.net MVCソリューションを持っていました
私のコントローラーには2つのコンストラクターがありました.1つはパラメーターなしで、もう1つはパラメーターがほとんどありません。
パラメーターを使用して ctor を通過するために使用されるコードは、Unity コンテナーを使用して DI でそれらを注入します。
私のウェブサイトを別のウェブサイトに統合しました。
これで、コントローラーはパラメーターなしの ctor を介して初期化されます。
私は何が欠けていますか?私のコード:
初期化コードが実行されます。
public static class Initializer
{
private static bool isInitialize;
private static readonly object LockObj = new object();
private static IUnityContainer defaultContainer = new UnityContainer();
static Initializer()
{
Initialize();
}
public static void Initialize()
{
if (isInitialize)
return;
lock (LockObj)
{
IUnityContainer container = defaultContainer;
//registering Unity for web API
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
//registering Unity for MVC
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
container.RegisterType<ICritieriaLoader, CritieriaLoader>();
container.RegisterType<IAppsLoader, AppsLoader>();
container.RegisterType<ISaveTestToBD, SaveTestToDB>();
container.RegisterType<ITestOperations, TestOperations>();
container.RegisterType<IServicesManager, ServicesManager>();
container.RegisterType<IMamConfigurationManager, MamConfigurationsManager>();
container.RegisterType<IAppsDataManager, AppsDataManager>();
container.RegisterType<IMamConfiguration_V1Manager, MamConfiguration_V1Manager>();
container.RegisterType<ICtidProd_V1Manager, CtidProd_V1Manager>();
container.RegisterType<ICtidsAliasManager, CtidsAliasManager>();
container.RegisterType<IDalFactory, DalFactory>();
container.RegisterType<IDal<AppsDataBl>, AppsDataDal>();
container.RegisterType<IApplicationLogger, Log4NetLogger>();
container.RegisterType<IDal<MamConfigurationBL>, MamConfigurationBlDal>();
container.RegisterType<IDal<Conduit.Mam.MaMDBEntityFramework.MamConfiguration>, MamConfigurationDal>();
container.RegisterType<IDal<AddServiceBL>, NewService>();
container.RegisterType<IAlias_V1UiToEfConvertor, Alias_V1UiToEfConvertor>(new ContainerControlledLifetimeManager());
container.RegisterType<IMamConfiguration_V1UiToEfConvertor, MamConfiguration_V1UiToEfConvertor>(new ContainerControlledLifetimeManager());
container.RegisterType<ICtidProdValidator, CtidPRodValidator>(new ContainerControlledLifetimeManager());
container.RegisterType<IGeoProdValidator, GeoProdValidator>(new ContainerControlledLifetimeManager());
container.RegisterType<IConstListsProvider, ConstListsProvider>(new ContainerControlledLifetimeManager());
container.RegisterType<IEntityToUiConvertor, EntityToUiConvertor>();
if (!isInitialize)
{
isInitialize = true;
}
}
}
}
.
protected void Application_Start()
{
Log.Application.Warn("Application started", "Application started");
WebApiConfig.Configure(GlobalConfiguration.Configuration);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Initializer.Initialize();
.
[Authorize]
public class CtidProdController : SAPController
{
#region Members
private IApplicationLogger mApplicationLog { get; set; }
//private IMamConfiguration_V1Manager mMamConfigurationManager { get; set; }
private ICritieriaLoader mCritieriaLoader { get; set; }
private IAppsLoader mAppsLoder { get; set; }
private IMamConfiguration_V1UiToEfConvertor mMamConfiguration_V1UiToEfConvertor { get; set; }
private CtidProdModel mCtidProdModel { get; set; }
private IConstListsProvider mConstListsProvider { get; set; }
private ICtidProd_V1Manager mCtidProd_V1Manager { get; set; }
private ICtidsAliasManager mCtidsAliasManager { get; set; }
#endregion
#region CTOR
public CtidProdController()
{
}
public CtidProdController(IApplicationLogger logger, ICtidProd_V1Manager ctidProd_V1Manager,
ICritieriaLoader critieriaLoader, IAppsLoader appsLoder, IMamConfiguration_V1UiToEfConvertor mamConfiguration_V1UiToEfConvertor,
IConstListsProvider constListsProvider, ICtidsAliasManager ctidsAliasManager)
{
mApplicationLog = logger;
mCtidProd_V1Manager = ctidProd_V1Manager;
mCritieriaLoader = critieriaLoader;
mAppsLoder = appsLoder;
mMamConfiguration_V1UiToEfConvertor = mamConfiguration_V1UiToEfConvertor;
mConstListsProvider = constListsProvider;
mCtidsAliasManager = ctidsAliasManager;
FillModel(mConstListsProvider);
}
#endregion
}
新しいウェブサイト コード: インジェクション コード:
public class NinjectDependencyScope : IDependencyScope
{
private IResolutionRoot resolver;
internal NinjectDependencyScope(IResolutionRoot resolver)
{
Contract.Assert(resolver != null);
this.resolver = resolver;
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
public object GetService(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has already been disposed");
return resolver.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has already been disposed");
return resolver.GetAll(serviceType);
}
}
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel.BeginBlock());
}
}