Dealing with the concurrency problem of peasy.net learning

Source: Internet
Author: User

Concurrent processing of Peasy.net

Businessservicebase is a custom implementation of ServiceBase that provides additional unique features

First, create a business service that must inherit Businessservicebase and fulfill three contractual obligations:
1, create a DTO, your dto will define an id attribute that needs to be specified as a TKey common parameter . allows your dto to implement Iversioncontainer to participate in concurrent processing .

2, create a data broker implementationIServiceDataProxy<T, TKey>,或通过创建从一个继承的数据代理这些类。

3, create a class that inherits Businessservicebase, specify the DTO (T) and ID (TKey), as a generic type parameter, and require the data proxy as a constructor parameter and pass it to the base class constructor

For example

public class Businessservicebasemock:businessservicebase<person, long>//Common classes
{
Public Businessservicebasemock (Iservicedataproxy<person, long> dataproxy): Base (Dataproxy)//base is the base class, This means that the arguments to the constructor are passed to the base class constructor,
{
}
}

IServiceDataProxy<Person, long>must be Required constructor Arguments the constructor passed to the businessservicebase.

Concurrent Processing:

Concurrency processing ensures that updates to the data store do not break changes made by other users. Businessservicebase supports concurrent processing through the first-write-wins scenario.

through create a specific implementation of Businessservicebase and make the dependent DTO Implement Iversioncontainer,    Concurrent processing can be easily implemented . It's an official explanation, but it's not quite understood.

In fact, that is, in the abstract class of the Declaration

This is his concrete inheritance implementation class

This person inherits the Iversioncontainer.

Version is declared inside the Iversioncontainer interface.

Businessservicebase will be covered separately servicebase.update and the Servicebase.updateasync , respectively in here and the here to provide its own implementation . These methods check whether the provided DTO implements the Iversioncontainer interface. If this is true , the value is compared to the Version current value in the data store . If the version is different, businessservicebase will throw concurrencyexception, otherwise it will allow the execution process to continue.

The Businessservicebase abstract class overrides the Update method and the Updateasync method

Both methods examine whether the DTO entity object implements the interface Iversioncontainer, and if it does, the value is compared to the Version current value in the Datastore Compare . If the version is different, businessservicebase will throw concurrencyexception, otherwise it will allow the execution process to continue.

need to be aware of is injected into the Businessservicebase instance of the Data Agent updates and retrieval of DTOs must be handled. Version property to enable concurrency to work correctly.

when your service class is exposed DTO when , they may leak a lot of data that should not be updatable. For example, create and update time stamp fields such as dates. These fields, although useful to customers, may not be updated by the customer. status is another good example, because you might not want customers to change these values, but instead want to present their business logic in their own workflows or in a similar form.

Businessservicebase provides opt-out support for certain fields that prohibit changes to DTOs. This can be done by System.ComponentModel.DataAnnotations.Editable the property is easy to implement for any attribute in the DTO that you want to opt out during the update process .

Businessservicebase will be covered separately servicebase.update and the Servicebase.updateasync , respectively in here and the here to provide its own implementation . These methods first query the data store for the current DTO representation, and scan the supplied DTO parameters to find Editable(false) any property, and then restores each property value to its current state in the data store.

The nullable foreign key property is 0 revert to null

in most databases, foreign key values cannot be zero because of foreign key constraints. Since you sometimes have no control over what your customers do when they work on your dto , assume that customers will inadvertently modify some data values for a DTO, That's a good idea.

Without pointing fingers, there are a lot of control kits, and these toolkits are often not very good at handling null values. These controls generally like to set a null value to 0, and when these executed DTOs reach your execution line and it is time to save the DTO in the data store, you only have foreign key exceptions to expect.

To prevent this from happening , you can use peasyforeignkeyattribute to apply a nullable foreign key property on your dto . applying this property to the nullable foreign key properties on a DTO will ensure that they always return to NULL when they contain 0 values values.

The empty foreign key Foreingkeyid can be used with the Peasyforeignkey property, so null is set for them when they contain 0 during the update.

Peasyforeignkey actually defines it.

Dealing with the concurrency problem of peasy.net learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.