views:

55

answers:

2

Hello SO,

I'm using LINQtoSQL to create cascading deletes IE: if Category is deleted then all the Products in that category are also deleted.

I have a Products repository set up already (IProductsRepository) and now I'm working in my Category repository to add the business domain logic for the cascade.

Products repository has this method :

public void DeleteProducts(IList<Product> Products)
    {
        foreach (Product product in Products)
        {
            productsTable.DeleteOnSubmit(product);
        }
        productsTable.Context.SubmitChanges();
    }

Then I am creating a delete category method in my Category repository:

    public void DeleteCategory(Category category)
    {
        IProductsRepository productsRepository;

        var CascadeDeleteProducts =
            from Products in productsRepository.Products
            where Products.CategoryID == category.CategoryID
            select Products;

        productsRepository.DeleteProducts(CascadeDeleteProducts.ToList());

        categoriesTable.DeleteOnSubmit(category);
        categoriesTable.Context.SubmitChanges();
    }

Visual Studio 2010 gives me an error in the above function for this line: from Products in productsRepository.Products. It says

Use of unassigned local variable 'productsrepository'

What could be causing this error? I'm creating the products repository through DI with this line: IProductsRepository productsRepository;. What am i doing wrong?

Edit

I neglected to mention that I am using Ninject to instatiate the product repository. So I beleieve that this line:IProductsRepository productsRepository; is effectually declaring and initializing the product repository.

+1  A: 

You're creating an instance of your repository, but you never initialize it.

The line should look something like:

IProductRepository productsRepository = new ProductRepository();

Either that or you should allow the caller to inject (either method or constructor) the repository:

public void DeleteCategory(Category category, 
    IProductRepository productRepository)
{
}

UPDATE

If you're using nInject for your dependency injection, your method declaration should look like (this assumes you have nInject configured correctly to Inject the Dependency):

[Inject]
public void DeleteCategory(Category category,
    IProductRepository productRepository)
{
}
Justin Niessner
Ummm... I'm new to Dependency injection. but I'm using Ninject to handle dependency injection. I thought that when i called `IProductsRepository productsRepository;` then Ninject with create the instance on the fly based on the configuration.
quakkels
You never mention nInject in your post or the tags.
Justin Niessner
Sorry... I'll edit my question.
quakkels
sorry Justin, added my comment before I saw your longer version.
mikerennick
I ended up initializing it in the controller, then I passed the instance as a parameter.
quakkels
A: 

Exactly what Justin said...but just to clarify for you...

IProductsRepository productsRepository = new ProductsRepository();

But, you will likely run into issues here if each repository instantiates it's own DataContext object. In that case LINQ will object that you are working with the same related data in two separate DataContext objects (as each DataContext object you create keeps track of changed records).

mikerennick
Instead of referencing the products reposiroty context from the category repository... do you think i should just handle everything inside the category repository?
quakkels
I would say yes.I've found that trying to handle cascading deletes is a real pain in LINQ. I solved my problem by NOT actually deleting records from the DB. Rather, I added a bit field called "isDeleted" to each table, and mark deleted records as "true" for that field. Then I make sure that when I select records I always include the directive isDeleted == false. Saves me a lot of headaches, and also allows me to recover accidentally deleted records with ease. In many cases I do not even need to recursively mark child records as deleted, bc "hiding" the parent record is "good enough."
mikerennick