.NET – Want to encapsulate the database in the business object?

I usually like to create a database connection myself, and use `using {}’ to manually control its life cycle. For example:

SqlConnection sqlConnection = new SqlConnection( connectionString );
using( sqlConnection) {
BusinessObject myBusinessObject = new BusinessObject( sqlConnection );
// do stuff with the business object
.. .
}

In this way, I can see and it is obvious that I am using resources that need to be properly cleaned up. However, this will eventually lead to a lot of repeated efforts. I would love to Create a Sql connection in the business object and implement IDisposable on it. I will close the connection in the Dispose() method.

using( BusinessObject myBusinessObject = new BusinessObject()) { 
// do stuff with myBusinessObject
...
}

The problem I have is that unless you see the business object in use, it may not So obvious.

What will you do?

Business objects should be reasonably (or completely) stupid relative to the database. You should implement some kind of access layer Object (repository or data context), it knows how to persist business objects to the database and keep the connection logic, instead of putting code in each business object. Your repository or context will be disposable so that it It can be cleaned up by yourself. @Marc suggesting that you follow the unit of work pattern is a good suggestion.

You may want to look at LINQtoSQL, nHibernate, Subsonic, etc. in order to use them, or at least if you insist How to build a good data layer when writing your own data layer. Based on personal experience, I can tell you that it is much easier to use existing technology than to write and maintain your own technology.

I usually like to create a database connection myself, and use `using {}’ to manually control its life cycle. For example:

SqlConnection sqlConnection = new SqlConnection( connectionString );
using( sqlConnection) {
BusinessObject myBusinessObject = new BusinessObject( sqlConnection );
// do stuff with the business object
...
}

In this way, I can see and it is obvious that I am using resources that need to be properly cleaned up. However, this will eventually lead to a lot of repeated efforts. I would love to create Sql connections in business objects and Implement IDisposable on it. I will close the connection in the Dispose() method.

using( BusinessObject myBusinessObject = new BusinessObject()) {
// do stuff with myBusinessObject
...
}

The problem I have is that unless you see the business object in use, it may not be so obvious.

< p>What will you do?

Business objects should be reasonably (or completely) stupid relative to the database. You should implement some kind of access layer object (repository or data context), which knows how Persist business objects to the database and keep the connection logic instead of putting code in each business object. Your repository or context will be one-off so that it can clean up on its own. @Marc recommends that you follow the unit of work pattern Is a good suggestion.

You may want to look at LINQtoSQL, nHibernate, Subsonic, etc. in order to use them, or at least how to build a good data layer if you insist on writing your own data layer Based on personal experience, I can tell you that using existing technology is much easier than writing and maintaining your own technology.

Leave a Comment

Your email address will not be published.