1 Mayıs 2018 Salı

MVC 4 Yapılandırması

MVC 4 Yapılandırması için öncelikle yeni bir MVC4 projesi başlatıyoruz. IOC kapsayıcı tercihim için StructureMap‘i kullanmak istedim.  Ancak bu bazı yapı hatalarına sebep olabiliyor. StructureMapDependencyResolver, Microsoft.Practices.ServiceLocation.IServiceLocator’ı kullanımında uyumsuz olan parametre adı: commonServiceLocator olacaktır.


Küçük bir uygulama ile Microsoft Kalıpları ve Uygulama grubundan CommonServiceLocator projesi örnek kullanımı için kullanılabilir. Bu projenin StructureMap için IServiceLocator arabiriminin StructureMap uygulaması bulunuyor. CommonServiceLocator nuget ile visual studio kurulumunu yapabiliriz. Daha sonra IServiceLocator’a  referans olarak kullanabiliriz. Kodun CommonServiceLocator uygulamasından alınmasıyla aşağıdaki örneği kullanabiliriz.


MVC 4 Yapılandırması


using System;

using System.Collections.Generic;

using System.Linq;

using Microsoft.Practices.ServiceLocation;

using StructureMap;


// This is part of MvcKickstart (https://nuget.org/packages/mvckickstart)

namespace MvcKickstart.Infrastructure


/// <summary>

/// Wrapper for IDependencyScope, so that StructureMap plays nicely with built in mvc4 dependency resolution.

/// </summary>

public class StructureMapDependencyScope : ServiceLocatorImplBase


protected readonly IContainer Container;


public StructureMapDependencyScope(IContainer container)


if (container == null)

throw new ArgumentNullException(“container”);


Container = container;


public new object GetService(Type serviceType)


if (serviceType == null)

return null;

try


return serviceType.IsAbstract

catch


return null;


/// <summary>

/// When implemented by inheriting classes, this method will do the actual work of resolving

/// the requested service instance.

/// </summary>

/// <param name=”serviceType”>Type of instance requested.</param>

/// <param name=”key”>Name of registered service you want. May be null.</param>

/// <returns>

/// The requested service instance.

/// </returns>

protected override object DoGetInstance(Type serviceType, string key)


if (string.IsNullOrEmpty(key))


return GetService(serviceType);


return Container.TryGetInstance(serviceType, key);


/// <summary>

/// When implemented by inheriting classes, this method will do the actual work of

/// resolving all the requested service instances.

/// </summary>

/// <param name=”serviceType”>Type of service requested.</param>

/// <returns>

/// Sequence of service instance objects.

/// </returns>

protected override IEnumerable<object> DoGetAllInstances(Type serviceType)


return Container.GetAllInstances(serviceType).Cast<object>();


public IEnumerable<object> GetServices(Type serviceType)


return Container.GetAllInstances(serviceType).Cast<object>();


public void Dispose()


Container.Dispose();



 


Aşağıdaki kodda , mvc’i structuremap’i bağımlılık çözümleyici olarak kullanacak şekilde ayarlayacağız. Webactivator nuget’ini visual studio’ya ekleyin ve aşağıdaki dosyayı app_start klasörünüze atın:


Örnek Kullanım


using System.Web.Mvc;

using MvcKickstart.Infrastructure;

using StructureMap;


[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourProject.IocConfig), “PreStart”, Order = -100)]

// This gets installed automatically with the MvcKickstart nuget (https://nuget.org/packages/mvckickstart)

namespace YourProject


public static class IocConfig


public static void PreStart()


// If changes need to be made to IocRegistry, please subclass it and replace the following line

ObjectFactory.Initialize(x => x.AddRegistry(new IocRegistry(typeof(IocConfig).Assembly)));

DependencyResolver.SetResolver(new StructureMapDependencyScope(ObjectFactory.Container));



 


 



MVC 4 Yapılandırması

Hiç yorum yok:

Yorum Gönder