简体   繁体   English

使用WCF WebService中的模型

[英]Using a Model from a WCF WebService

Not really sure if the title is correct, but I'll explain it further. 不确定标题是否正确,但我会进一步解释。

At the moment I have two web applications; 目前我有两个Web应用程序; one being an ASP.NET MVC 5 application and one being a WCF Service. 一个是ASP.NET MVC 5应用程序,另一个是WCF服务。 The WCF Service is a service containing all the business logic and the data access layer. WCF服务是包含所有业务逻辑和数据访问层的服务。 For accessing my database, I use Entity Framework 6. 为了访问我的数据库,我使用Entity Framework 6。

I am trying to create a Controller for accessing some data in the database through the MVC application. 我正在尝试创建一个Controller,用于通过MVC应用程序访问数据库中的某些数据。 It should have regular CRUD operations for testing purposes. 它应该有常规的CRUD操作用于测试目的。 The problem I ran into is that because my DAL is in the WCF Service, I continuously have to cast the retrieved data from the service to a model in my ASP.NET MVC application for it to be shown. 我遇到的问题是因为我的DAL在WCF服务中,所以我不得不将检索到的数据从服务转换到我的ASP.NET MVC应用程序中的模型,以便显示它。

My question is; 我的问题是; is it possible to use the EF-generated classes on my WCF Service to function as model in my ASP.NET MVC application? 是否可以在我的WCF服务上使用EF生成的类作为我的ASP.NET MVC应用程序中的模型?

For clarity, I added some codesamples; 为清楚起见,我添加了一些代码示例;

WCF EF-generated object WCF EF生成的对象

public class WCFPerson
{
    public string Name { get; set; }
    public int Age { get; set; }
}

WCF Service WCF服务

[WebMethod]
public List<WCFPerson> GetAllPersons()
{
    using (var db = new SomeDbcontext())
    {
        return db.WCFPersons.ToList();
    }
}

ASP.NET MVC Model ASP.NET MVC模型

public class Person
{
    [Required]
    public string Name { get; set; }

    [Required, RegularExpression("([0-9]+)")] 
    public int Age { get; set; }
}

ASP.NET MVC Controller ASP.NET MVC控制器

public class PersonController : Controller
{
    private WCFService.WebServiceClient client = new WCFService.WebServiceClient();

    public ActionResult Index()
    {
        var persons = client.GetAllPersons();
        var model = new List<Person>();

        //My problem:
        foreach (var person in persons)
        {
            model.Add(new Person() { Name = person.Name, Age = person.Age });
        }

        return View(model);
    }
}

I find this casting is very annoying. 我觉得这个演员非常讨厌。 Imagine a scenario where the database changes, nuff said. 想象一下数据库发生变化的情景,nuff说。 Also, this example is small - my current Model has about 15 properties and it's very annoying to cast them all. 此外,这个例子很小 - 我目前的模型有大约15个属性,并且将它们全部投射非常烦人。

There is no chance I put the database connection directly in the MVC application. 我没有机会直接将数据库连接放在MVC应用程序中。 It has to be in the WCF Service for specific reasons. 它必须出于特定原因而在WCF服务中。 If it would be possible, I would've already done that . 如果有可能的话,我已经做到了

Edit 1: Added DataAnnotations. 编辑1:添加了DataAnnotations。 This is actually quite important. 这实际上非常重要。

You should be able to use it directly as the model. 您应该可以直接将其用作模型。 There is no problem to do it so. 这样做没有问题。

Another thing, to make the conversion you could also use Linq: 另一件事,要进行转换,你也可以使用Linq:

model=persons.Select(t=>new Person{
                          Name=t.Name,
                          Age=t.Age
                          });

This way is more compact. 这种方式更紧凑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM