I am Working on Entity framework to get started with it. Below is the way i have arranged my projects to interact with EF. I just want to know if i am heading to the right direction or not. Please do suggest the better approach. Please ignore the architecture things like UI should not have reference to DAL. This is just “get to know” project. 🙂
This is what i am doing:
One project for DAL. Here all the DB operations will be doing using EF.
Model – Business Entity classes. Instead of returning Order object of EF, i will be returning Entity Class of Model. (Is there a way to map my Business Entities to EF entity?)
UI – This will call DAL and get things done.
Project 1 – DAL
This project also contains my edmx file.
public class DALRepository
{
OrderEntities orderEntitiesContext = new OrderEntities();
public IQueryable<Model.OrderModel> GetOrders ()
{
return orderEntitiesContext.Orders.Select(o => new Model.OrderModel { OrderName = o.OrderName, Id = o.OrderID });
}
}
Project 2 – Model
public class OrderModel
{
public long Id { get; set; }
public string OrderName { get; set; }
}
Project 3 – UI
public partial class OrderList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DAL.DALRepository dal = new DAL.DALRepository();
GridView1.DataSource = dal.GetOrders();
GridView1.DataBind();
}
}
Consider adding an interfaces project with the API needed outside the DAL to enable unit testing without the DAL and a implementation project that implements those interfaces using the DAL project.