1
\$\begingroup\$

I'm in the process of delving into different design patterns and trying to implement them into my daily work routine.

I come from an unstructured world of PHP and not understanding how to utilize it to its potential. I have recently switched over to C# and I'm trying to unlearn some bad habits I picked up through my lack of understanding.

I have some of my WebAPI project, which will grow in the future so I'm trying to plan for that now. Please review the code and see if I have implemented a repository pattern correctly. The code is 100% functional but I put it together out of a few eBooks I'm reading and a few articles I found online.

Simple interface with only one method:

public interface IProductRepository
    {
        ProductViewModel GetProduct(string ProdNum);
    }
}

Product Repository Class:

public class ProductRepository : IProductRepository
{
    private ProductMaterialEntities pd;

    public ProductRepository(ProductMaterialEntities product)
    {
        pd = product;
    }
    public ProductViewModel GetProduct(string prodNum)
    {
        var result1 = pd.products.Where(x => x.ProdNum == prodNum).FirstOrDefault();
        var result2 = pd.ProductImagePaths.Where(x => x.ImageProdNum == prodNum).FirstOrDefault() ?? new ProductImagePath();
        if (result1 != null)
        {
            ProductViewModel product = new ProductViewModel()
            {
                ProdID = result1.ProdID,
                ProdNum = result1.ProdNum,
                ProdCode = result1.ProdCode,
                .
                .
                .
                .//whole bunch of attributes
                ImageCompany = result2.ImageCompany
            };
            return product;
        }
        //return empty ProductViewModel if nothing is found
        return new ProductViewModel();
    }
}

Default controller

public class ValuesController : ApiController
    {
        private IProductRepository productRepository;
        public ValuesController()
        {
            productRepository = new ProductRepository(new ProductMaterialEntities());
        }

        [Route("Product/GetProduct/{prodNum}")]
        // Access via: http://xxxxxxxxx/Product/GetProduct/02130
        public IHttpActionResult GetProduct(string prodNum)
    {
        var productInfo = productRepository.GetProduct(prodNum);
        if (productInfo.ProdID <= 0)
        {
            return NotFound();
        }
        return Ok(productInfo);
    }   
   }
\$\endgroup\$
  • 2
    \$\begingroup\$ If you create a repository pattern which sits ontop of Entity Framework you should ask yourself: what is the purpose of using entity framework? The EF already implements a repository pattern. Why do you need a second to be implemented? This question has a split opinion whether or not to use this pattern ontop of EF but I recommend you to read as much as you can to get your mind set on one of them. This post is great read and read Chris answer : programmers.stackexchange.com/questions/180851/… \$\endgroup\$ – Marcus H Apr 29 '16 at 7:07
  • \$\begingroup\$ Interesting, thank you for the reply. I have done some research into the articles posted. Also, do you think my implementation of this design pattern is correct? The above code was really an exercise into my understanding of the concept. \$\endgroup\$ – Waragi Apr 29 '16 at 12:40
  • \$\begingroup\$ You are on the right track. The repository pattern is a pattern to introduce abstraction to your code and in this case you want an abstraction in your controller from ProductMaterialEntities but you create a new instans of the ProductMaterialEntities in your constructor of ValuesController. Here you introduce a hard coupling to the class. To avoid this read about the dependency inversion principle. After reading that take a look at Dependncy Injection and Inversion of control. stackoverflow.com/questions/62539/… \$\endgroup\$ – Marcus H Apr 29 '16 at 13:57
  • \$\begingroup\$ I did notice that, but I'm using MVC5 and as far as I have real DI isn't built into the technology. I saw there was some packages available on nuget to get around this. Perhaps my next project I'll use MVC6 with DI baked in. Thank you for your feedback. \$\endgroup\$ – Waragi Apr 29 '16 at 14:05
  • 1
    \$\begingroup\$ I think you should avoid variable names like 'pd', 'prodNum', 'result1', 'result2', and replace them with more meaningfull names. Maybe they do mean something to you (for now), but they don't mean anything to someone else. Consider using 'productContext', 'productNumber', 'product' and 'productImagePath' instead, and leave no room for misinterpretation \$\endgroup\$ – Jeroen1984 May 3 '16 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.