AutoMapperIntro.Controllers.Manager.GetManufacturerById C# (CSharp) Method

GetManufacturerById() public method

public GetManufacturerById ( int id ) : ManufacturerBase
id int
return ManufacturerBase
        public ManufacturerBase GetManufacturerById(int id)
        {
            // Fetch the object using its identifier
            // A DbSet collection supports the spiffy Find() method
            // Way easier syntax than SingleOrDefault(),
            // because it doesn't need a lambda expression
            var fetchedObject = ds.Manufacturers.Find(id);

            if (fetchedObject == null)
            {
                // Return null to the caller (who will also test the result)
                return null;
            }
            else
            {
                // Create and deliver a view model object
                return Mapper.Map<ManufacturerBase>(fetchedObject);

                /*
                var man = new ManufacturerBase();
                man.Id = fetchedObject.Id;
                man.Name = fetchedObject.Name;
                man.Country = fetchedObject.Country;
                man.YearStarted = fetchedObject.YearStarted;

                return man;
                */
            }
        }

Usage Example

Example #1
0
        //
        // GET: /Manufacturers/Details/5
        public ActionResult Details(int id)
        {
            // Attempt to fetch the desired object
            var fetchedObject = m.GetManufacturerById(id);

            if (fetchedObject == null)
            {
                return(RedirectToAction("index"));
            }
            else
            {
                return(View(fetchedObject));
            }
        }