控制器中的依赖注入-ASP MVC 4 Framework 4

时间:2018-06-21 08:42:35

标签: asp.net-mvc-4 dependency-injection asp.net-4.0

我想在我的项目中添加WebServices的依赖注入。 在 App_Start> ApiRouteConfig 中,我需要添加依赖项注入解析器吗? 当我在浏览器中打开时,在构造函数中出现错误。

我在控制器中

public class TicketController : ApiController
{
    private readonly IInstitutionManager _institution;
    public TicketController(ICharacterRepository institution)
    {
        _institution = institution;
    }
}

在界面中:

public interface IInstitutionManager {
    InstitutionItem GetInstitutionsById(int id, string guid);
}

在课堂上:

internal class InstitutionsManager : PublicAdministrationsManager
{
    internal static InstitutionsManager manager;

    private const string DEFAULT_INSTITUTION_IMAGE = "ph";

    /// <summary>
    /// Get Instance
    /// </summary>
    /// <returns></returns>
    internal static InstitutionsManager GetInstance()
    {
        if (manager == null)
        {
            manager = new InstitutionsManager();
            LoadImagesForInstitutions();
        }

        return manager;
    }

    /// <summary>
    /// Get Institution details by institutionID
    /// </summary>
    /// <param name="guid"></param>
    /// <param name="id">institutions ID</param>
    /// <returns></returns>
    InstitutionItem GetInstitutionsById(int id, string guid)
    {
        List<InstitutionItem> institutions = GetInstitutions(guid, false);
        InstitutionItem item = institutions != null ? institutions.FirstOrDefault(x => x.Id == id.ToString()) : null;

        return item;
    }
}

0 个答案:

没有答案