-1

从 SQL Server 填充数据时,到达BObj_Sf's的最佳方式是什么?Sn

这个

public class BObj_Sub
{
    BObj_Sf _Sn = new BObj_Sf();

    public BObj_Sf Sn
    {
        get { return _Sn; }
        set { _Sn = value; }
    }
}

或者这是 ooP 的一个更好的方面?

BObj_Sub sub = new BObj_Sub
 {
    Sf = new BObj_Sf { SAd = SAd },
 };
4

1 回答 1

1

我相信您的问题与良好的编程实践和there could be multiple solution or design technique for this question.

我会创建这样的类 -

// create interface for providing {x} features
public interface IBObj_Sf
{
   ...
   ...
}

// create class which provides {x} features of IBObj_Sf interface
public class BObj_Sf : IBObj_Sf
{
   ...
   ...
}

// now implement BObj_Sub class like -
public class BObj_Sub
{
        // mark as readonly depending it is modifiable or 
        // not withing BObj_Sub's lifecycle
        private readonly IBObj_Sf _Sn; 

        public BObj_Sub(IBObj_Sf sn)
        {
            _Sn = sn;
        }

        public BObj_Sf Sn
        {
            get { return _Sn; }
            private set { _Sn = value; }
        }
}

用法:

BObj_Sf sf = new BObj_Sf();
BObj_Sub sub = new BObj_Sub(sf);

优点:明天如果你想换BObj_Sf一个新的班级BObj_Xf,那么只有以下几行会随着你的新班级而改变。-

BObj_Xf xf = new BObj_Xf();
BObj_Sub sub = new BObj_Sub(xf);

中的代码BObj_Sub将保持不变。

于 2013-06-06T17:55:44.670 回答