WPF / C#,使用实体框架处理数据访问层和业务逻辑层 [英] WPF/C#, Dealing With Data Access Layer and Business Logic Layer Using Entity Framework

查看:306
本文介绍了WPF / C#,使用实体框架处理数据访问层和业务逻辑层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后端使用SQL Server的WPF应用程序中使用Entity Framework。据说我在项目中使用数据访问层和业务逻辑层。我以前没用过这些。我已将Ado.Net模型放在数据访问层中。我已在单独的类中在业务逻辑层中进行了验证。怎么办?我应该在哪里保留实体的添加,更新,删除功能?如何?这是其中一个课程





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


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();
        }
    }

推荐答案

IDesign 是一种使用UI的架构 - > ; [经理 - > DataAccess] - >数据库。我相信这种架构设计正是您所寻求的。我目前使用UI [MVVM]图层 - >经理层 - > DataAccess图层 - > SQL Server使用实体框架。基本上你会创建一个接口和一个管理器类来保存逻辑以确定功能(例如,如果id = 0则为Save(),那么它将是使用实体框架的插入,或者如果id = 3则它已经在那里的数据库我们将在实体框架中使用更新。)你的经理做出决定,然后传递到实际使用实体框架命令的数据访问。
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.


这篇关于WPF / C#,使用实体框架处理数据访问层和业务逻辑层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆