0

我是一个新手学习实体框架(VS2012),正在编写一个简单的 CRUD 应用程序进行测试。我为简单的插入/更新创建了以下函数。我想知道它是否可以或有任何缺陷并且可以改进?

此函数将位于类库类文件中,并将在表单提交时从 Web UI 调用。

这是功能:

public static bool Save(int id, string hospitalname, string hospitaladdress, int cityid,
                            string postcode, int countryid, string email, string     phone, string fax, string contactperson,
                            string otherdetails, bool isactive, DateTime createddate)
    {
        bool flag = false;
        using (var dataContext = new pacsEntities())
        {
            if (id == 0)
            {
                // insert
                var newhospital = new hospital_master();

                newhospital.hospitalname = hospitalname;
                newhospital.hospitaladdress = hospitaladdress;
                newhospital.cityid = cityid;
                newhospital.postcode = postcode;
                newhospital.countryid = countryid;
                newhospital.email = email;
                newhospital.phone = phone;
                newhospital.fax = fax;
                newhospital.contactperson = contactperson;
                newhospital.otherdetails = otherdetails;
                newhospital.isactive = isactive;
                newhospital.createddate = DateTime.Now;

                dataContext.hospital_master.AddObject(newhospital);
                dataContext.SaveChanges();
                flag = true;
            }
            else
            {
                // update
                var hospital = dataContext.hospital_master.First(c => c.hospitalid == id);
                if (hospital != null)
                {
                    hospital.hospitalname = hospitalname;
                    hospital.hospitaladdress = hospitaladdress;
                    hospital.cityid = cityid;
                    hospital.postcode = postcode;
                    hospital.countryid = countryid;
                    hospital.email = email;
                    hospital.phone = phone;
                    hospital.fax = fax;
                    hospital.contactperson = contactperson;
                    hospital.otherdetails = otherdetails;
                    hospital.isactive = isactive;

                    dataContext.SaveChanges();
                    flag = true;
                }
            }
        }
        return flag;
    }
4

2 回答 2

0

You may implement a generic repository that will be responsible for all CRUD operations.

For example: http://geekswithblogs.net/seanfao/archive/2009/12/03/136680.aspx

You will be able to use it for all entities that you have in your application.

于 2013-10-29T12:23:07.350 回答
0

您可以进行多项改进:

1-您可以创建一个类医院并通过它

2-我个人认为抛出异常比返回标志确定失败更好,但仍然可以在成功时返回true

3-保存上下文可以在 if 之外写入一次

4- .First在更新场景中可以是FirstOrDefault,如果要返回成功/失败标志,则检查null;或者只是让 First 失败,以防你想抛出异常而不是返回 true/false,但是一种或另一种方式是一致的,你要么抛出异常,要么返回 true/false。您正在使用 First 并检查 null!

5- 拥有与实体类 (hospital_master) 不同的模型类 (Hospital) 是很好的,在更高层中使用 Hospital 类,在数据层中使用 Save 方法,进行转换或使您的物体水化/脱水。

6 - 您可以通过以下方式简化对象的创建:

new hospital_master
{
    hospitalname = hospital.hospitalname,
    hospitaladdress = hospital.hospitaladdress,
   ...
};

7-你可以在你的医院类(ToEntity)上有扩展方法,然后简单地调用它来保存:

dataContext.hospiltal_master.AddObject(hospital.ToEntity());

8- EntitySet(包含 hospital_masters 实例的 hospital_master 集)似乎与实体本身具有相同的名称。您正在创建hospital_master 实体,然后将其添加到hospital_master:datacontext.hospital_master.AddObject... 我认为应该有datacontext。hospital_masters .AddObect... 复数。

于 2013-10-29T09:35:41.137 回答