C # practical micro-framework: FakeDataTool ool (Version 1)

Source: Internet
Author: User

Recently, the company has received many small projects from Microsoft, resulting in insufficient manpower, especially serious disconnections between the database backend and the front-end. Some customers have been worried, an emergency meeting was held yesterday afternoon between the boss and our teamLeader,
Discuss the way to deal. Because the efficiency of the front-end is higher than that of the back-end (the number of backend workers is almost the same, many interns or new employees), before the next week's background work, I proposed to my boss the "fakeDataTool ool"
The idea is a micro-framework that creates temporary false data and can solve the pressing problem. The boss was a great architect, so he readily accepts this idea, so the first version of a micro-framework was born at this morning...
Functions completed in this version include:
1: Create a class Object and assign values to the attributes (only the first version has implemented class, string, guid, int, int ?, Double, double, IEnumerable, IDictionary)
2: All the parent classes of this class recursively create objects and assign values (strictly speaking, until the object type is reached)
3: Assign the configuration to the programmer (user) and the framework to implement the above functions (the first version only produces the single object and set object (IEnumerable) functions)
4: Give the programmer (specified) the Attribute range, tentatively implement with Attribute (for example, the int type is a random number between 0 and 100, and the string type is a random string in a string array)
5: The set IExporter interface can implement various output functions (xml, json, words) in a few days)
6: A series of bugs need to be changed: Reference yourself, reference other classes, and have your own classes ..... (Will cause overflow) and so on
First read this Code:
  
Static void Main (string [] args)
{
FakeDataConfig config = new FakeDataConfig
{
ConfigName = "StudentConfig ",
FakeDataCount = 3,
GetFakeType = FakeDataType. IEnumerableData
};
FakeDataTool tool = new FakeDataTool ool (config );
Var data = tool. CreateFakeEnumrableData <Student> ();
}

In fact, this tool is very simple to use, declare a config, enter the type you want to create, create the number of false data, and so on, and then create a series of false data through the tool.
Everyone who writes a framework that is too small knows that sometimes too much encapsulation is not necessarily a good thing, so after some ideological struggles, I decided to expose the FakeDataTool ool method.
Next, the object class looks like:
1 public class Human
2 {
3 [FakeData (new string [] {"aaa", "bbb", "ccc"})]
4 public string Gender {get; set ;}
5
6}
7
8 public class Man: Human
9 {
10 public Guid ID {get; set ;}
11 [FakeData (2, "Man")]
12 public List <int> HumanProperty {get; set ;}
13 [FakeData (2, "man")]
14 public Dictionary <int, int> HumanList {get; set ;}
15}
16
17 public class Student: Man
18 {
19 [FakeData (new string [] {"aaa", "bbb", "ccc"})]
20 public string Name {get; set ;}
21 [FakeData (100)]
22 public int Age {get; set ;}
23 [FakeData (2, "Student")]
24 public Dictionary <int, Human> HumanList {get; set ;}
25
26}

You may understand at a glance that the FakeData tag is used to specify the first layer of the class (what is the first layer? Here, we must make it clear that the first layer here refers to a class (including the value levels of non-set attributes in all its parent classes ))
Smart, you have already reflected it. In the student class, IDictionary <int, Huaman> here, the manufacturing values of the two generic parameters are no longer the first layer.
1. fakeData (new string []...) this applies only to properties of the string type. The string array can be defined at will. fakeDataTool assigns the elements in these arrays to the attribute of the tag at the first layer.
2. FakeData (int) is the data range for integer data. The default value ranges from 0 to the value on the tag.
3. FakeData (2, "Student") indicates the number of objects created in the collection in the set attribute.
 
You can see the result:




 

You may wonder why the object Value in humanList is like this. Why am I not tagged on the Human class? Back to the previous topic: the second layer or even higher layers become quite complex. Due to the time, I can only place the optimization in later versions.

This method helps you understand complexity: IDictionary <int, IDictionary <int, List <IDictionary ........

 

Next we will explain how to change the micro-framework architecture:




 

Top-down:
1 Exporter module: outputs the final fake data to a specific file (which will be implemented in later versions)
1 public enum ExportFileType {XMLExport, JSon, Txt, Word}
2
3 interface IExporter
4 {
5 void ExportToFile ();
6}
7
8 public abstract class ExporterBase: IExporter
9 {
10 public ExportFileType FileType {get; set ;}
11 public abstract void ExportToFile ();
12}

 
2. FakeDataAttribute, FakeDataHelper, and FakeDataException
1 public class FakeDataAttribute: Attribute
2 {
3 public int FakeRange {get; set ;}
4 public string [] FakeStringData {get; set ;}
5 public int FakeSubClassOrTypeCount {get; set ;}
6
7 public FakeDataAttribute ():
8 this (null)
9 {
10
11}
12
13 public FakeDataAttribute (int fakeRange = 100)
14 {
15 this. FakeRange = fakeRange;
16
17}
18
19 public FakeDataAttribute (string [] fakeStringData)
20 {
21 this. FakeStringData = fakeStringData;
22}
23
24 public FakeDataAttribute (int fakeSubClassCount, string subTypeName = null)
25 {
26 this. FakeSubClassOrTypeCount = fakeSubClassCount;
27}
28
29}

 
 
3. The lower part is whether the user inputs a command (create a fake object or multiple objects)
Command-> Command Reciver uses the user's config to create the objects required by the user. After the manufacturing is complete, the Reciver will hand over the objects to the user,
Reciver uses Abstract Factory manufacturing data www.2cto.com
1 class CreateObjectCommand <T>: CommandBase <T> where T: class
2 {
3 private object newObject;
4 public object NewObject
5 {
6 get {return this. newObject ;}
7}
8
9 public CreateObjectCommand (CreateObjectReciver <T> recever)
10: base (recever)
11 {
12}
13
14 public override void ExcuteThisCommand ()
15 {
16 var createObjectReciver = this. Recever as CreateObjectReciver <T>;
17 var config = createObjectReciver. config as FakeDataConfig;
18 if (config. GetFakeType = FakeDataType. singleDataOnly)
19 {
20 this. newObject = createObjectReciver. CreateObject ();
21}
22 else if (config. GetFakeType = FakeDataType. IEnumerableData)
23 {
24 this. newObject = createObjectReciver. CreatEnumerableData ();
25}
26}
27}

 

The above is the basic situation of this insignificant small framework. I will upload the code later. If there are bugs or better suggestions, please note that, hope you can learn and make progress together

 

From anti-clockwise alert

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.