English 中文(简体)
Ninject, Reito and DAL
原标题:Ninject, Repository and DAL

我是跨国激进党的新鲜事,存放概念和依赖性注射。

我的存放处和达格·哈马舍尔德图书馆 looks。

public interface IRepository<TEntity> where TEntity : class
{
    List<TEntity> FetchAll();
    IQueryable<TEntity> Query { get; }
    void Add(TEntity entity);
    void Delete(TEntity entity);
    void Save();
}


public class Repository<T> : IRepository<T> where T : class
{
    private readonly DataContext _db;

    public Repository(DataContext db)
    {
        _db = db;
    }

    #region IRepository<T> Members

    public IQueryable<T> Query
    {
        get { return _db.GetTable<T>(); }
    }

    public List<T> FetchAll()
    {
        return Query.ToList();
    }

    public void Add(T entity)
    {
        _db.GetTable<T>().InsertOnSubmit(entity);
    }

    public void Delete(T entity)
    {
        _db.GetTable<T>().DeleteOnSubmit(entity);
    }

    public void Save()
    {
        _db.SubmitChanges();
    }

    #endregion
}

In Global.asax file I have

    private void RegisterDependencyResolver()
    {
        var kernel = new StandardKernel();
        kernel.
            Bind(typeof(IRepository<>)).
            To(typeof(Repository<>))
            .WithConstructorArgument("db", new DataContext(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()));
        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
    }

但当我试图进入存放处时,我获得“<条码>。 反对不提及标的。 我是否正确理解保存和注射应如何发挥作用?

 public class AdminController : Controller
    {

       private readonly IRepository<User> _userRepository;

        public ActionResult Index()
        {
            var a = _userRepository.FetchAll(); //I get exception here
            return View();
        }
}
最佳回答

由于您没有制定<条码>_用户检索<>/代码>,请予以撤销。 载于<代码>。 喷雾器 构造者和尼j将注射:

public class AdminController : Controller
{
    private readonly IRepository<User> _userRepository;
    public AdminController(IRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    //...
}

https://github.com/ninject/ninject/wiki/Injection-Patterns” rel=“nofollow”>injection patterns with Ninject and .how injectionwork/a>。

问题回答

网上汇辑

<appSettings>
    <add key="RepoSetting" value="Solution.DAL.OrderRepository"/>
</appSettings>

中文不适用。

private static void RegisterServices(Ikernel Kernel)
{
    //kernl.Bind<Irepo>().To<CustRepo>();

    string name = WebConfigurationManager.AppSettings["RepoSetting"];
    Type repoToInject = Assembly.GetExecutingAssembly().GetType(name);

    kernel.Bind<ICustomerRepository>().To(repoToInject
}




相关问题
How to Inject Open Connections with an IoC

First, my scenario. I have a service, BillingService, has a constructor like this: BillingService(IInstallmentService, IBillingRepository) The InstallmentService has a constructor that looks like ...

Using the Ninject kernel as a Unit of Work object factory

So I m starting to use Ninject for dependency injection and I m wondering what people think of using a kernel as an object factory for Unit of Work type objects like Linq2Sql Datacontexts. I would ...

array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Unity constructors

I have setup unity in my project and it is working for objects that don t have constructor injection implemented on them. The issue is now I do have an object which requires a custom object as a ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

ASP.NET MVP Injecting Service Dependency

I have an ASP.NET page that implements my view and creates the presenter in the page constuctor. Phil Haack s post providing was used as the starting point, and I ll just the examples from the post ...

Authorization and Windsor

I m trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute { private readonly IUserService _userService; public MyCustomAuth(IUserService ...

热门标签