views:

149

answers:

2

Hi folks,

I've got a winform app in visual studio 2010. My app does the following

  • Get a list of files which I need to read the data then insert into a database.
  • For each file, read data and insert into DB.

So .. this is the code i have.

// *******
// *** How should the _repository be Injected??
// *******
var list = _repository.GetFileList();
if (list != null)
{
    int i = 0;
    foreach(var file in list)
    {
       i++;
       var service = new MyService(i, _repository);
       service.ParseAndSave();
    }
}

So i was hoping to have a new repository for each 'service' i create.

Firstly, i'm not sure if I should be using IoC in this case. I believe I should be because then i don't need to tightly couple this winform to a repository.

Secondly, I've tried using a Singleton repo, which I don't want and can confirm that it kills that code (crashes with an exception).

Some other notes (which shouldn't impact this question) - Using Entity Framework for ASP.NET 4. - Using StructureMap for IoC

Can someone help, please?

UPDATE

Oh, I forgot to mention. When i don't specify the lifecycle type (eg. Singleton, etc). my objects which i try to save, just don't save. (ie nothing is sent to the DB, looking at SQL Profiler). If i use a Singleton with one file ... it works. A singleton with 2+ files, then exception/crashes because of (internal EF) Primary Key conflicts with Entity Framework. So if i should be using a Singleton, then the issue must be with how i've setup my EF4 context.

A: 

Constructor Injection is always a good start

public MyClass(IRepository repository)
{
    if(repository == null)
    {
        throw new ArgumentNullException("repository");
    }

    this.repository = repository;
}

However, with Windows Forms, you run into the issue that your Form classes must have default constructors to support design-time functionality in Visual Studio. You can address this issue by making sure your Forms are nothing more than dumb renderers of data.

Mark Seemann
Yep - that's all good .. and i do that with my WebForms/ASP.NET MVC apps... but with a winform? how (and with structuremap, for bonus points).... (and yep, i checked that link out). The main thing is.. i can't use Constructor Injection on the Form .. because in that form, each of my Files (i read) needs it's OWN repository .. not the one the Form will have/use.
Pure.Krome
A: 

You have two basic choices Constructor Injection (as in Mark Seemann's example) or Property (setter) injection (where supported by your container eg Unity)

In the case of a winform you either need to use Property (setter) injection which allows a default constructor to be used, or again as Mark suggests, using the form as just a simple renderer of data and in this case you should probably consider some variant of the Model View Presenter pattern (note in the link this pattern has been further refined and subdivided)

Its work taking a look at the sample code for the MS Patterns and Practices Prism project for some examples which use MVP approach with Unity

Richard