Last post Aug 13, 2010 08:40 PM by kwanann
Aug 11, 2010 05:07 PM|luppin|LINK
In my ASP.NET applicationI have a website project and classlibrary project "DataAccess", that acts as datalayer, where I use Entity framework to work with database.
The conceptual structure of this is:
BaseRepository class that implements IDisposable, and creates and disposes the Context Object.
Then every Entity class (like CommentsRepository or DocumentsRepository that part of same context model) inherit this BaseRepository, and implement the database operation.
Now my website should use it somehow.
I want to know if it's wise to create some Business class that will use this Dataaccess modules.
I don't want to create instances of this class every time I work with data,
create it onse in Global Application_Start, and use static property to access it.
Aug 11, 2010 06:44 PM|kwanann|LINK
well simply, this class is not thread safe, so its not recommended to have a static version of this class.
you can refer to the following 2 threads for more info
Aug 12, 2010 02:58 AM|luppin|LINK
well, maybe you didn't understand me (because the code that i pasted didn't pass through).
I am creating single instance of Business class:
public class Global : HttpApplication
public static BusinessForum_EF BusinessForumLayer_EF;
void Application_Start(object sender, EventArgs e)
//Code that runs on application startup
//Instance of the Business class
BusinessForumLayer_EF = new BusinessForum_EF();
now business class will have code like this:
public class BusinessForum_EF : BaseForum
public List<ForumFull> GetFullForumsBySection(int sectionID)
List<ForumFull> lForum = null;
using (ForumsRepository forumRepository = new ForumsRepository())
lForum = forumRepository.GetFullForumsBySection(sectionID);
ForumsRepository, will access data using EF.
All my webpages don't have to create, on every postback or dataaccess new instance of BusinessForumLayer_EF.
But the Object context and entity still recreated so there is no static single version of them.
Aug 12, 2010 03:06 AM|kwanann|LINK
oh in that case, declare the class and all the functions as static, it will be much better than declaring a static variable
Aug 12, 2010 03:18 AM|luppin|LINK
can you please explain why?
Aug 13, 2010 08:40 PM|kwanann|LINK
declaring a static class and static methods essentially creates a singleton, .net will manage all the required resources for you
declaring it inside the application class means that
- you have to keep referencing the application class to get the business layer (additional call application class),
- you have to handle disposal for the class (since its declared as a normal variable),
- it *may* become null (since its just a variable)
so many things which you should not need to handle