Generic Property and IoC (Autofac) Issues

Im doing an MVC 5 proyect using DI with IoC (Autofac) and the use of Generic Repository for the Data access part.

Now, my controller have an interface as a property injected to it called IProg_II_ModelFactory , this is the concrete class in question:

    public class Prog_II_ModelFactory : IProg_II_ModelFactory
    {
        #region Fields
        private readonly IGenericRepository<T> _genericRepository;
        #endregion

        #region Ctor
        public Prog_II_ModelFactory(
            IGenericRepository<T> genericRepository
            )
        {
            _genericRepository = genericRepository;
        }
        #endregion

        #region Methods
        public Prog_II_Model SetProgIIModelStartUp()
        {
            var model;
            model.FillList = _genericRepository.GettAll();

            //Other things to be filled.


            return model;

        }
    }

And my controller is injecting the interface of this class, here is the controller:

public class HomeController : Controller 
{
    private readonly IProg_II_ModelFactory _prog_II_ModelFactory;

    public HomeController(
        IProg_II_ModelFactory prog_II_ModelFactory
        )
    {
        _prog_II_ModelFactory = prog_II_ModelFactory;
    }

    public ActionResult Index()
    {
        var model = _prog_II_ModelFactory.SetProgIIModelStartUp();

        return View(model);
    } 
}

The problem with this is that Prog_II_ModelFactory must be generic ( Prog_II_ModelFactory<T> where T : class )

The problem with this is that, it wont stop there, then it will ask me that the controller should also be generic and on top of that as Im using Autofac, the partial class to for the binding must be generic too and the App_Start itself!.

Is there any way I can use the Generic Interface of My repository without to flow TEntity all the way to App_Start ?

I read that I can use a method? but a lot of people is against this.

Edit:

Maybe instead of the property being Out of T, I have to type it? for example:

#region Fields
private readonly IGenericRepository<Student> _genericRepository_Student;
private readonly IGenericRepository<Teacher> _genericRepository_Teacher;
#endregion

#region Ctor
public Prog_II_ModelFactory(
    IGenericRepository<Student> genericRepository_Student,
    IGenericRepository<Teacher> genericRepository_Teacher
    )
{
    _genericRepository_Student = genericRepository_Student;
    _genericRepository_Teacher = genericRepository_Teacher;
}
#endregion

This means I cant do it at runtime, I have to know to which entity at compile time, what if I have 20 tables.

Edit 2:

This is my Model:

namespace Prog_II.Models
{
    public class Prog_II_Model
    {
        #region Fields
        public StudentModel Student{ get; set; }

        public List<TurnModel> Turns{ get; set; }
        public List<CarrerModel> Carrers { get; set; }
        public List<StudentModel> Students{ get; set; }
        #endregion

        #region Ctor
        public Prog_II_Model(){}
        #endregion

    }

    public class StudentModel
    {
        #region Fields
        public string Name { get; set; }
        public string LastName{ get; set; }
        public string NumberId { get; set; }
        public string Carrer { get; set; }
        public string Turn { get; set; }
        #endregion
    }

    public class TurnModel
    {
        public string Description{ get; set; }
    }

    public class CarrerModel
    {
        public string Description{ get; set; }
    }

}

And sure enough I pass a Prog_II_Model in my view:

@using Prog_II.Models
@model Prog_II_Model

public HomeController(Prog_II_ModelRepository modelRepository)...

public class Prog_II_ModelRepository()
{
   IGenericRepository<Student> _genericRepository_Student; // get from somewhere
   public Prog_II_Model GetModel()
   {
       var model = new Prog_II_Model();
       model.Student = _genericRepository_Student.Get(); // some student
       // put the rest of data into your huge all-encompassing model
       return model;
   }
}

That's just the idea - if you put all your data in a single model object, type-specific repositories are useful inside this single model repo, but it itself has no need to be generic (as opposed to general :)

I think there's something wrong with this One Huge Model approach, but YMMV.


Is your HomeController supposed to handle multiple entity types (Student, Teacher, Janitor, Dog, Cat, ...)? Is your Model flexible enough to handle all and any of them? If yes, use a non-generic repository interface, returning an object or some base entity type. If no, and you have different models per entity type (Dog has Color and Student has Scores), then your domain model asks for controller-per-entity type, inherited, as you said, from a generic controller type.

I don't see any reason for App_Start to be generic.

链接地址: http://www.djcxy.com/p/65674.html

上一篇: 谁“杀了”我的过程,为什么?

下一篇: 通用属性和IoC(Autofac)问题