如何将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请求
建议:
-
确保已为字符串定义了绑定。
-
-
如果绑定是在模块中定义的,请确保模块已加载到内核中。
-
-
确保您无意中创建了多个内核。
-
-
如果使用自动模块加载,请确保搜索路径和筛选器正确。*
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页时产生的异常。
*激活字符串时出错
没有匹配的绑定可用,并且类型不可自绑定。
激活路径:
-
向xmlDefaultRepository类型的构造函数的参数contentType中注入依赖项字符串
-
将依赖关系IRepository注入到PageController类型构造函数的参数库中
-
iController请求
建议:
-
确保已为字符串定义绑定。
-
如果绑定是在模块中定义的,请确保模块已加载到内核中。
-
确保您没有意外地创建多个内核。
-
如果使用自动模块加载,请确保搜索路径和筛选器正确。*