示例#1
0
 public IActionResult Index(Note note)
 {
     if (!ModelState.IsValid)
     {
         return(View(note));
     }
     else
     {
         noteContext.Insert(note);
         noteContext.Commit();
         return(RedirectToAction("NotedAdded"));
     }
 }
        public ActionResult Create(ProductCategory productCategory)
        {
            if (!ModelState.IsValid)
            {
                return(View(productCategory));
            }
            else
            {
                if (context == null)
                {
                    return(View(productCategory));
                }
                context.Insert(productCategory);
                context.Commit();

                return(RedirectToAction("Index"));
            };
        }
        public ActionResult Create(Product product, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid)
            {
                return(View(product));
            }
            else
            {
                if (file != null)
                {
                    product.Image = product.Id + Path.GetExtension(file.FileName);
                    file.SaveAs(Server.MapPath("/Content/ProductImages/") + product.Image);
                }

                context.Insert(product);
                context.Commit();

                return(RedirectToAction("Index"));
            }
        }
示例#4
0
        private static void Main(string[] args)
        {
            // Using Repository Pattern
            using (IRepository <Employee> empRepository = new SQLRepository <Employee>())
            {
                empRepository.Add(new Employee {
                    Id = 1, Name = "John"
                });
                empRepository.Commit();

                // Variance => made IReadOnlyRepository() => Covariance
                DumpPeople(empRepository);
                Employee employee = empRepository.FindById(1);

                // Variance => made IWriteOnlyRepository() => Contravariance - accepts child class in IRepository<Employee>
                AddManagers(empRepository);

                // Manager is child class of Employee - Contravariance - accepts child class in IRepository<Employee>
                empRepository.Add(new Manager {
                    Id = 10, Name = "person"
                });
            }

            // Example 2
            // T and TKey implementation of RepositoryWithKey
            // Can NOT use ints since T must be of IEntity type
            // IRepositoryWithKey<int, Person> repoWithKey = new RepositoryWithKey<int, Person>();
            IRepositoryWithKey <EntityImplementation, Employee> repoWithKeyEmployee =
                new RepositoryWithKey <EntityImplementation, Employee>();

            repoWithKeyEmployee.AddItem(new EntityImplementation(1), new Employee {
                Name = "John"
            });
            repoWithKeyEmployee.AddItem(new EntityImplementation(2), new Employee {
                Name = "Steve"
            });
            repoWithKeyEmployee.AddItem(new EntityImplementation(3), new Employee {
                Name = "Bob"
            });
            Console.WriteLine(repoWithKeyEmployee.GetItem(new EntityImplementation(2)).Name);

            // Manager should also work
            IRepositoryWithKey <EntityImplementation, Manager> repoWithKeyManager =
                new RepositoryWithKey <EntityImplementation, Manager>();

            repoWithKeyManager.AddItem(new EntityImplementation(1), new Manager {
                Name = "Lisa"
            });
            Console.WriteLine(repoWithKeyEmployee.GetItem(new EntityImplementation(1)).Name);
        }