显示标签为“domain service”的博文。显示所有博文
显示标签为“domain service”的博文。显示所有博文

2010年1月20日星期三

WCF RIA 服务 (十)- Domain Services 3

如何在Domain Services中添加商业逻辑
即使在Domain Services中已经包含了更新、插入、删除等操作,但我们还是经常需要添加一些商业逻辑来管理那些修改数据的过程。还可能需要添加一些有别于传统的新的查询、更新等操作。这节中,我们将学习如何修改数据操作来满足商业要求,还学习如何添加一个命名的更新named update方法和一个调用invoke的操作
在操作数据的方法中添加商业逻辑


  1. 创建更新、插入、删除等应用程序所需的方法 :当在添加新的域服务类的对话框中生成域服务时,选择允许编辑(Enable editing)选项。或添加满足这些操作所需签名的方法。
  2. 在这些方法中,添加代码来指定处理需求的逻辑。
  3. 添加其他满足商业需求的方法。如果不想方法作为服务而公开,可以标记[IgnoreOperationAttribute]属性。

下面是一个插入的方法,此方法指派一个销售员。如果在公司的数据库中有顾客,RetrieveSalesPersonForCompany方法从公司中检索销售员的名字。这个方法标记了IgnoreOperationAttribute属性,所以客户端不能调用这个方法。


public void InsertCustomer(Customer customer
{
if (customer.SalesPerson == String.Empty)
{
customer.SalesPerson = RetrieveSalesPersonForCompany(customer.CompanyName);
}
this.ObjectContext.AddToCustomers(customer);
}

[IgnoreOperation]
public string RetrieveSalesPersonForCompany(string companyname)
{

string salesPersonToAssign = "unassigned";

List customers = GetCustomers().Where(c => c.CompanyName == companyname).ToList();
if (customers.Count > 0)
{
salesPersonToAssign = customers.First().SalesPerson;
}

return salesPersonToAssign;
}

添加命名的更新方法 named update method


  • 在Domain Services中,添加一个满足命名更新方法所需签名的方法。这个方法或者标记[Update]属性并设置UsingCustomMethod为true,或者接受一个实体作为第一个参数并没有返回值。下面的代码允许角色为CustomerRepresentative的用户重新设置客户的密码。

    [RequiresRole("CustomerRepresentative")]
    public void ResetPassword(Customer customer)
    {
    // Implement logic to reset password
    }

当添加一个命名的更新named update方法,在客户端会生成两个方法。一个在Domain context上生成,另一个是由实体生成的,这个实体是这个named update方法的传递参数。从客户端可以使用这两种方式来调用named update方法。调用这个方法后,还需要掉调用SubmitChanges方法。


selectedCustomer.ResetPassword();
customerContext.SubmitChanges(OnSubmitCompleted, null);




添加一个可调用的操作
在Domain Services类中,添加一个标记[Invok]属性的方法。
下面的示例表示如何根据邮编来查询当地的气温。


[Invoke]
public int GetLocalTemperature(string postalcode)
{
// Implement logic to look up temperature
}

然后可以通过使用InvokeOperation(TValue)对象来调用这个方法。如下所示:


InvokeOperation invokeOp = customerContext.GetLocalTemperature(selectedPostalCode);

如何使用HTTPS与Domain Services
当我们想改善建立在Domain Services上的通信安全时,可以配置Domain Services只接受https上的请求。此时Domain Services将拒绝所有http上的请求。当Domain Services配置为应用于https时,相应的DomainContext类也对所有的请求使用https。
想使用https,我们需要配置下Web服务器。可以通过How to Set Up an HTTPS Service in IIS和Configuring HTTP and HTTPS获得详细信息。配置完服务器后,还需要下面的步骤来指定Domain Services使用HTTPS。
当应用EnableClientAccessAttribute属性时,设置REquiresSecureEndpoint为true,代码如下

[EnableClientAccess(RequiresSecureEndpoint = true)]
public class AuthenticationDomainService : AuthenticationBase

2010年1月19日星期二

WCF RIA 服务 (九)- Domain Service 2

演练:添加查询方法
查询数据源的方法有时被叫做查询方法。在WCF RIA Services中,查询方法必须以框架承认的方式来定义。此外,只返回一个实体的查询和有可能返回多个实体的查询定义是不同的。
当我们建立一个新的domain service类并在Add New Domain Service Class 对话框中指定实体时,RIA Services框架会自动为每一个服务端公开的实体创建一个简单的查询。这个简单的查询方法检索实体的所有数据。这个演练将描述如何添加一个用参数值来过滤结果的复杂查询方法。还描述了如何添加一个返回单个实体和一个实体集合的查询。
添加一个接受参数并返回单一实体的查询方法

  1. 打开我们第三节中创建的RIAServicesExample解决方案。

  2. 在服务端,打开从Customer表公开数据的domain Services 类。这个类应该叫做CustmerDomainService。

  3. 添加一个查询方法,这个方法接受一个整数类型的参数并返回符合Customer ID的Customer实体。 如果返回单一实体的方法包含Query属性,必须设置IsComposable为false. 用户不能从客户端指定其他的查询操作。如果这个查询方法满足了作为查询所期望的签名,我们就不必使用[Query]属性。返回值必须是任何实体对象的单一实例。

    [Query(IsComposable=false)]



    public Customer GetCustomersByID(int customerID)



    {



    return this.ObjectContext.Customers.FirstOrDefault(c => c.CustomerID == customerID);



    }







添加一个接受一个参数并返回一个实体集合的查询方法

  1. 打开从Customer表公开数据的domain service类。名字应为CustomerDomainService。

  2. 添加一个方法,这个方法接受一个字符型参数并返回所有名字以参数开始的客户。这个方法可以返回一个IQueryable<>对象,因为用户可能想从客户端提供额外的查询。

    public IQueryable GetCustomersByLastNameLetter(string startingLastNameLetter)



    {



    return this.ObjectContext.Customers.Where(c => c.LastName.StartsWith(startingLastNameLetter) == true);



    }




在客户端显示这些查询的结果

  1. 在客户端打开MainPage.xaml文件。

  2. 添加两个TextBox控件和两个Button控件,这样用过就可以通过ID或名的首字母来过滤。下面的xaml代码显示了DataGrid的完整布局。








































































































  3. 打开MainPage.xaml的代码文件。


  4. 添加代码来根据用户的输入来检索数据。

    public partial class MainPage : UserControl



    {



    private CustomerDomainContext _customerContext = new CustomerDomainContext();







    public MainPage()



    {



    InitializeComponent();



    }







    private void LetterButton_Click(object sender, RoutedEventArgs e)



    {



    IDButton.IsEnabled = false;



    LetterButton.IsEnabled = false;



    LoadOperation loadOp = this._customerContext.Load(this._customerContext.GetCustomersByLastNameLetterQuery(LetterValue.Text), CustomerLoadedCallback, null);



    CustomerGrid.ItemsSource = loadOp.Entities;



    }







    private void IDButton_Click(object sender, RoutedEventArgs e)



    {



    IDButton.IsEnabled = false;



    LetterButton.IsEnabled = false;



    LoadOperation loadOp = this._customerContext.Load(this._customerContext.GetCustomersByIDQuery(int.Parse(IDValue.Text)), CustomerLoadedCallback, null);



    CustomerGrid.ItemsSource = loadOp.Entities;



    }







    void CustomerLoadedCallback(LoadOperation loadOperation)



    {



    IDButton.IsEnabled = true;



    LetterButton.IsEnabled = true;



    }







    }







  5. 运行解决方案。将会看到如下结果

WCF RIA 服务 (八)-- domain services 1

domain Services 是向客户端公开数据访问层的WCF Services。当我们创建一个domain services实例时,就指定了想要公开的实体类,以及这个domain Services所允许的数据操作。
DomainService类 和派生类
DomainService类是所有做为domain Services的服务类的基类。WCF RIA Services还提供了LinqToEntitiesDomainService(TContext)和LinqToSqlDomainService(TContext)类,这两个类是从DomainService类派生出来的抽象类。
如果想创建绑定一个ADO.NET实体模型或一个LINQ to SQL类,我们需要分别创建从LinqToEntitiesDomainService(TContext)或LinqToSqlDomainService(TContext)派生的类。如果创建绑定到自定义数据对象的domain Service,就需要创建一个从DomainService类派生的类。当我们使用Add New Domain Service Class对话框来创建domain service时,会根据想要公开的实体自动创建正确的domain service类型。
一个domain service必须标记EnableClientAccessAttribute属性,才能被客户端项目引用。
通过在domain service中添加方法来执行我们想公开的数据操作。例如,可以添加方法来执行以下操作:
  • 查询
  • 更新
  • 插入
  • 删除

还可以添加更复杂的操作,如下:

  • 处理操作 Resolve - 在服务端通过代码来处理发生的冲突。
  • 调用操作 Invoke - 那些不用追踪或延期运行的操作。
  • 命名的更新操作 Named Update - 不是简单修改的自定义操作。

命名约定

当我们添加方法来执行这些操作时,这些方法必须满足这些操作需要的签名。除了满足签名外,方法还应包含一个满足这个操作命名约定的名字前缀。如果方法的名字不是由预期的名字前缀开始的,就必须对这个操作应用一致的属性。如果操作的名字满足命名约定,那么属性就是可选的。

我们不能重载那些是域操作的方法。我们必须对每一个可以从客户端调用的方法指定唯一的名字。所有表示domain service操作的方法都应该是public的。方法对参数和返回值应使用串行类型。

可以通过对一个已经公开的方法使用IgnoreOperationAttribute属性,来阻止这个方法的调用。

下面的表提供了数据操作的签名:

Query

Return value : Itenumerable<T>,IQueryable<T>,ot entity

Parameters : Any number

Name Prefix : Any name

Attribute : [Query] (c#) or <Query()>(VB)

Example : public IQueryable<Product> GetProducts()

Update

Return value : None

Parameters : Entity

Name Prefix : Update, Change, or Modify

Attribute : [Update]

Example : public void UpdateProduct(Product product)

Insert

Return value : None

Parameters : Entity

Name Prefix : Insert, Add, or Create

Attribute : [Insert]

Example : public void InsertProduct(Product product)

Delete

Return value : None

Parameters : Entity

Name Prefix : Delete or Remove

Attribute : [Delete]

Example : public void DeleteProduct(Product product)

Resolve

Return value : Boolean

Parameters : Current entity, Original entity, Store entity, Boolean

Name Prefix : Resolve

Attribute : [Resolve]

Example : public bool ResolveProduct( Product currentProduct, Product orininalProduct, Product storeProduct, bool deleteOperation)

Invoke

Return value : any

Parameters : any

Name Prefix : any

Attribute : [Invoke]

Example :

[Invoke]

public decimal GetCompetitorsPrice(Product product)

Named Update

Return value : None

Parameters : entity, any number of other parameters

Name Prefix : 任何名字,只要和插入、更新、删除等操作的名字前缀不同即可。

Attribute : [Update(UsingCustomMethod=true]

Example :

[Update(UsingCustomMethod=true]

public void DiscontProduct(Product product, int percentage)

添加应用逻辑到Domain Service

在定义了要公开的数据操作之后,就可以把所需的应用逻辑添加到domain service类了。可以直接把逻辑添加到操作方法中,也可以添加到操作方法可调用的其他方法中。

WCF 和 Domain Services

domain service是建立在WCF概念上的。domain service保留了下面的特点:

  • WCF Service的标准使用方式。
  • 以有的WCF编程模型结构,例如操作合约,操作行为,以及服务行为。
  • 标准的WCF定制功能,例如绑定配置、行为配置等

domain context和domain service的交流是通过使用WCF ChannelFactory创建一个通道,并向它传递一个从domain service生成的服务合约。