protected void Application_Start()
{
Database.SetInitializer<AppDBContext>(new DropCreateDatabaseIfModelChanges<AppDBContext>());
// Bootstrapper.Initialise(); //autocreated with Unity.MVC4
// but instead of above Bootstraper.Initialise method, I have used my own created
// class "MyUnityConfiguration" as below line
MyUnityConfiguration.ConfigureIoCContainer();
}
namespace RepoPrac.Helper
{
public class MyUnityConfiguration
{
public static void ConfigureIoCContainer()
{
IUnityContainer container = new UnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
RegisterTypes(container);
}
private static void RegisterTypes(IUnityContainer container)
{
// container.RegisterType<IMyProjectContext, MyProjectContext>(new TransientLifetimeManager());
container.RegisterType<ICategoryRepository, CategoryRepository>(new ContainerControlledLifetimeManager());
container.RegisterType<IPersonRepository, PersonRepository>(new ContainerControlledLifetimeManager());
// or we can also mention as below
//to create registrations for all the types that implement an interface where the ITenant/Tenant naming convention is in use.
// container.RegisterTypes(
// AllClasses.FromLoadedAssemblies().Where(t=> t.Namespace== "Repository"),
// WithMappings.FromMatchingInterface,
// WithName.Default,
// WithLifetime.ContainerControlled);
}
}
}
//INTERFACE CREATION, This interface will be implemented by the repository Class which in turn
// uses the database context class for database manipulation.
public interface ICategoryRepository
{
IEnumerable<Category> GetAll();
Category Get(int id);
Category Add(Category item);
object Remove(int id);
bool Update(Category item);
IEnumerable<Category> GetActiveCategory();
}
// CLASS i.e MODEL CREATION
public class Category
{
public int CategoryID { get; set; }
[Required]
//[MinLength(7, ErrorMessage="Minimum Length Should be of 7 Characters." )]
public string CategoryName { get; set; }
public bool IsActive { get; set; }
public virtual ICollection<BlogPost> BlogPosts { get; set; }
}
// CATEGORY REPOSITORY
public class CategoryRepository : ICategoryRepository
{
private AppDBContext db = new AppDBContext();
public IEnumerable<Category> GetAll()
{
return db.Category.ToList();
}
public Category Get(int id)
{
return db.Category.Find(id);
}
public Category Add(Category item)
{
try
{
db.Category.Add(item);
db.SaveChanges();
return item;
}
catch (Exception)
{
return null;
}
}
public object Remove(int id)
{
try
{
Category category = db.Category.Find(id);
db.Category.Remove(category);
db.SaveChanges();
return new { result = true };
}
catch (Exception)
{
return new { result = false };
}
}
public bool Update(Category item)
{
db.Entry(item).State = EntityState.Modified;
db.SaveChanges();
return true;
}
public IEnumerable<Category> GetActiveCategory()
{
throw new NotImplementedException();
}
}
// NOW INSIDE THE CONTROLLER
public class CategoryController : Controller
{
private ICategoryRepository _repository;
public CategoryController(ICategoryRepository repository)
{
if (repository == null)
{
throw new ArgumentNullException("repository");
}
_repository = repository;
}
public ActionResult Index()
{
return View(_repository.GetAll()); // (db.Category.ToList());
}
public ActionResult Details(int id = 0)
{
Category category = _repository.Get(id); //db.Category.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
[HttpPost]
public ActionResult Create(Category category)
{
if (ModelState.IsValid)
{
_repository.Add(category);
return RedirectToAction("Index");
}
return View(category);
}
public ActionResult Edit(int id = 0)
{
Category category = _repository.Get(id);// db.Category.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
[HttpPost]
public ActionResult Edit(Category category)
{
if (ModelState.IsValid)
{
if (!_repository.Update(category))
{
return HttpNotFound();
}
return RedirectToAction("Index");
}
return View(category);
}
public ActionResult Delete(int id = 0)
{
Category category = _repository.Get(id);//db.Category.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
// Category category = db.Category.Find(id);
// db.Category.Remove(category);
// db.SaveChanges();
_repository.Remove(id);
return RedirectToAction("Index");
}
0 comments:
Post a Comment