को जोड़ने के लिए क्या @Poco कहा, यहाँ है हल्के अपने नियंत्रकों रखने के लिए एक अतिरिक्त परत लागू करेगा का एक उदाहरण है एक उदाहरण:
Foo.Common.Repositories
में (Foo.Common
परियोजना के अंदर):
public interface IRepository<T>
{
IEnumerable<T> GetAll();
void Update(T entity);
void Add(T entity);
void Delete(T entity);
void Save();
}
public interface IUserRepository : IRepository<User>
{
void GetByCredentials(string username, string password);
}
अंदर Foo.Data.Repositories
(Foo.Data
अंदर परियोजना):
public class UserRepository
{
// ... other methods/properties snipped.
public IEnumerable<User> GetAll()
{
// Where this.Users would be L2Sql or Code-First... or some other ORM.
return from u in this.Users orderby u.Name select u;
}
}
फिर अपने वास्तविक
Foo.Web
अंदर
:
public class UserController : Controller
{
private readonly IUserRepository userRepository;
public UserController(IUserRepository userRepository)
{
this.userRepository = userRepository;
}
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult List()
{
var users = this.userRepository.GetAll();
return this.View(users);
}
}
और अपने अंदर Global.asax
आप IUserRepository
हल करने Ninject या कुछ अन्य आईओसी कंटेनर होगा:
public static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUserRepository>().To<UserRepository>();
}
protected void Application_Start()
{
var kernel = new StandardKernel();
AreaRegistration.RegisterAllAreas();
MvcApplication.RegisterGlobalFilters(GlobalFilters.Filters);
MvcApplication.RegisterRoutes(RouteTable.Routes);
MvcApplication.RegisterServices(kernel);
// I'm using MVC3 here:
DependencyResolver.SetResolver(new NinjectResolver(kernel));
}