RIAServices手动构建的DomainService不支持的类型 [英] RIAServices unsupported types on hand-built DomainService

查看:78
本文介绍了RIAServices手动构建的DomainService不支持的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的EF模型是从我的SQL Server数据库生成的。然后我为EF模型生成了RIAServices的DomainService。其中一个实体称为EntryCategories。 DomainService创建了这个方法:

  public IQueryable< EntryCategories> GetEntryCategoriesSet()
{
return this.Context.EntryCategoriesSet;
}

由于我的用户界面显示模型与物理模型看起来截然不同,所以我决定为其和相关实体编写我自己的DomainService。是的,我知道我们是修改生成的,但是在这里有很多东西,我想集中在一个小东西。



我删除了> EnableClientAccess 属性,并添加一个名为ClientDomainService的新类,并在其中封装生成的DomainService:

  [EnableClientAccess()] 
public class ClientDomainService:DomainService
{
//生成的域服务封装在我的新帐户中。
private DataDomainService _dcds = new DataDomainService();

//重新实现DataDomainService方法之一
public IQueryable< EntryCategories> GetEntryCategories()
{
返回(从_dcds.GetEntryCategoriesSet()中的t,其中t.EntryCategoriesVersions.EntryCategoriesVersionId == datahead.EntryCategoriesVersions.EntryCategoriesVersionId order by t.DisplayOrder select t);
}
}

我尝试的第一件事是重新实现GetCateogoriesSet方法,但是基于我的类中另一个实体(未显示)过滤基础数据。但是当我构建它时,会出现一个错误:

 实体'DataProject.Web.EntryCategories'具有一个属性EntryCategoriesVersionsReference不支持的类型

如果我注释掉我的CientDomainService,请将 EnableClientAccess 属性替换在生成的DomainService上,并将原始的GetEntryCategoriesSet方法中的analagous linq过滤器放在原始的GetEntryCategoriesSet方法中,该项目编译没有错误。



一个没有?这是metadata.cs文件吗?

解决方案

生成的域服务的特殊功能不是.metadata.cs文件可以保留它,并使用它,但它不能解决您的问题)。



问题出现某种原因,因为RIA服务(?)需要一个域服务描述提供程序'为暴露的Linq到EF实体。 LinqToEntitiesDomainService类具有已应用的LinqToEntitiesDomainServiceDescriptionProviderAttribute,因此从其继承的生成的域服务也继承提供程序。



当您构建自己的自定义域服务时,派生自DomainService,并通过它暴露实体,您需要自己应用此属性。此外,由于提供程序无法从域服务基类(如果基类为LinqToEntitiesDomainService)中可以和执行的方式推断对象上下文类型,则需要在属性构造函数中指定对象上下文类型,如下所示:

  [EnableClientAccess()] 
[LinqToEntitiesDomainServiceDescriptionProvider(
typeof(YourObjectContextType))]
public class ClientDomainService :DomainService
{
...
}

修复它。



请注意,这意味着如果您希望将您的对象上下文从您的域名服务抽象出来,您将失望。我选择了似乎流行的存储库模型,其中所有在对象上下文中运行的代码都进入域服务器使用的提供者。这有助于单元测试,但显然不会删除域服务对对象上下文的依赖。 RIA服务需要使用上下文来理解您的内容,或至少由域实体引用的内容(例如您的案例中的EntryCategoriesVersions)。


My EF model was generated from my SQL Server database. I then generated a DomainService for RIAServices against the EF model. One of the entities is called "EntryCategories". The DomainService created this method:

public IQueryable<EntryCategories> GetEntryCategoriesSet()
{
    return this.Context.EntryCategoriesSet;
}

Since my user interface display model looks quite different from the physical model, I decided to write my own DomainService for that and related entities. Yes, I know we are meant to modify the generated one but it has so much stuff in there and I wanted to focus on a small thing.

I removed the EnableClientAccess attribute from the generated DomainService and added a new class called ClientDomainService, and encapsulated in it the generated DomainService:

[EnableClientAccess()]
public class ClientDomainService : DomainService
{
    // the generated domain service encapsulated in my new one.
    private DataDomainService _dcds = new DataDomainService(); 

    // reimplement one of the DataDomainService methods
    public IQueryable<EntryCategories> GetEntryCategories()
    {
        return (from t in _dcds.GetEntryCategoriesSet() where t.EntryCategoriesVersions.EntryCategoriesVersionId == datahead.EntryCategoriesVersions.EntryCategoriesVersionId orderby t.DisplayOrder select t);
    }  
}

The very fist thing I tried is to reimplement the GetCateogoriesSet method but with the underlying data filtered based on another entity in my class (not shown). But when I build this, an error shows up:

Entity 'DataProject.Web.EntryCategories' has a property 'EntryCategoriesVersionsReference' with an unsupported type

If I comment out my CientDomainService, replace the EnableClientAccess attribute on the generated DomainService, and place the analagous linq filtering in the original GetEntryCategoriesSet method, the project compiles with no errors.

What is so special about the generated DomainService that my new one doesn't have? Is it that metadata.cs file?

解决方案

What's special about the generated domain service is not the .metadata.cs file (you can keep it, and use it, but it doesn't solve your problem).

The problem appears somehow because RIA services (?) needs a 'domain service description provider' for the exposed Linq to EF entities. The LinqToEntitiesDomainService class has the LinqToEntitiesDomainServiceDescriptionProviderAttribute, already applied, so the generated domain services which inherit from it also inherit the provider.

When you build your own custom domain service, derived from DomainService, and expose entities through it, you need to apply this attribute yourself. Furthermore, since the provider cannot infer the object context type from the domain service base class (which it can and does if the base class is LinqToEntitiesDomainService), you need to specify the object context type in the attribute constructor, like this:

[EnableClientAccess()]
[LinqToEntitiesDomainServiceDescriptionProvider(
                            typeof(YourObjectContextType))]
public class ClientDomainService : DomainService
{
    ...
}

That should fix it.

Note that this means if you had hoped to abstract your object context away from your domain service, you'll be disappointed. I had opted for the seemingly popular repository model where all code that operates on the object context goes into a provider used by the domain service. This facilitates unit testing, but evidently doesn't remove the domain service's dependency on the object context. The context is required for RIA Services to make sense of your entites, or at least those referenced by the domain entity (such as EntryCategoriesVersions in your case).

这篇关于RIAServices手动构建的DomainService不支持的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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