三层开发 - 有点困惑.. [英] 3-Tier Development - A little confused..

查看:57
本文介绍了三层开发 - 有点困惑..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发一个新的应用程序,我正在尝试将其作为我的第一个真正的

OOP / 3层应用程序。我理解演示文稿的原则,

业务和数据层。但是,对于某些功能应该放在哪里以及应该如何实现
,我有一些问题。


让我们使用一个简单的例子来管理客户

记录(customer_id,first_name,last_name)。我将拥有一个具有ID,FirstName和LastName属性的Customer

业务对象。
我不明白的内容如下:


1.如何获取预装了来自

数据库(例如customer_id 3)。我可以在Customer业务类中创建一个构造函数

,其中customer_id作为参数,

让构造函数调用数据层以返回包含
$的数据集b $ b客户的数据,然后相应地设置属性。这是

这通常是如何实现的?我还可以在

客户数据层中使用一个方法,该方法接受customer_id作为参数,并且
返回一个Customer业务对象。我唯一的问题就是我在我脑海中有这样的表示层,表示层永远不应该与数据层进行通信。这里的一些帮助会很棒。


2.如何在

的情况下获取Customer对象的集合或ArrayList,我需要返回的数量超过一个客户对象?这样的一个例子就是列出所有客户的表格。在客户数据层调用客户数据层中的方法返回客户业务的ArrayList

对象也可以在这里工作,但这违反了同样的规则
使用表示层处理数据层的
。另一个

选项是在Customer业务对象中放置一个方法来返回一个

的Customer业务对象的ArrayList,但是我的代码必须是

实例化Customer对象只是为了返回更多的Customer对象。

这是除非我在Customer业务对象中创建方法

共享。

3.插入,更新,删除数据库记录的数据层方法 -

插入和更新方法是否应接受业务对象作为

参数,还是应该接受一个长参数列表,其中包含每个业务对象属性的

单独参数?


任何人都可以提供的帮助非常欢迎。这些问题

一直让我无法开发OOP / 3层解决方案,因为我似乎不能强迫自己开发一个系统

没有真正知道如何解决这些问题的正确方法。


先谢谢你的帮助! />

Shawn Berg

Developing a new app and am trying to make this my first truly
OOP/3-Tier app. I understand the principles of the presentation,
business, and data layers. I do, however, have some questions on where
certain functionality should be placed and how some things should be
implemented.

Let''s use a simple example such as an application to manage customer
records (customer_id, first_name, last_name). I''d have a Customer
business object with ID, FirstName, and LastName properties. What I
don''t understand is the following:

1. How do I obtain a Customer business object pre-loaded with data from
the database (customer_id 3 for example). I could create a constructor
in the Customer business class with the customer_id as a parameter and
have the constructor call the data layer to return a dataset containing
the customer''s data and then set the properties accordingly. Is this
how this is normally accomplished? I could also have a method in the
Customer data layer that accepts a customer_id as a parameter and
returns a Customer business object. My only problem with this is that I
have it in my head that the presentation layer should never communicate
with the data layer. Some help here would be great.

2. How do I obtain a collection or ArrayList of Customer objects in the
case where I need to return more than one customer object? Such an
example would be a form that listed all customers. Calling a method in
the Customer data layer to return an ArrayList of Customer business
objects would work here as well, but this is breaking the same rule of
having the presentation layer working with the data layer. The other
option is to put a method in the Customer business object to return an
ArrayList of Customer business objects, but then my code has to
instantiate the Customer object simply to return more Customer objects.
This is unless I make the method in the Customer business object
shared.

3. Data layer methods to insert, update, delete database records -
Should the insert and update methods accept business objects as
parameters or should they accept a long parameter list that contains a
seperate parameter for each of the business object''s properties?

Any help anyone can provide here is more than welcome. These issues
have been keeping me from developing an OOP/3-tier solution for quite
some time as I just can''t seem to force myself to develop a system
without truly knowing how these issues should be tackled the proper
way.

Thanks in advance for your help!

Shawn Berg

推荐答案

嗨Shawn,


好问题! OOP就是先考虑思考,然后再编码。


我看到你对这些原理的理解有两个漏洞。第一个

是了解哪个层知道哪个其他层。无论你是在谈论一个3层或更多层的应用程序,原则都是失败的

简单:每个层都是它下面的层的客户端。这意味着没有任何级别的b $ b知道关于它上面的层的关联,并且只知道它下面的等级
。示例:


演示层

了解业务层(下面1个)

不了解数据层(下面2个)

业务层

了解数据层(下面1个)

不了解Presentation Tier(上图)

数据层

不了解Presentation Tier(上图)

不了解业务层(上图)


如何业务层是否与表示层进行通信?它使用了

属性,表示层可以读取,以及

表示层可以订阅的事件。


这是什么关于问题1(我如何获得预先加载来自数据库的数据的客户业务对象)的意思,

答案应该是显而易见的了解了这个原则。客户

级是商务舱。它可能知道数据层。所以,是的,你

可以创建一个Contructor,它接受一个客户ID并从Data Tier填充

实例。或者您可以在客户类中创建一个客户ID属性,该类在setter方法中从

数据层中获取数据,并使用它填充实例。第二种是更好的

方法。第一种方法也可以使用,通过重载构造函数,

,并在带有客户ID的变体中,使用

客户ID属性的setter方法填充班级。非常简单的例子:


公共类客户

{

private string _ConnectionString =" Your Default Connection String" ;;

public string ConnectionString

{

get {return _ConnectionString; }

set {_ConnectionString = value; }

}


私有字符串_FirstName;

公共字符串FirstName

{

get {return _FirstName; }

set {_FirstName = value; }

}


private int _CustomerID;

public int CustomerID

{

get {return _CustomerID; }

set

{

//设置私人会员

_CustomerID = value;


//(虚数据层方法)

//注意数据层只知道它被告知的内容

//关于它的数据正在使用。

//业务层知道如何使用数据层。

DataTable dt = DataTier.GetDataTable(

" TblCustomer"," CustomerID =" + _CustomerID,

_ConnectionString);


//填充实例

_FirstName =(int)dt.Rows [0] [" FirstName"];

}

}


公共客户(){}


public Customer(string connectionString)

{

_ConnectionString = value;

}


public Customer(string connectionString,int customerID)

{

//更改默认连接字符串

_ConnectionString = connectionString;


//注意这里使用setter方法

CustomerID = customerID;

}


公共客户(int customerID)

{

//注意这里使用setter方法。

//这个重载使用默认连接字符串

CustomerID = customerID;

}


}


你似乎缺少的第二件事是一个班级和一个对象是

不一样的东西。一个班级只有一个副本。可以有很多

的对象副本。对象是类的实例(或副本)。


所以,当你问


我如何获得一个集合或者客户对象的ArrayList在案例

,我需要返回多个客户对象?


,你说


"另一种选择是在Customer业务对象中放置一个方法

返回一个Customer业务对象的ArrayList


似乎你在混合类和对象。解决方案是

创建一个Collection类,可用于存储多个Customer

实例。最好的事情,而不是使用ArrayList,是创建一个

强类型集合,它始终与客户实例一起使用。


问题3在关于哪个层知道什么是关于其他层的
我的开场白。


-

HTH,


Kevin Spencer

Microsoft MVP

..Net开发人员


假设上帝是只有一个想法 -

存在想法。

因此,上帝存在。


" iTISTIC" < SH *** @ itistic.com>在消息中写道

news:11 ********************** @ v46g2000cwv.googlegr oups.com ...
Hi Shawn,

Good questions! OOP is all about thinking first, and coding later.

I see a couple of holes in your understanding of these principles. The first
is understanding which layer knows what about which other layer. Whether
you''re talking about a 3-tier or more-tier app, the principle is failry
simple: Each tier is a client of the tier below it. That means that no tier
knows anthing about the tier above it, and only knows about the tier
immediately below it. Example:

Presentation Tier
Knows about Business Tier (1 below)
Does NOT know about Data Tier (2 below)
Business Tier
Knows about Data Tier (1 below)
Does NOT know about Presentation Tier (above)
Data Tier
Does NOT know about Presentation Tier (above)
Does NOT know about Business Tier (above)

How does the Business tier communicate with the Presentation tier? It used
Properties which the Presentation Tier can read, and Events which the
Presentation Tier can subscribe to.

What this means with regards to Question number 1 ("How do I obtain a
Customer business object pre-loaded with data from the database"), the
answer should be obvious after understanding this principle. The Customer
class is a business class. It may know about the Data Layer. So, yes, you
could create a Contructor that takes a Customer ID and populates the
instance from the Data Tier. Or you could create a Customer ID property in
the Customer class that, in the setter method, fetches the data from the
Data Tier, and populates the instance with it. The second is the better
method. The first method can also be used, by overloading the Constructor,
and in the variation that takes a Customer ID, use the setter method of the
Customer ID property to populate the class. Very Simple Example:

public class Customer
{
private string _ConnectionString = "Your Default Connection String";
public string ConnectionString
{
get { return _ConnectionString; }
set { _ConnectionString = value; }
}

private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

private int _CustomerID;
public int CustomerID
{
get { return _CustomerID; }
set
{
// Set the private member
_CustomerID = value;

// (Imaginary Data Tier method)
// Note that the Data Tier only knows what it is told
// about the data it''s working with.
// The business tier knows how to use the Data Tier.
DataTable dt = DataTier.GetDataTable(
"TblCustomer", "CustomerID = " + _CustomerID,
_ConnectionString);

// Populate the instance
_FirstName = (int)dt.Rows[0]["FirstName"];
}
}

public Customer() {}

public Customer(string connectionString)
{
_ConnectionString = value;
}

public Customer(string connectionString, int customerID)
{
// Changes the Default Connection String
_ConnectionString = connectionString;

// Note the use of the setter method here
CustomerID = customerID;
}

public Customer(int customerID)
{
// Note the use of the setter method here.
// This overload uses the Default Connection String
CustomerID = customerID;
}

}

The second thing you seem to be missing is that a class and an object are
not the same thing. There is only one copy of a class. There can be many
copies of objects. An object is an instance (or copy) of a class.

So, when you ask

"How do I obtain a collection or ArrayList of Customer objects in the case
where I need to return more than one customer object?"

and you say

"The other option is to put a method in the Customer business object to
return an ArrayList of Customer business objects"

it seems that you''re mixing up classes and objects. The solution is to
create a Collection class that can be used to store multiple Customer
instances. The best thing, rather than using an ArrayList, is to create a
strongly-typed Collection that always works with Customer instances.

Question 3 is answered in my opening remarks about what layer knows what
about the other layers.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"iTISTIC" <sh***@itistic.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
开发一个新的应用程序,并试图使这成为我的第一个真正的OOP / 3层应用程序。我理解演示文稿,业务和数据层的原则。但是,对于应该放置某些功能以及应该如何实现某些功能,我有一些问题。

让我们使用一个简单的例子,如应用程序来管理客户
记录(customer_id,first_name,last_name)。我将拥有一个具有ID,FirstName和LastName属性的Customer
业务对象。我不明白的内容如下:

1.如何获取预装了数据库数据的客户业务对象(例如customer_id 3) )。我可以在Customer业务类中创建一个构造函数,其中customer_id作为参数,并且构造函数调用数据层以返回包含客户数据的数据集,然后设置相应的属性。这是通常如何实现的?我还可以在
Customer数据层中使用一个方法,该方法接受customer_id作为参数,并返回一个Customer业务对象。我唯一的问题是,我在表达层中不应该与数据层进行沟通。这里的一些帮助会很棒。

2.如何在需要返回多个客户对象的情况下获取Customer对象的集合或ArrayList?这样的例子将是列出所有客户的表格。调用客户数据层中的方法来返回客户业务对象的ArrayList也可以在这里工作,但这违反了表示层使用数据的相同规则
层。另一个选项是在Customer业务对象中放置一个方法来返回一个Customer业务对象的ArrayList,但是我的代码必须简单地实例化Customer对象以返回更多的Customer对象。
这是除非我将客户业务对象中的方法共享。

3.数据层方法插入,更新,删除数据库记录 -
insert和update方法接受业务对象作为
参数,还是应该接受一个长参数列表,其中包含每个业务对象属性的
分隔参数?

任何人都可以提供的帮助非常受欢迎。这些问题一直让我无法开发OOP / 3层解决方案,因为我似乎不能强迫自己开发一个系统
而不是真正了解如何这些问题应该以正确的方式解决。

提前感谢您的帮助!

Shawn Berg
Developing a new app and am trying to make this my first truly
OOP/3-Tier app. I understand the principles of the presentation,
business, and data layers. I do, however, have some questions on where
certain functionality should be placed and how some things should be
implemented.

Let''s use a simple example such as an application to manage customer
records (customer_id, first_name, last_name). I''d have a Customer
business object with ID, FirstName, and LastName properties. What I
don''t understand is the following:

1. How do I obtain a Customer business object pre-loaded with data from
the database (customer_id 3 for example). I could create a constructor
in the Customer business class with the customer_id as a parameter and
have the constructor call the data layer to return a dataset containing
the customer''s data and then set the properties accordingly. Is this
how this is normally accomplished? I could also have a method in the
Customer data layer that accepts a customer_id as a parameter and
returns a Customer business object. My only problem with this is that I
have it in my head that the presentation layer should never communicate
with the data layer. Some help here would be great.

2. How do I obtain a collection or ArrayList of Customer objects in the
case where I need to return more than one customer object? Such an
example would be a form that listed all customers. Calling a method in
the Customer data layer to return an ArrayList of Customer business
objects would work here as well, but this is breaking the same rule of
having the presentation layer working with the data layer. The other
option is to put a method in the Customer business object to return an
ArrayList of Customer business objects, but then my code has to
instantiate the Customer object simply to return more Customer objects.
This is unless I make the method in the Customer business object
shared.

3. Data layer methods to insert, update, delete database records -
Should the insert and update methods accept business objects as
parameters or should they accept a long parameter list that contains a
seperate parameter for each of the business object''s properties?

Any help anyone can provide here is more than welcome. These issues
have been keeping me from developing an OOP/3-tier solution for quite
some time as I just can''t seem to force myself to develop a system
without truly knowing how these issues should be tackled the proper
way.

Thanks in advance for your help!

Shawn Berg



Kevin,


到目前为止你们一直非常乐于助人。我很感激你的时间。你能不能给我一个简短的CustomerCollection类的例子以及它是如何实现的?b $ b这是有道理的,但我不确定如何实现

它。


现在提出的另一个问题是为什么业务层

处理特定于数据层的连接字符串?

是否可以将此功能移至数据层并具有

数据tier默认从web.config设置连接字符串

< connectionStrings>变量?我想我可以有一个基类

,所有其他数据类都继承自标题为DataBase的数据类。其中

内置了此功能,以及在必要时更改

连接字符串的属性。这好吗?


再次感谢,


Shawn

Kevin,

You''ve been very helpful thus far. I appreciate your time. Would you be
able to show me an example of a brief CustomerCollection class and how
it is implemented? This makes sense, but I''m not sure how to implement
it.

Another question that has been raised now is why the Business tier
deals with connection strings which are specific to the data tier?
Would it be OK to move this functionality to the data tier and have the
data tier set the connection string by default from a web.config
<connectionStrings> variable? I''m thinking I could have a base class
that all the other data classes inherit from entitled "DataBase" which
has this functionality built in, as well as the properties for changing
the connection string if necessary. Is this ok?

Thanks again,

Shawn


我想我已经找到了一篇很好的文章。这就是你为CustomerCollection类引用的
吗?


然后在这个类中,我可以使用诸如LoadByCity(ByVal
city As String)&,等会填充这个集合?


Shawn

Think I''ve found a good article on this. Is this what you were
referring to for the CustomerCollection class?

And then in this class I could have methods such as "LoadByCity(ByVal
city As String)", etc. which would populate the collection?

Shawn


这篇关于三层开发 - 有点困惑..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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