0

I am really new to dependency injection concept. So maybe I made it wrong, I am not sure. Let me describe the issue.

I have a Windows forms app. In main form, I have called Entity Framework db context

 DBContext context = new DBContext();

I have several forms and business logic for every entity. What I am trying to do is: the user opens a form, there is a datagridview in the form to display data. Its data source is context.

User enters some data, then, if user clicks ok button, I call context.saveChanges();

If user clicks cancel button I don't call and I expect data doesn't be saved.

Here is the related code. In mainform I call my form and inject context:

dataForm form = new dataForm(context);
form.ShowDialog();

In dataForm user sees the data, crud. I inject the same context which is coming from mainform to logic class:

DBContext context;
dataLogic logic;

public dataForm(DBContext context)
{
     this.context = context;
     logic = new dataLogic(context);
} 

private void okButton_Click(object sender, EventArgs e)
{
    context.SaveChanges();
    DialogResult = DialogResult.OK;
}

private void cancelButton_Click(object sender, EventArgs e)
{
    DialogResult = DialogResult.Cancel;
}

The problem here is, since I use dependency injection (context in mainform -> inject to dataform -> inject to logic) and I use the context as datasource for datagridview even if I don't call context.SaveChanges(); the data has been saved to context. So user clicks cancel and still sees the modified data. Unless it restart the application.

To fix the issue I can create new instances of context in logic classes or data forms but this solution will come with problems that DI solves.

So, how should I implement my code? Did I made DI not correct?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
xifusuk
  • 9
  • 1
  • show us code for `dataLogic` class – jazb Nov 21 '18 at 06:15
  • Are you using any framework for DI like Autofac or Ninject? – Manprit Singh Sahota Nov 21 '18 at 06:17
  • It would be awesome if you could provide a [mcve] – jazb Nov 21 '18 at 06:26
  • @JohnB I don't think dataLogic class is needed. It's all doing is making some calculations before context.dataset.Add(Entity); – xifusuk Nov 21 '18 at 06:37
  • @Manprit Singh Sahota I don't use any frameworks – xifusuk Nov 21 '18 at 06:37
  • The problem is here as your data context is passed to data form so from anywhere else the data is modified and from other form you call the save changes. Every changes to context is applied to database. – Eldho Nov 21 '18 at 07:43
  • The idea of DI is that the class should not have any knowledge about the dependency its needed .Wiki "The intent behind dependency injection is to decouple objects to the extent that no client code has to be changed simply because an object it depends on needs to be changed to a different one" – Eldho Nov 21 '18 at 07:46
  • **Option 1** - Don't pass use a shared instance of DbContext, instead pass a new instance every time and make sure you dispose it when form is disposing. **Option 2** - For each entry in change tracker of your db context, change entry state. For example take a look at [this post](https://stackoverflow.com/a/22098063/3110834). – Reza Aghaei Nov 21 '18 at 08:12

0 Answers0