【发布时间】:2014-04-24 05:40:03
【问题描述】:
我需要在 C# 中通过引用传递 this 对象。但如您所知,这是不可能的。 我有一个多层应用程序。简而言之,DAL 从 Web 服务中获取 JSON 格式的数据。 JSON 数据必须在业务层对象中进行转换。因此,我初始化了业务层对象并将其传递给 DAL。 DAL 会将数据转换为对象。我展示了一个代码示例。首先是 DAL:
public Stream GetSession ( ref BusinessLayer.Session session)
{
Stream dataStream;
// Use the ServiceManager to build send the data to the services
// SOAServiceManager sm = new SOAServiceManager("http://www.Test.da/authentication.json","",DataAccessLayer.HTTPMethod.POST);
// Add the Values
sm.AddCriteriaValue ("name","demo");
sm.AddCriteriaValue ("password","demo");
// Now it's show time and the datastream is full
dataStream = sm.CommitPost ();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(BusinessLayer.Session));
session = (BusinessLayer.Session)ser.ReadObject(dataStream);
return dataStream;
}
现在业务层正在使用这个 DAL 类:
namespace BusinessLayer
{
public class Session
{
public bool Success { get; set; }
public string Name { get; set; }
public Session ()
{
DataAccessLayer.Session dal_Session = new DataAccessLayer.Session ();
dal_Session.GetSession ( ref this);
}
}
}
所以问题是,不可能发送“this”作为参考。 所以我看到的解决方案是创建一个复制对象并将其发送到 DAL,然后将其值分配给 this 对象。但这不是一个聪明的解决方案。 有没有办法在 C# 中解决这个问题?
【问题讨论】:
-
我已经阅读了那篇文章,但这不是一个解决方案。如您所见,我已经写过这是可能的,但并不聪明。在这种情况下,“我”会被填满,但“这个”对象不会改变。所以你必须再次将“me”对象的值赋给“this”对象。
标签: c# design-patterns data-access-layer multi-tier