如果WCF在MVC应用程序中,它是否应该使用控制器访问数据库以保持“DRY”?
我有一个访问 SQL 和 Windows Azure 的 MVC 应用程序。逻辑流程如下所示:
Person <--> View <--> Controller.ConvertPersonHere(x) <--> StorageContext.DoDataAction <--> AzurePersonTableEntity
ConvertPersonHere 是 此堆栈的答案溢出问题 它将模型实体转换为存储实体
public class Person
{
public string Name {get;set;}
public int ID {get;set;}
}
public class PersonEntity : TableServiceEntity
{
public string Name {get;set;}
public int ID {get;set;}
// Code to set PartitionKey
// Code to set RowKey
}
现在我将 WCF 添加到组合中,我应该如何访问数据函数?假设我当前在控制器中有一个
.Save(Person)
方法,并且希望从我的 WCF 调用中Save(Person)
。我需要抽象出控制器中的数据操作吗?
I have an MVC application that accesses SQL and Windows Azure. The logical flow looks like this:
Person <--> View <--> Controller.ConvertPersonHere(x) <--> StorageContext.DoDataAction <--> AzurePersonTableEntity
ConvertPersonHere is the answer to this Stack Overflow question and it converts the Model entity to the Storage entity
public class Person
{
public string Name {get;set;}
public int ID {get;set;}
}
public class PersonEntity : TableServiceEntity
{
public string Name {get;set;}
public int ID {get;set;}
// Code to set PartitionKey
// Code to set RowKey
}
Now that I'm adding WCF to the mix, how should I go about accessing data functions? Assume I have currently have a method to
.Save(Person)
in the controller and want toSave(Person)
from my WCF call.Do I need to abstract out the data actions in the controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这是一个切线,但如果您混合使用 WCF 和 ASP.NET MVC,您至少应该了解 OpenRasta。 主要贡献者的 Herding Code 播客是一个好的开始。
(不,这甚至不是为了回答您的实际问题!)
I know it's a tangent, but if you're mixing WCF and ASP.NET MVC, you should at least be aware of OpenRasta. A good start is this Herding Code podcast with the main contributor.
(No, this is not even intended to answer your actual question!)
我会像这样重构代码 - 将功能从 Person 转换为 PersonEntity,反之亦然到单独的映射器,将保存功能也移动到单独的存储库,并将用于调用映射器和存储库的控制器代码也移动到单独的服务。
因此,控制器中的方法将类似于:
在 WCF 服务中,您将能够重用代码。为了使用 DataAnnotations 属性验证 WCF 中的类,您可以使用类似于以下的方法 - http://blog.jorgef.net/2011/01/odata-dataannotations.html
I would refactor the code like this - move the functionality to convert from Person to PersonEntity and vice versa to a separate mapper, move saving functionality to separate repository as well, and move controller's code for invoking mapper and repository to separate service too.
So methods in your controller will look similar to:
And in your WCF service you'll be able to reuse the code. In order to validate the classes in WCF using DataAnnotations attributes, you can use the approach similar to the following - http://blog.jorgef.net/2011/01/odata-dataannotations.html
在此示例中,如果您的 Mvc 项目已消失并被 Wpf 项目取代,您的其他功能仍然可用。如果您有这两个项目,它们可以引用核心功能。在其他项目中有与UI(MVC或WPF)无关的实现。这样那些 UI 项目就可以引用此功能。
Mvc或Wpf项目
核心项目
基础设施项目
From this example, if your Mvc project was gone and replaced by a Wpf project, your other functionality is still available. If you have both projects they can reference core functionality. Have the implementation which has no relation to UI (MVC or WPF) in other projects. This way those UI projects can reference this functionality.
Mvc or Wpf project
Core project
Infrastructure project