代码之家  ›  专栏  ›  技术社区  ›  mare

如何将非默认构造函数与ninject一起使用?

  •  0
  • mare  · 技术社区  · 14 年前

    如何将Ninject与接口及其实现的示例代码一起使用,如下所示:

    公共接口IRepository { //所有内容类型的通用方法 void insert(basecontentobject o); 无效更新(baseContentObject O); 空删除(字符串段塞); void删除(contenttype contenttype,string slug); IEnumerable<baseContentObject>getInstances(); BaseContentObject GetInstance(ContentType ContentType,String Slug); baseContentObject getInstance(String ContentType,String Slug); baseContentObject获取实例(字符串slug); IEnumerable<string>getSlugsForContentType(int limit=0,string query=); contentList getContentItems(); bool isuniqueslug(字符串段塞); String对象持久文件夹获取;设置; } 公共类xmlDefaultRepository:IRepository { private contenttype selectedtype; 公共XmlDefaultRepository(String ContentType) { selectedType=(contentType)enum.parse(typeof(contentType),contentType); } public void insert(models.contentClasses.baseContentObject o) { 抛出new NotImplementedException(); } /… } 公共类pagecontroller:basecontroller { 私有iRepository内容存储库获取;私有集; 公共页面控制器(iRepository存储库) { contentRepository=存储库; } / / //获取:/ {蛞蝓} //get:/pages/slug_ 公共操作结果显示(字符串段塞) { //示例存储库用法 page page=contentrepository.getInstance(slug); /… } } < /代码>

    我的代码不包含默认构造函数,因为我不需要默认构造函数(即使想要创建它,我也不能,因为我总是需要提供内容类型。

    由于逻辑上没有要提供的默认内容类型,因此无法创建默认构造函数。

    这是Ninject在尝试加载我的ASP.NET MVC页时产生的异常。

    <> <代码> >

    *激活字符串时出错

    没有匹配的绑定可用,并且类型不可自绑定。

    激活路径:

    <开始=“3”>
  • 向xmlDefaultRepository类型的构造函数的参数contentType中注入依赖项字符串
  • 将依赖关系IRepository注入到PageController类型构造函数的参数库中
  • iController请求
  • 建议:

    1. 确保已为字符串定义了绑定。
    2. 如果绑定是在模块中定义的,请确保模块已加载到内核中。
    3. 确保您无意中创建了多个内核。
    4. 如果使用自动模块加载,请确保搜索路径和筛选器正确。*

    public interface IRepository
    {
        // common methods for all content types
        void Insert(BaseContentObject o);
        void Update(BaseContentObject o);
        void Delete(string slug);
        void Delete(ContentType contentType, string slug);
        IEnumerable<BaseContentObject> GetInstances();
        BaseContentObject GetInstance(ContentType contentType, string slug);
        BaseContentObject GetInstance(string contentType, string slug);
        BaseContentObject GetInstance(string slug);
        IEnumerable<string> GetSlugsForContentType(int limit = 0, string query = "");
        ContentList GetContentItems();
        bool IsUniqueSlug(string slug);
        string ObjectPersistanceFolder { get; set; }
    }
    
    public class XmlDefaultRepository : IRepository
    {
        private ContentType SelectedType;
    
        public XmlDefaultRepository(string contentType)
        {
            SelectedType = (ContentType) Enum.Parse(typeof(ContentType), contentType);
        }
    
        public void Insert(Models.ContentClasses.BaseContentObject o)
        {
            throw new NotImplementedException();
        }
    
        // ...
    }
    
    public class PageController : BaseController
    {
        private IRepository ContentRepository { get; private set; }
    
        public PageController(IRepository repository)
        {
            ContentRepository = repository;
        }
    
        // 
        // GET: /{slug}
        // GET: /Pages/{slug}
        public ActionResult Display(string slug)
        {
            // sample repository usage
            Page page = ContentRepository.GetInstance(slug);
            // ...
        }
    }
    

    我的代码不包含默认的构造函数,因为我不需要它(即使想要创建它,我也不能,因为我总是需要提供内容类型)。

    我无法创建默认构造函数,因为逻辑上没有要提供的默认内容类型。

    这是Ninject在尝试加载我的ASP.NET MVC页时产生的异常。

    *激活字符串时出错

    没有匹配的绑定可用,并且类型不可自绑定。

    激活路径:

    1. 向xmlDefaultRepository类型的构造函数的参数contentType中注入依赖项字符串
    2. 将依赖关系IRepository注入到PageController类型构造函数的参数库中
    3. iController请求

    建议:

    1. 确保已为字符串定义绑定。
    2. 如果绑定是在模块中定义的,请确保模块已加载到内核中。
    3. 确保您没有意外地创建多个内核。
    4. 如果使用自动模块加载,请确保搜索路径和筛选器正确。*

    1 回复  |  直到 7 年前
        1
  •  3
  •   Ruben Bartelink    13 年前

    您不应该仅仅为了取悦ninject而引入默认的ctor。

    假设你使用的是V2, the semantics for how a constructor is chosen are detailed here

    (顺便说一句,默认情况下, string 类型不被视为可解析的OOTB类型,但您应该能够 Bind ContentType To 并调用该构造函数)。

    如果以上任何一项都不能让您移动,您能提供一个使用场景和/或一个详细说明您所遇到问题的异常情况吗?

    编辑:现在我还不清楚你肯定处于一种不应该添加 ConstructorArgument (可能通过 WithConstructorArgument() IILC) Bind<T>() 语句。