I have a structures a project this way: Data Layer - for models MVC Website - for all other mvc website related.
I want my controller to inject the database context and the service at the same time and still ensure dependency injection.
Here's my code My Controller:
private CMSDB _db;
public HomeController(CMSDB db)
{
_db = db;
}
My Database Context
public class CMSDB : DbContext
{
public CMSDB() : base("DefaultConnection"){ }
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<UserDatails> Users { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Module> Modules { get; set; }
}
Model in a separate project
public class Module : IModule
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual DateTime? DateCreated { get; set; }
public virtual string ModuleDescription { get; set; }
}
My interface in the same project with the Model
public interface IModule
{ }
I want my controller to inject the CMSDB and the IModule at the same time and still ensure dependency injection.
The important factor is been able to calve any other solution (a portal maybe) from this later. All opinion is welcome.
make sure you are creating the mapping this should be running on startup
public static class ContainerBootstrapper
{
public static void BootstrapStructureMap()
{
// Initialize the static ObjectFactory container
ObjectFactory.Initialize(x =>
{
x.ForRequestedType<iNTERFACE>().TheDefaultIsConcreteType<ContreteClass>();
});
}
}
then
iNTERFACE objectINMVCController = ObjectFactory.GetInstance<iNTERFACE>();