proper way to instantiate repository classes

Oct 11, 2007 at 3:53 PM
With the "pre October 1" version of the Data Access Guidance package, it was pretty straightforward to instantiate a repository class. For example:

MyRepositoryType myRepo = new MyRepositoryType("MyDatabaseConnectionString");

The 10/1 release of the package nicely hid away all of the underlying implementations of the MyRepository methods into subdirectories (thanks!), leaving only the interface IMyRepository out at the root level. But, maybe I'm missing something here, but there isn't a provided factory for creating the "MyRepository". Now I have to do:

MyBusinessEntities.MyRepositoryType.MyRepositoryArtifacts.MyRepositoryType myRepo = new MyBusinesEntities.MyRepositoryType.MyRespositoryArtifacts.MyRepositoryType("dbstring")... you get the idea.

It appears that the new directory format of the generated files is trying to hide all of those files away from view, but at the same time I still have to get down in there when creating the object.

Am I missing something?

Thanks,

Curt
Austin, TX
Developer
Oct 11, 2007 at 4:27 PM
Curt,

You will want to use the RepositoryFactory Class to instantiate your Repository Classes as follows:

IMyRepository repository = RepositoryFactory.Create<IMyRepository>();

or

IMyRepository repository = RepositoryFactory.Create<IMyRepository>(namedConnectionString);

I talk about it more in the following post:

Repository Factory - New and Improved Data Access Guidance Package

Regards,

Dave

__________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Oct 16, 2007 at 1:31 PM
David,

For me passing the Connection String only worked and if I dont send, it was throwing an exception telling that object is not instantiated.
Oct 23, 2007 at 7:39 AM
Even if I passed the connection string, still i got error:

IHApplicationUserRepository repository = RepositoryFactory.Create<IHApplicationUserRepository>(reader.getConnStrXML);

Error encountered:

Object reference not set to an instance of an object.

Please help.
Developer
Oct 23, 2007 at 1:10 PM
chakkaradeep,

When using the version that does not provide a connection string:

IMyRepository repository = RepositoryFactory.Create<IMyRepository>();

You need to make sure the Data Access Application Block is specifying a DefaultDatabase in your App.config or Web.config:

<dataConfiguration defaultDatabase="ConnectionString" />

If you do not specify a default database, you will get an error, probably the one you encountered.

Regards,

Dave

__________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Developer
Oct 23, 2007 at 1:17 PM
gAlvero,

I would double check that reader.getConnStrXML is not null or empty and that the named connection string it points to is indeed in the App.config or Web.config. You also need to make sure IHApplicationUserRepository and the type it maps to are indeed in the Repository Configuration Section and are accessible to the host application.

It sounds like the errors that are thrown by the RepositoryFactory Class need to be updated so that it is more obvious as to the problem. "Object reference not set to an instance of an object" is a difficult problem to debug unless you step through the code with the debugger.

Regards,

Dave

___________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Oct 24, 2007 at 2:29 PM
Thanks David for the great help,
It's already working but I'd like to ask if this repository factory is ready in implementing asynchronous connection.
Oct 24, 2007 at 7:24 PM
David, I am trying to instantiate a Repository class, however I get the following run time error:

Could not load file or assembly 'Microsoft.Practices.ObjectBuilder, Version=1.0.51206.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"Microsoft.Practices.ObjectBuilder, Version=1.0.51206.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

The code that I am using is:

public MainForm()
{
InitializeComponent();
string str = ConfigurationManager.ConnectionStrings"connectionString".ConnectionString;
IDefectRepository defectRepo = RepositoryFactory.Create<IDefectRepository>(str);
List<Defect> defectList = defectRepo.GetAllFromDefect();
dgvDefects.DataSource = defectList;

}

The error is in the statement:
IDefectRepository defectRepo = RepositoryFactory.Create<IDefectRepository>(str);

Is there anything wrong during the installation of the Repository Factory?. I think that I followed all the required steps.

Thanks and best regards,
Alberto




DavidHayden wrote:
gAlvero,

I would double check that reader.getConnStrXML is not null or empty and that the named connection string it points to is indeed in the App.config or Web.config. You also need to make sure IHApplicationUserRepository and the type it maps to are indeed in the Repository Configuration Section and are accessible to the host application.

It sounds like the errors that are thrown by the RepositoryFactory Class need to be updated so that it is more obvious as to the problem. "Object reference not set to an instance of an object" is a difficult problem to debug unless you step through the code with the debugger.

Regards,

Dave

___________________________

David Hayden
Microsoft MVP C#
PnPGuidance

Developer
Oct 25, 2007 at 2:42 PM
The Repository Factory uses the Data Access Application Block in Enterprise Library to communicate with the database. Enterprise Library uses ObjectBuilder to build up objects, such as the database class used to communicate with the database. Therefore, ObjectBuilder needs to be referenced or in your application's bin directory so that it can be used by Enterprise Library.

Just grab ObjectBuilder from the Enterprise Library 3.1 bin directory if it is not included with the Repository Factory and make sure it is available to your application. Adding a reference to it from the host application will assure it gets copied to the bin directory.

Regards,

Dave

_______________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Oct 26, 2007 at 2:44 PM
Thanks David. It is working now. I changed the code to look like the following:

IDefectRepository defectRepo = RepositoryFactory.Create<IDefectRepository>("connectionString");
List<Defect> defectList = defectRepo.GetAllFromDefect();
dgvDefects.DataSource = defectList;

Best regards,
Alberto


DavidHayden wrote:
The Repository Factory uses the Data Access Application Block in Enterprise Library to communicate with the database. Enterprise Library uses ObjectBuilder to build up objects, such as the database class used to communicate with the database. Therefore, ObjectBuilder needs to be referenced or in your application's bin directory so that it can be used by Enterprise Library.

Just grab ObjectBuilder from the Enterprise Library 3.1 bin directory if it is not included with the Repository Factory and make sure it is available to your application. Adding a reference to it from the host application will assure it gets copied to the bin directory.

Regards,

Dave

_______________________________

David Hayden
Microsoft MVP C#
PnPGuidance