Complex business Simplification

Source: Internet
Author: User

A small trick to simplifying a complex business in a complex system, when the business data is "deleted" (generally not allowed to delete the business data, but only for example), it needs to be judged according to other business data such as:  1. Generated out of stock list does not allow deletion of,  2. Payment slip confirmed that deletion is not allowed ,  3. The returned goods are not allowed to be deleted.   may be more complex in real business.        When this happens, there will be a lot of "delete" business judgments, and it will change frequently, and it is likely that the "delete" business needs to be re-adjusted when other business changes.   WORKAROUND: Add a middleware "can remove identities" for business data change this ID to false   other business modifications when this identity cannot be changed to true  Delete only based on this identity. You may manually modify the business identity (advanced actions)        This identity cannot be changed to true because it does require a lot of validation to be done, so you cannot modify the identity to true in order to avoid complexity.        such as: User demand "self-shop, you can modify the price, you can add gifts, do not deduct pre-deposit." Non-self-owned shop, can not change the price, can not add gifts, debit deposit "             The design of the program should not be" self-employed or non-self-employed "as the criteria for judging, and will be" whether to allow the price change, whether to allow the addition of gifts, Whether to deduct pre-deposit "as the criteria for judging the store (business data) to increase the corresponding control switch configuration.              This may be a good way to improve your program's scalability, and you can change your code without changing your business in the future. For example, "non-self-store may add gifts" when.        The control switch configuration for business data can be persisted using the Key/value form, which refuses to be associated with queries for business data.        NOTE: 1. If a large number of switch configurations are used in the system, the switch configuration is often modified, and concurrency needs to be considered, preventing multiple businesses from simultaneously modifying the same control development.                     2.This type of configuration will not be done as data retrieval criteria           above is the work of the record content, sharing the corresponding processing code, I hope to be useful to you     

<summary>
Data configuration Classes
</summary>
public class Smartconfig:baseentity
{
<summary>
Type
</summary>
[MaxLength (32)]
public string Type {get; set;}
<summary>
Type identification
</summary>
[MaxLength (32)]
public string Typeidentity {get; set;}
<summary>
Time cut
</summary>
[MaxLength (32)]
public string Timestamp {get; set;}
<summary>
Configuration
</summary>
public string Config {get; set;}
}

This is the entity class for the table, which is used for data storage.

<summary>
Business Data Configuration Classes
Do not directly new this object, use the Smartconfigservice.get method to get the object
</summary>
<typeparam name= "T" ></typeparam>
<remarks>2014-08-20 YWB Creation </remarks>
public class Smart<t>
{
<summary>
Do not directly new this object, use the Smartconfigservice.get method to get the object
</summary>
<param name= "Typeidentity" ></param>
<param name= "Timestamp" ></param>
<param name= "config" ></param>
Public Smart (String typeidentity, string timestamp, T config)
{
This. Timestamp = Timestamp;
This. Type = typeof (T). Name.replace ("C_", "" ");
This. Typeidentify = typeidentity;
This. Config = config;
}
<summary>
Business data types
</summary>
public string Type {get; private set;}
<summary>
Business Data identification
</summary>
public string Typeidentify {get; private set;}
<summary>
Time stamp
</summary>
public string Timestamp {get; private set;}
<summary>
Configuration
</summary>
Public T Config {get; private set;}
}

Business Data Configuration Classes

<summary>
Dish configuration
</summary>
<remarks>
Contract naming rules are prefixed with "c_" + "Business Table name"
This method is not very standard but easy to use, with vs smart tips to write code
</remarks>
public class C_dish
{
Public C_dish ()
{
To set the default value for a configuration item in a constructor
Needcooking = true;
Enabledprint = true;
Return = false;
Allowtocart = true;
}
<summary>
Chef Cooking Required
</summary>
[Description ("Requires chef's Cooking")]
public bool Needcooking {get; set;}
<summary>
Fallback when status is "done"
</summary>
[Description ("The status" is completed "can be retired")]
public bool Return {get; set;}

<summary>
Enable printing
</summary>
[Description ("Enable printing")]
public bool Enabledprint {get; set;}

<summary>
Allow customers to order meals
</summary>
[Description ("Allow customer to order")]
public bool Allowtocart {get; set;}

}

This is a specific business-owned configuration that sets the default value for the business data configuration item in the constructor

<summary>
Configure Service
</summary>
public static Class Smartconfigservice
{
<summary>
Update Sysmartconfig
If the return false is likely to be caused by concurrency, a business termination execution is required.
</summary>
<param name= "type" > Business type </param>
<param name= "typeidentity" > Business type Identification </param>
<param name= "config" > configuration </param>
<param name= "timestamp" > timestamp before update </param>
<param name= "Newtimestamp" > post-update timestamp </param>
<returns></returns>
public static bool Update (string type, string typeidentity, string config, string timestamp, string newtimestamp)
{

var flag = DB. Wdd.Database.ExecuteSqlCommand ("Update smartconfigs set Config={0},timestamp={1} where Type={2} and Typeidentity={3} and Timestamp={4} "
, config, Newtimestamp, type, typeidentity, timestamp) = = 1;
Cachememory.remove (String. Format (Cachekey.domain_smartconfig, type, typeidentity));
return flag;

}
<summary>
Get sysmartconfig if not in the database, create and return Sysmartconfig
</summary>
<param name= "type" > Business type </param>
<param name= "typeidentity" > Business type Identification </param>
<param name= "timestamp" > Timestamp </param>
<returns></returns>
public static Smartconfig Get (String type, string typeidentity, string timestamp)
{
var CacheKey = string. Format (Cachekey.domain_smartconfig, type, typeidentity);
Return cachememory.get<smartconfig> (CacheKey, () =
{
var config = DB. Wdd.SmartConfigs.FirstOrDefault (p = p.type = = Type && p.typeidentity = = typeidentity);
if (config! = null) return config;

Config = new Smartconfig ()
{
Config = string. Empty,
Timestamp = Timestamp,
Type = Type,
Typeidentity = typeidentity
};
Db. wdd.addentity<smartconfig> (config);
return config;
}, New TimeSpan (24, 0, 0));
}

<summary>
Save Business Data Configuration
If the return false is likely to be caused by concurrency, a business termination execution is required.
</summary>
<param name= "smart" > Business Data Configuration .</param>
public static bool Save<t> (smart<t> Smart)
{
if (smart = = null) throw new ArgumentNullException ("smart");
Return Update (smart. Type, Smart. Typeidentify, Newtonsoft.Json.JsonConvert.SerializeObject (smart. Config), Smart. Timestamp, Timestamp (DateTime.Now));
}

<summary>
Get Business Data configuration
</summary>
<param name= "typeidentity" > Business type Identification .</param>
public static smart<t> get<t> (String typeidentity)
{
var type = typeof (T). Name.replace ("C_", "" ");
var smartconfig = Get (Type, typeidentity, Timestamp (DateTime.Now));
if (string. IsNullOrEmpty (smartconfig.config)) Smartconfig.config = "{}";

Conversion error Exception not handled
var config = newtonsoft.json.jsonconvert.deserializeobject<t> (smartconfig.config);
return new Smart<t> (typeidentity, Smartconfig.timestamp, config);
}

<summary>
Gets the timestamp.
</summary>
<param name= "DT" > Time .</param>
<returns></returns>
private static string Timestamp (DateTime dt)
{
DateTime DT1 = new DateTime (1970, 1, 1);
TimeSpan ts = dt-dt1;
Return TS. Totalmilliseconds.tostring ();
}
}

This is the interface layer code implementation, developers only need to care about the following two methods

public static bool Save<t> (smart<t> Smart)

public static smart<t> get<t> (String typeidentity)

Other methods do not need to be concerned and can be changed to private.

The calling code is as follows:

            var dish = smartconfigservice.get<c_dish> ("1");            Dish. Config.needcooking = true;            Smartconfigservice.save (dish);

When you save it, you can tell if the return value is false, it may be because of concurrency and should end business processing

Complex business Simplification

Related Article

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.