在x层类之间传递对象,值或结果的最佳方法是什么? [英] How is the best way to pass object, values or results between x-layer classes?

查看:48
本文介绍了在x层类之间传递对象,值或结果的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起这个愚蠢的问题,我知道,但有时候需要从基本开始



我不知道如何传递结果从DAL类产生的方法

到BL类

按原样返回结果。


我的意思是,例如这样的事情。


名称空间DAL

{

公共类预订

{

....

}


公共类书籍

{

....

public< List> Book GetList()

{

.....

}

}

}


名称空间BL

{

公共类预订

{

与DAL结构相同

}


公共类书籍

{

public< List> Book GetList()

{

这里我需要一个新的DAL对象并获得

getList函数的结果。

}

}

}


我知道这是OOP编程的基础,但我正在学习......并预订

总是说你不能或者不得......但是从来没有说过如何解决。


我知道返回不同的类型是不可能的,因为返回

对象没有我没有共享相同的界面,但是我试图实现

界面以及继承而没有好结果。

有人可以这么亲切一试以简单的方式解释我或提供一个简短的

代码来分析并帮助我了解我的错误?


谢谢

Andrea

Sorry for the stupid question, I know, but sometimes is necessary starts
from the basic.
I don''t know how to pass the result of a method generated from a DAL class
to a BL class
returning the results as it is.

I mean, for instance, something like this.

namespace DAL
{
public class Book
{
....
}

public class Books
{
....
public <List>Book GetList()
{
.....
}
}
}

namespace BL
{
public class Book
{
same structure as DAL
}

public class Books
{
public <List>Book GetList()
{
Here I need to istanciate a new DAL object and get the result of
getList function.
}
}
}

I know this is the basic of OOP programming, but I''m learning ... and book
always says that you can''t or mustn''t ... but never says how to solve.

I know that is impossibile to return a different type since the returning
object doesn''t share the same interface, but I''ve tried to implement the
interface as well as the inheritance with no good result.
Can someone be so kindly try to explain me in a simple way or provide a short
code to analize and that help me to learn my errors?

Thank you
Andrea

推荐答案

" andrea" <去********** @ freemail.it> écritdansle message de news:
9f ************************** @ news.microsoft.com ...


|对不起这个愚蠢的问题,我知道,但有时需要开始

|从基本的。

|我不知道如何传递从DAL类生成的方法的结果

| BL课程

|返回结果。

|

|我的意思是,例如,这样的事情。

|

|命名空间DAL

| {

|公共课预订

| {

| ....

| }

|

|公共课书籍

| {

| ....

| public< List> Book GetList()

| {

| .....

| }

| }

| }

|

|命名空间BL

| {

|公共课预订

| {

|与DAL相同的结构

| }

|

|公共课书籍

| {

| public< List> Book GetList()

| {

|在这里,我需要istanciate一个新的DAL对象,并获得

|的结果getList函数。

| }

| }

| } $ / $

你的第一个问题是命名空间BL中的Book类与命名空间DAL中的Book类不同。

。 />
通常,DAL被实例化为Singleton或被设计为静态方法的静态
类。检索列表的方法可以声明为




公共静态类DAL

{

...


公共静态列表< T> GetList()

{

//创建列表并使用反射填充它

}

}


您真的不需要图书课程,因为这可以通过使用List< Book>

列表来涵盖。


您的主叫代码应如下所示:


{

List< Book> books = DAL.GetList< Book>();

}


Joanna


-

Joanna Carter [TeamB]

顾问软件工程师
"andrea" <go**********@freemail.it> a écrit dans le message de news:
9f**************************@news.microsoft.com...

| Sorry for the stupid question, I know, but sometimes is necessary starts
| from the basic.
| I don''t know how to pass the result of a method generated from a DAL class
| to a BL class
| returning the results as it is.
|
| I mean, for instance, something like this.
|
| namespace DAL
| {
| public class Book
| {
| ....
| }
|
| public class Books
| {
| ....
| public <List>Book GetList()
| {
| .....
| }
| }
| }
|
| namespace BL
| {
| public class Book
| {
| same structure as DAL
| }
|
| public class Books
| {
| public <List>Book GetList()
| {
| Here I need to istanciate a new DAL object and get the result of
| getList function.
| }
| }
| }

Your first problem is that the Book class in namespace BL is not the same as
the Book class in namespace DAL.

Normally, a DAL is instantiated as a Singleton or is designed as a static
class of static methods. The method fro retrieving a list can be declared as
:

public static class DAL
{
...

public static List<T> GetList()
{
// create list and populate it using reflection
}
}

You really don''t need a Books class as this is covered by using a List<Book>
list.

Your calling code should look something like this :

{
List<Book> books = DAL.GetList<Book>();
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


感谢您快速回复Joanna。

我需要澄清一下....


J>你的第一个问题是命名空间BL中的Book类不是

J>与命名空间DAL中的Book类相同。

我知道......它们是两个组件,这是非常重要的事情。


J>通常,DAL被实例化为Singleton或被设计为

J>静态类的静态方法。检索列表的方法可以是

J>声明为:

一个Singleton类听起来不错。我认为它在winform中是正确的

场景,

但在webform中我认为DAL不是......单身或者是/ br / >
没有,每次

页面结束加载每个对象已经死亡。


J> public static List< T> GetList()

J> {

J> //创建列表并使用反射填充它

J> }


在这里,我不明白你为什么谈论反思。你能发一个样品



能让我更好看的东西吗?


J>你真的不需要一个图书课程,因为这是通过使用

J>列表与LT;图书>清单。

这里的黑洞...如果我不需要Book课程,我的名单怎么知道

什么类型
$ b这是$ b?你的意思是BL书课?

谢谢

Andrea

Thanks for your quickly reply Joanna.
I need some clarification about it ....

J> Your first problem is that the Book class in namespace BL is not the
J> same as the Book class in namespace DAL.
I know ... they are two assemblies, and this is the real important thing.

J> Normally, a DAL is instantiated as a Singleton or is designed as a
J> static class of static methods. The method fro retrieving a list can
J> be declared as :
Well a Singleton class sounds good. I imagine that it can be right in winform
scenario,
but in webform I suppose that it doesn''t matter as DAL is ... singleton or
not, every time
the page ends the loading every object has died.

J> public static List<T> GetList()
J> {
J> // create list and populate it using reflection
J> }

Here I don''t understand why you speak about reflection. Can you post a sample
or
something that let me better undestand?

J> You really don''t need a Books class as this is covered by using a
J> List<Book> list.
Here the black hole ... if I don''t need the Book class, how can my list know
of what type
is it? You mean the BL book class?
Thanks
Andrea


这样的东西?


使用System;

使用System.Collections.Generic;


名称空间DAL

{

公共课本书

{

公共预订()

{

_author =" Eric Baldwin" ;;

_title =" The big panter";

}


公共字符串作者

{

get {return _author; }

set {_author = value; }

}


公共字符串标题

{

get {return _title; }

set {_title = value; }

}


private string _title;

private string _author;

}


公共类Class1:列表< book>

{

public Class1(){}


public List< book> test()

{

List< book> c =新列表< book>();


for(int i = 0; i< = 5; i ++)

{

c.Add(新书());

}


返回c;

}

}

}


命名空间BL

{

使用dl = DAL ;


公共班Class1

{

public Class1()

{}


公开列表< dl.book> test()

{

dl.Class1 x = new DAL.Class1();

List< dl.book> lst = x.test();


返回lst;

}

}

}


命名空间GUI

{

使用b = BL;

使用a = DAL;


公共课测试

{

[STAThread]

static void Main()

{

b.Class1 x = new b.Class1();

List< a.book> lst = x.test();

}

}

}


有什么奇怪的对我来说,GUI应该引用DAL程序集。

这是完成任务的正确方法吗?


谢谢

Andrea

Something like this?

using System;
using System.Collections.Generic;

namespace DAL
{
public class book
{
public book()
{
_author = "Eric Baldwin";
_title = "The big panter";
}

public string Author
{
get { return _author; }
set { _author = value; }
}

public String Title
{
get { return _title; }
set { _title = value; }
}

private string _title;
private string _author;
}

public class Class1: List<book>
{
public Class1(){}

public List<book> test()
{
List<book> c = new List<book>();

for (int i = 0; i <= 5; i++)
{
c.Add(new book());
}

return c;
}
}
}

namespace BL
{
using dl = DAL;

public class Class1
{
public Class1()
{ }

public List<dl.book> test()
{
dl.Class1 x = new DAL.Class1();
List<dl.book> lst = x.test();

return lst;
}
}
}

namespace GUI
{
using b = BL;
using a = DAL;

public class Test
{
[STAThread]
static void Main()
{
b.Class1 x = new b.Class1();
List<a.book> lst = x.test();
}
}
}

What is strange for me is that GUI should has the DAL assembly referenced.
Is this the right way to accomplish the task?

Thanks
Andrea


这篇关于在x层类之间传递对象,值或结果的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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