DataContext understanding / issue

Jan 28, 2013 at 9:17 PM

Hi,

I am using this framework in web application it's not truely MVC but some what like MVC

I have online cart which is comprised of order, orderdetail, company (which has shipping and billing addresses), purchase, products and so on tables

i have a OrderController class which instantiates an OrderRepository object (obviously autogenerated by the T4 template)

so assuming OrderRepository object will create an instance of the OrderDataContext, which as the framework is saying it's unique and staus alive for the scope of that web request.

what instance of the datacontext the staticrepository classes using?

when do i call the DiscardDataContext method on this OrderRepository and how it can't access it because it's protected or I don't need to call the DiscardDataContext at all?

I have couple methods in OrderRepository and in it's subclass which calls submitchanges method on this OrderRepository instance's datacontext.

Any insight in this matter is really appreciated quickly.

Thanks in Advance.

Amit

Developer
Jan 29, 2013 at 1:57 PM

I think you should be using the OrderStaticRepository instead of the OrderRepository. Here's an example of what I think you need:

If you have 2 Orders, order1 and order2, then to update both within the same scope, do the following:

 

OrderStaticRepository.Update(order1, false);
OrderStaticRepository.Update(order2, false);

OrderStaticRepository.DataContext.SubmitChanges();

 

 

Alternatively, if you have update / insert operations to perform across different StaticRepositorys, then you can use the TransactionScope within a using statement:

using (var scope = new TransactionScope())
{
    OrderStaticRepository.Update(order1);
    OrderStaticRepository.Update(order2);
    ProcessStaticRepository.Insert(newProcess);

    scope.Complete();
}

Jan 29, 2013 at 4:28 PM

Thanks for your quick reply.

You didn't answer my important question though, when do I call the DiscardDataContext method or is it called automatically on all StaticRepository classes? I believe it must be called very last but we don't know when this request would end or is it when order is submitted successfully? what if there is an unknown exception which is not handeled then this would leave this datacontext dangling, right?

I understand that using the using statement would dispose the obejct automatically but in your using statement above where does it dispose the DataContext that this StaticRepository is using?

Please help me understand what's going on with the data context using the web request.

Again, thanks for your respose earlier and will appreciate your answer on this too.

Amit