PDA

View Full Version : Dependecy Injection



Future
یک شنبه 24 مرداد 1389, 05:09 صبح
سلام دوستان, کسی میدونه Dependency Injection چی هست و کجا کاربرد داره؟
منظور از unit testing چی هست؟

ricky22
یک شنبه 24 مرداد 1389, 08:25 صبح
به صورت خلاصه ترزيق وابستگي و يا dependency injection ، الگويي است جهت تزريق وابستگي‌هاي خارجي يك كلاس به آن، بجاي استفاده مستقيم از آن‌ها در درون كلاس.
براي مثال شخصي را در نظر بگيريد..........

توضیحات کامل+مثال در اینجا (http://vahidnasiri.blogspot.com/2009/12/dependency-injection.html)

Future
سه شنبه 26 مرداد 1389, 06:04 صبح
سلام دوستان,
من در پیاده سازی DI به مشکل برخوردم. وقتی کد برنامه را اجرا می کنم با این خطا مواجه میشم
No matching bindings are available, and the type is not self-bindable. Activation path: 2) Injection of dependency CommunityUserRepository into parameter _rep of constructor of type AccountController 1) Request for AccountController

این کد برنامه هست

using SportsStore.WebUI.Controllers;
using System.Web.Routing;
using Ninject.Modules;
using Ninject.Core.Modules;
using SportsStore.Domain.Abstract;
using System.Configuration;
using SportsStore.Domain.Concrete;

namespace SportsStore.WebUI.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
// A Ninject "kernel" is the thing that can supply object instances
private IKernel Kernel = new StandardKernel(new SportsStoreServices());


// ASP.NET MVC calls this to get the controller for each request
protected override IController GetControllerInstance(RequestContext context,Type controllerType)
{
//if (controllerType == null)
// return null;
//return (IController)Kernel.Get(controllerType);

return controllerType == null ? null
: (IController)Kernel.Get(controllerType);
}

// Configures how abstract service types are mapped to concrete implementations
private class SportsStoreServices : StandardModule
{
public override void Load()
{
//registered any IProductsRepository with the DI container.
Bind<IProductRepository>().To<SqlProductRepository>().WithConstructorArgument
("connectionString", ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString);

}
}
}
}