Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using Entity Framework in a WPF application with SQL Server on back end. I am said to use Data Access Layer and Business Logic Layer in the project. I have not used these before. I have put that Ado.Net Model in Data Access Layer. I have made validations in Business Logic Layer in separate class. What to do more? Where should I keep the Add,Update, Delete Functions of the entities ? And How? Here is one of the classes


C#
public partial class AddCar : Window
    {
        RentACarEntities db = new RentACarEntities();
        public AddCar()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoad);
        }
        void OnLoad(object sender,EventArgs e)
        {
            var items= new List<string>() { "Petrol","Diesel","CNG"};
            foreach(var item in items)
            {
                cmbfuel.Items.Add(item);
            }
        }

        void SaveData()
        {
            try
            {
                Car car = new Car();
                car.Type = txttype.Text;
                car.Model = txtmodel.Text;
                car.RegistrationNumber = "";
                car.Year = txtyear.Text;
                car.Fuel = cmbfuel.SelectedItem.ToString();
                car.RentPerDay = Convert.ToDouble(txtrent.Text);
                db.Cars.Add(car);
                if(db.SaveChanges()>0)
                MessageBox.Show("New Car Added Successfully");
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error occured while adding new Car");
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
Posted
Updated 29-Mar-16 5:14am
v3
Comments
jame01 18-Dec-15 16:49pm    
im not so sure about your question do you want to use database to store those data?
if you want save data in some place and the sql is good database to use and easy to use, if this is u question
and here is link you can check
http://www.codeproject.com/Articles/823854/How-to-connect-SQL-Database-to-your-Csharp-program
Ikram Khan 19-Dec-15 3:45am    
@jame01 my question is not that you got. My question is that how to use Three Tier Architecture With Entity Framework in role in the project. I am using SQL on back end.
Jitesh Hirani 22-Jan-16 8:05am    
http://www.dotnetcrazy.com/aspnet/tutorial/crud-operations-using-entity-framework-in-asp-net-part-1
Have a look at this tutorial, probably this may help you sort out the issue.
ryanba29 25-Feb-16 14:49pm    
Is this a MVVC question? If so your above code is a mixture of your View and your View model. You should move your methods and other business logic to a separate class. You can this initialize a instance of that class form you window and set your data context to that class. And in this case your database in the Data Model

1 solution

IDesign is an architecture that uses UI -> [Manager -> DataAccess] -> Database. I believe that this architecture design is what you are looking for. I currently use UI[MVVM] layer -> Manager layer -> DataAccess layer -> SQL Server using Entity Framework. Basically you would make a interface and a manager class that would hold the logic to determine the functionality (for example Save() if the id = 0 then it would be an insert using entity framework, or if id = 3 then it is already in the database there for we would use update in entity framework.) Your manager makes the decision, then passes to the Data Access which would actually use the Entity Framework commands.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900