Reputation: 75
I'm learning Entity Framework and building a simple web application. I'm using ASP.net Web Pages (Razor 3), in Visual Studio.
I want to create a basic structure for database operations. I have the entities, i can add, update records with it but i'm still using one more class for those. I have a class named cDB and in it there are few methods like this:
public class cDB
{
private maasEntities1 m = new maasEntities1();
public void createUpdateBranch(Branch branch)
{
if (branch.bra_id == 0)
{
m.Branches.Add(branch);
}
}
public void saveChanges()
{
m.SaveChanges();
}
public maasEntities1 getContext()
{
return m;
}
}
It's pretty simple, i just add records for now. I have few questions about this:
I want to use one DBContext in whole project and if i need to read some data from it, for example if i want to list details of a branch, i will do this:
cDB DB = new cDB();
Branch curBranch = new Branch();
curBranch = DB.getContext().Branches.Find(1);
Is it the right way to do it?
I can just use m.Branches.Add(branch); code in the page, maybe i don't need to do this in cDB class? It looks better though?
I check if it's a new record or an update like this:
if (branch.bra_id == 0)
{
m.Branches.Add(branch);
}
Again, is it the right way?
Thanks
Upvotes: 0
Views: 474
Reputation: 5513
Whenever you add layers of abstraction, you need to understand exactly what you gain by that abstraction. IMHO, adding repository pattern on top of EntityFramework does not add anything useful, as EF DbContext is THE repository.
Further reading: Repositories On Top UnitOfWork Are Not A Good Idea
Upvotes: 2