为实现我的对象Save方法 [英] Implement a Save method for my object

查看:129
本文介绍了为实现我的对象Save方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提高我的应用程序的设计,所以不是调用从presentation层的数据访问层。我会尽力实现从我在BusinessObjects层对象的Save方法。但我不知道如何传递对象,或者它经过层层属性。例如,在我的旧的设计,我只是在presentation层创建我的对象的实例,并指定它的属性然后只需调用数据访问方法保存在数据库中的这些信息并传递对象作为如图所示的参数。

I'm trying to improve my application's design, So instead of calling the DataAccess layer from the presentation layer. I'll try to implement a save method from my object in the BusinessObjects layer. but I'm not sure how to pass the object or it's properties through the layers. for example in my old design I just create an instance of my object in the presentation layer and assign it's properties then just call the DataAccess method for saving this info in the database and pass the object as a parameter as illustrated.

DAL

public static void SaveObject(Object obj)
{
   int id = obj.id;
   string label = obj.label;
}

PL

Object obj = new Object();
obj.id = 1;
obj.label = "test";
DAL.SaveObject(obj); 

但我只是想这样做在我的 PL

Object obj = new Object();
obj.id = 1;
obj.label = "test";
obj.SaveObject();

这可能吗?我DAL如何会是什么样子?

Is that possible? and how would my DAL look like ?

编辑: 解释我的要求

我现在我的基础code上一个非常重要的目标在我的系统。

I'll base my code right now on a very important object in my system.

BusinessEntitiesLayer 使用BusinessLogic层

BusinessEntitiesLayer uses BusinessLogic Layer

namespace BO.Cruises
{
    public class Cruise
    {
        public int ID
        { get; set; }

        public string Name
        { get; set; }

        public int BrandID
        { get; set; }

        public int ClassID
        { get; set; }

        public int CountryID
        { get; set; }

        public string ProfilePic
        { get; set; }

        public bool Hide
        { get; set; }

        public string Description
        { get; set; }

        public int OfficialRate
        { get; set; }

        public string DeckPlanPic
        { get; set; }

        public string CabinsLayoutPic
        { get; set; }

        public List<Itinerary> Itineraries
        { get; set; }

        public List<StatisticFact> Statistics
        { get; set; }

        public List<CabinRoomType> RoomTypesQuantities
        { get; set; }

        public List<CabinFeature> CabinFeatures
        { get; set; }

        public List<CruiseAmenity> Amenities
        { get; set; }

        public List<CruiseService> Services
        { get; set; }

        public List<CruiseEntertainment> Entertainment
        { get; set; }

        public List<CustomerReview> CustomerReviews
        { get; set; }
    }

}

BusinessLogicLayer 使用DataAccessLayer

BusinessLogicLayer uses DataAccessLayer

其实这层打算验证,然后我对象调用DAL的方法,但我并没有马上实现任何验证,所以我只是用它来调用DAL方法。

Actually this layer is intended to be validating my object then call the DAL methods but I didn't implement any validation right now, so I'm just using it to call the DAL methods.

    public static void Save(object cruise)
    {
        CruisesDAL.Save(cruise);
    }

DataAccessLayer 尝试引用BussinessEntities但它给我的循环依赖错误!

DataAccessLayer trying to reference BussinessEntities but it's giving me circular dependencies error!

这应该接收对象,并投它克鲁斯实体

It's supposed to receive the object and cast it as Cruise entity

    public static void Save(object cruise)
    {
         Cruise c = cruise as Cruise;

         //access the object c properties and save them to the database
    }

这是我的项目code样品:<​​/ P>

Code sample from my project:

public static List<Cruise> GetCruisesList()
{
    string commandText = "SELECT ID, Name + CASE Hide WHEN 1 Then ' (Hidden)' ELSE '' END AS Name FROM Cruises";
    List<Cruise> cruises = new List<Cruise>();
    Cruise cruise;

    using (SqlConnection connection = new SqlConnection(ConnectionString))
    {
        using (SqlCommand command = new SqlCommand(commandText, connection))
        {
            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    cruise = new Cruise();

                    cruise.ID = Convert.ToInt32(reader["ID"]);
                    cruise.Name = reader["Name"].ToString();

                    cruises.Add(cruise);
                }
            }
        }
    }

    return cruises;
}

presentationLayer 使用BusinessEntities

PresentationLayer uses BusinessEntities

输入控件(文本框,DropDownList的,等等)

Input controls (TextBoxes, DropDownList, etc)

当保存按钮被点击我把所有的值,创建一个克鲁斯对象并调用Cruise.Save();

When the save button is clicked I take all the values, create a Cruise object and call Cruise.Save();

推荐答案

传递对象本身的数据层通常是有点古怪。相反,我建议您做谈话的数据层中的对象,并让数据层做它的事。

Passing the object itself to the data layer is usually a bit funky. Instead, I recommend that you have the object do the talking to the data layer, and let the data layer do its thing.

internal static class DataLayer {

    public static bool Update(int id, string label) {
        // Update your data tier
        return success; // bool whether it succeeded or not
    }
}

internal class BusinessObject {

    public int ID {
        get;
        private set;
    } 

    public string Label {
        get;
        set;
    } 

    public bool Save() {
        return DataLayer.Update(this.ID, this.Label); // return data layer success
    }
}

你会做这种方式的原因,是因为你的数据层可能没有你的业务对象的引用,因此,根本不知道它是什么。你将无法传递对象本身。这是一个平常之情况,因为通常它是你的业务对象集引用您的数据层组件。

The reason you would do it this way, is because your data layer may not have a reference to your business object, and thus would have no idea what it is. You would not be able to pass the object itself. This is a usual scenerio because generally it is your business object assembly that references your data layer assembly.

如果您在同一个组装的一切,比上述不适。后来但是,如果您决定在数据层重构到它自己的模块(通常是其真正的实力,而且是很好的设计),传递对象将打破,因为那时它失去你的业务对象的引用。

If you have everything in the same assembly, than the above does not apply. Later on however, if you decide to refactor your data layer into its own module (which is often how it turns out, and is good design), passing the object will break because then it loses its reference to your business object.

你这样做无论哪种方式,你应该知道,你将不得不如果添加一个新的领域或成员同时更新你的对象,你的数据层。这只是一个给定的,当你添加新的东西。

Either way you do it, you should know that you will have to update both your object and your data layer if you add a new field or member. That's just a given when you add something new.

我可能会写一些这方面的良好设计实践一个博客,但那是我的建议。

I may write a blog on some good design practices for this, but that is my recommendation.

这篇关于为实现我的对象Save方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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