lundi 21 mars 2016

Dependencies between CartItem, Product and ProductRepository classes

I have simple eCommerce project on MVC5 with classes like:

Product.cs

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Cost { get; set; }
}

IProductRepository.cs

public interface IProductRepository
{
    IEnumerable<Product> GetProducts();
    Product GetProduct(int id);
}

CartItem.cs

public class CartItem
{
    public Product Product { get; set; }
    public int Quantity { get; set; }
}

Cart.cs

public class Cart
{
    private List<CartItem> lineCollection = new List<CartItem>();
    // some methods like AddItem...
}

Information about cart store in session:

public class CartModelBinder : IModelBinder
{
    private const string sessionKey = "Cart";

    public object BindModel(ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        Cart cart = null;
        if (controllerContext.HttpContext.Session != null)
        {
            cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
        }
        if (cart == null)
        {
            cart = new Cart();
            if (controllerContext.HttpContext.Session != null)
            {
                controllerContext.HttpContext.Session[sessionKey] = cart;
            }
        }
        return cart;
    }
}

And for controller I used Ninject.

ProductController.cs:

public class ProductController : Controller
{
    private IProductRepository Repository { get; set; }

    public ProductController(IProductRepository repo)
    {
        this.Repository = repo;
    }

    // Some controller methods...
}

MyDependencyResolver.cs:

public class MyDependencyResolver : IDependencyResolver
{
    ...
    private void AddBindings()
    {
        ...
        kernel.Bind<IProductRepository>().ToConstant(repo);
        ...
    }
}

NinjectWebCommon.cs:

public static class NinjectWebCommon 
{
    ...
    private static void RegisterServices(IKernel kernel)
    {
        System.Web.Mvc.DependencyResolver.SetResolver(new MyDependencyResolver(kernel));
    }
}

Now all info about product store in session. I want to change CartItem to get information about the product from a repository with lazy initialization product information.

NewCartItem.cs:

public class NewCartItem
{
    private int _productId;

    private Product _product = null;

    public int ProductId
    {
        get { return _productId; }
        set
        {
            // To ensure that the Prod object will be re-created
            _product = null;
            _productId = value;
        }
    }

    public Product Product
    {
        get
        {
            // Lazy initialization - the object won't be created until it is needed
            if (_product == null)
            {
                // in this place I want to get all info about product from repository like this:
                _product = repo.GetProduct(_productId);
                // or like this
                _product = new Product(_productId);
            }
            return _product;
        }
    }
    public int Quantity { get; set; }
}

My first idea is IProductRepository object must be placed in one of Product or CartItem classes. But in my opinion this is not the best way. Please tell what need to do to get what I want.

P.s. Also I would to always get up to date information about the product from a repository.

Aucun commentaire:

Enregistrer un commentaire