【发布时间】:2016-03-18 10:58:54
【问题描述】:
背景:我需要发布一个简单的项目,它将用于将数据从我的存储传输到其他地方(其他将使用我的 WCF 的开发人员。WCF 将用于传输数据(保护数据库等) . 我在我的项目中使用了洋葱架构(基础部分)。
--------------------------------------
| WCF |
| ---------------------------------- |
| | Interfaces for working with DB | |
| | ------------- | |
| | |DomainModel| | |
| | ------------- | |
| -----------------------------------|
--------------------------------------
我用一个简单的例子来展示我的问题:
我有这样的 DomainModel:
[DataContract]
public class User
{
private string _name;
private List<Text> _texts;
public User(string name, List<Text> texts)
{
_name = name;
_texts = texts;
}
[DataMember]
public string Name { get { return _name; } }
[DataMember]
public List<Text> Texts { get { return _texts; } }
}
[DataContract]
public class Text
{
private string _name;
public Text(string name)
{
_name = name;
}
[DataMember]
public string Name { get { return _name; } }
}
WCF 服务有这个方法:
public DomainModel.User ReturnUser()
{
User user = new User("Texts",
new List<Text>()
{
new Text("TextOne"),
new Text("TextTwo")
});
return user;
}
但是当我调用方法时
static void Main(string[] args)
{
try
{
ServiceRomanClient client = new ServiceRomanClient();
User user = client.ReturnUser();
Console.WriteLine(user.Name);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
我得到了这个例外: 这可能是由于服务端点绑定未使用 HTTP 协议。 这也可能是由于服务器中止了 HTTP 请求上下文 (可能是由于服务关闭)。有关详细信息,请参阅服务器日志。
如果我在此更改 DomainModel 中的属性(我添加了集合)
[DataContract]
public class User
{
private string _name;
private List<Text> _texts;
public User(string name, List<Text> texts)
{
_name = name;
_texts = texts;
}
[DataMember]
public string Name { get { return _name; } set{ _name = value; } }
[DataMember]
public List<Text> Texts { get { return _texts; } set { _texts = value; } }
}
[DataContract]
public class Text
{
private string _name;
public Text(string name)
{
_name = name;
}
[DataMember]
public string Name { get { return _name; } set{ _name = value;} }
}
我没有执行力,但我违反了 S.O.L.I.D 原则。
我不想在DomainModel中添加set,我建议解决这个问题的更好方法是在DomainModel和WCF之间创建一个传输层。
如果您能根据您的经验告诉我您的想法,我将不胜感激。
【问题讨论】:
标签: c# wcf soa solid-principles onion-architecture