在asp.net MVC的,有没有使用国际奥委会注入多发性库到控制器更优雅的方式? [英] In asp.net-mvc, is there a more elegant way using IOC to inject mutiple repositories into a controller?

查看:106
本文介绍了在asp.net MVC的,有没有使用国际奥委会注入多发性库到控制器更优雅的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net MVC的网站,我使用ninject为国际奥委会和NHibernate我的ORM映射

I have an asp.net-mvc website and i am using ninject for IOC and nhibernate for my ORM mapping

这是我结合国际奥委会code:

Here is my IOC binding code:

internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>)).InRequestScope();
    }
}

和这里是我怎么做的国际奥委会在我的控制器code的例子:

and here is an example of how I am doing IOC into my controller code:

    public FAQController(IIntKeyedRepository<FAQ> faqRepository, IUnitOfWork unitOfWork)
    {
        _faqRepository = faqRepository;
        _unitOfWork = unitOfWork;
    }

问题是,到现在为止,每个控制器有它被指向,所以我只需要传递到它的仓储类...

The issue is that up until now, each controller had a single table that it was pointing to so i only needed on repository class passed into it...

现在,我有很多的表和类,它们都只是有2个字段:

Now, I have a number of tables and classes that are all just have 2 fields:


  • 标识

  • 名称

对于每个类,我只是从称为基类继承:

for each of these classes, i simply inherit from a base class called:

 BaseModel

这仅仅是:

public class BaseModel
{
    public virtual string Name { get; set; }
    public virtual int Id { get; set; }
}

我想有一个:

 StaticDataController

类,可以为每一个简单地从BaseModel继承(没有额外的字段)类完成所有的CRUD

class that can do all of the CRUD for every class that simply inherits from BaseModel (with no extra fields)

哑简单的方法是做到这一点:

The dumb simple way would be to do this:

    private readonly IIntKeyedRepository<Object1> _object1Repository;
    private readonly IIntKeyedRepository<Object2> _object2Repository;
    private readonly IIntKeyedRepository<Object3> _object3Repository;
    private readonly IIntKeyedRepository<Object4> _object4Repository;
    private readonly IIntKeyedRepository<Object5> _object5Repository;

    public StaticDataController(IIntKeyedRepository<Object1> obj1Repository, IIntKeyedRepository<Object2> obj2Repository, IIntKeyedRepository<Object3> obj3Repository, IIntKeyedRepository<Object4> obj4Repository, IIntKeyedRepository<Object5> obj5Repository)
    {
        _obj1Repository= obj1Repository;
        _obj2Repository= obj2Repository;
        _obj3Repository= obj3Repository;
        _obj4Repository= obj4Repository;
        _obj5Repository= obj5Repository;
    }

由于我路过表中作为参数传递给我的方法,我会在我的控制器中的一些switch语句基于参数的字符串来得到正确的库类。

Since I am passing the table in as a parameter to my methods, I would have to have some switch statement in my controller to get the right repository class based on the string of the parameter.

我认为必须有一个更优雅的方式来支持我试图这样做,我想看看是否有任何这里的最佳实践(控制器继承,反射等)?

I assume there must be a much more elegant way to support what I am trying to do so I wanted to see if there is any best practice here (controller inheritance, reflection, etc.)?

推荐答案

如果你需要做到这一点就意味着你的控制器做太多的事情,一个强烈的信号,它需要一个服务层。在这种情况下,我驱逐这些存储库到服务层。所以,我的控制器需要的服务,而不是多个存储库:

If you need to do this it means that your controller does too many things and a strong indication that it requires a service layer. In this case I deport those repositories into the service layer. So my controller takes a service instead of multiple repositories:

private readonly IStatisticDataService _service;

public StaticDataController(IStatisticDataService service)
{
    _service = service;
}

该服务有可能由多个原子库的CRUD方法的业务。

The service has business that could be composed of multiple atomic repository CRUD methods.

我知道,你可能会说:是的,但现在我要所有的库注入到 IStatisticDataService 接口的实现。是的,但它会更有意义聚合这些原子的CRUD操作到服务层,而不是控制。

I know that you might say: yes, but now I have to inject all those repositories into the implementation of the IStatisticDataService interface. Yes, but it would make more sense to aggregate those atomic CRUD operations into the service layer rather than the controller.

但是,如果需要,为了执行某些业务操作5个或更多资料库,也许你需要重新考虑您的域名体系结构。也许你可以在你的域模型使用的组成和以减少库的数量界定它们之间的关系。这很难不知道您的域的具体情况提供更具体的建议。

But if need 5 or more repositories in order to perform a some business operations, maybe you have to rethink your domain architecture. Probably you could use composition in your domain models and define relations between them in order to reduce the number of repositories. It's difficult to provide more concrete advice without knowing the specifics of your domain.

现在,我有很多的表和类,它们都只是有2个字段:

Now, I have a number of tables and classes that are all just have 2 fields:

大,使他们从相同的基本领域模型的所有派生,并有一个单一的存储库来为他们服务。你可以使用descriminator列,等等...

Great, make them derive all from the same base domain model and have a single repository to serve them. You could use descriminator columns, etc...

这篇关于在asp.net MVC的,有没有使用国际奥委会注入多发性库到控制器更优雅的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆