我已经定义了一个接口和一个类:
public interface IRepository<T>
{
}
public class RoleRepository:IRepository<Domain_RoleInfo>
{
}
Run Code Online (Sandbox Code Playgroud)
在这里注入:
public RoleService
{
[Inject]
public RoleService(IRepository<Domain_RoleInfo> rep)
{
_roleRep=rep;
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用Ninject执行依赖注入,说明如何绑定?
我已经编写了一个如下所示的辅助类,它可以与非泛型接口一起工作.但是如何重构它支持如上所述的通用接口?
public class RegisterNinjectModule : NinjectModule
{
public override void Load()
{
BindServices();
BindRepositories();
}
private void BindServices()
{
FindAndBindInterfaces("RealMVC.Service.Interfaces", "RealMVC.Services");
}
private void BindRepositories()
{
FindAndBindInterfaces("RealMVC.Repository.Interfaces", "RealMVC.Repositories");
}
private void FindAndBindInterfaces(string interfaceAssemblyName, string implAssemblyName)
{
//Get all interfaces
List<Type> interfaces = Assembly.Load(interfaceAssemblyName).GetTypes().AsQueryable().Where(x => x.IsInterface).ToList();
IQueryable<Type> ts = Assembly.Load(implAssemblyName).GetTypes().AsQueryable().Where(x => x.IsClass);
foreach (Type intf in interfaces) …Run Code Online (Sandbox Code Playgroud) 我对使用Lazy加载功能的Linq to SQL非常感兴趣.而在我的项目,我用AutoMapper映射数据库模型域模型(从DB_RoleInfo到DO_RoleInfo).在我的存储库代码中如下:
public DO_RoleInfo SelectByKey(Guid Key)
{
return SelectAll().Where(x => x.Id == Key).SingleOrDefault();
}
public IQueryable<DO_RoleInfo> SelectAll()
{
Mapper.CreateMap<DB_RoleInfo, DO_RoleInfo>();
return from role in _ctx.DB_RoleInfo
select Mapper.Map<DB_RoleInfo, DO_RoleInfo>(role);
}
Run Code Online (Sandbox Code Playgroud)
SelectAll方法运行良好,但是当我调用时SelectByKey,我收到错误:
方法"RealMVC.Data.DO_RoleInfo MapDB_RoleInfo,DO_RoleInfo"无法转换为SQL.
Automapper是否完全不支持Linq?
我尝试了下面的手动映射代码而不是Automapper:
public IQueryable<DO_RoleInfo> SelectAll()
{
return from role in _ctx.DB_RoleInfo
select new DO_RoleInfo
{
Id = role.id,
name = role.name,
code = role.code
};
}
Run Code Online (Sandbox Code Playgroud)
这种方法按照我想要的方式工作.