1 00:00:07,650 --> 00:00:08,160 Hello. 2 00:00:08,580 --> 00:00:12,210 In this video, we're going to talk about Question 17. 3 00:00:12,450 --> 00:00:17,160 What are attributes, attributes of metadata to a type? 4 00:00:17,580 --> 00:00:24,690 In other words, there are a way to add information about a type or method to the existing metadata, 5 00:00:24,690 --> 00:00:29,670 which describes that type or method, which we can read from the type object. 6 00:00:29,970 --> 00:00:34,950 First, let's understand what metadata is, generally speaking. 7 00:00:34,980 --> 00:00:39,690 Metadata is data providing information about other data. 8 00:00:40,050 --> 00:00:47,190 For example, when working with databases, the data stored inside the database is the actual data. 9 00:00:47,340 --> 00:00:54,780 While the structure of tables and relations between them is metadata in programming, metadata describes 10 00:00:54,780 --> 00:00:57,000 types used in the application. 11 00:00:57,570 --> 00:01:01,020 First, let's consider this simple person class. 12 00:01:01,560 --> 00:01:04,650 There is a lot of metadata describing this class. 13 00:01:05,010 --> 00:01:10,590 For example, the metadata contains the information that this class is named person. 14 00:01:10,830 --> 00:01:18,900 It is public, not static, non-zero, etc. It contains too good only public properties called name 15 00:01:18,900 --> 00:01:20,040 and year of birth. 16 00:01:20,220 --> 00:01:26,100 It has one public constructor taking two three parameters and one taking one parameter. 17 00:01:26,490 --> 00:01:33,420 The actual data stored in an instance of this class would be the string representing the name and int 18 00:01:33,570 --> 00:01:35,310 representing the year of birth. 19 00:01:35,850 --> 00:01:39,650 We can access all the classes metadata using reflection. 20 00:01:39,690 --> 00:01:42,060 What we learned about in the previous lecture. 21 00:01:42,450 --> 00:01:49,080 Sometimes we want to add extra metadata to a type or a method, and this is what attributes our form. 22 00:01:49,710 --> 00:01:57,090 Let's consider the following example You want to have a common way of validating some data in the application. 23 00:01:57,540 --> 00:02:04,050 No matter the type, we want to be able to specify that some members, for example, 3GHz, must have 24 00:02:04,050 --> 00:02:04,980 certain length. 25 00:02:05,400 --> 00:02:06,630 This is what they want. 26 00:02:12,250 --> 00:02:19,480 I want to create some validating grass that will be able to take objects off any grass and took it for 27 00:02:19,480 --> 00:02:20,860 any of their properties. 28 00:02:21,070 --> 00:02:22,810 This validation is required. 29 00:02:23,050 --> 00:02:27,590 If so, it should check if the values of those properties are valued. 30 00:02:28,250 --> 00:02:33,750 First, let's write how we would like to use the validator and then we'll implement it. 31 00:02:59,280 --> 00:02:59,920 All right. 32 00:03:00,330 --> 00:03:07,830 So what we want to do is to add some more metadata, the name properties in both person and dog types, 33 00:03:08,070 --> 00:03:10,920 we found there are minimal and maximal lengths. 34 00:03:11,340 --> 00:03:16,920 This is some extra metadata and to define it, we must use a custom attribute. 35 00:03:17,430 --> 00:03:19,440 This is how I would like to use it. 36 00:03:27,610 --> 00:03:34,600 To add an attribute to a member or type, we simply must write its name in the brackets above the type 37 00:03:34,600 --> 00:03:36,730 or member who wants to add it to. 38 00:03:37,180 --> 00:03:41,170 As you can see, this attribute will require two parameters. 39 00:03:41,470 --> 00:03:43,540 Minimal and maximal length. 40 00:03:44,060 --> 00:03:48,130 Now let's define the string length validate attribute graphs. 41 00:03:54,360 --> 00:04:01,260 All attributes must derive from the attribute the base class also its name should earn with attribute. 42 00:04:01,500 --> 00:04:06,600 As you see here, this postfix is omitted when we actually use the attribute. 43 00:04:11,380 --> 00:04:12,580 Well, so that's it. 44 00:04:12,910 --> 00:04:19,570 These attribute must only hold the metadata about the minimal and maximal length of this -- property. 45 00:04:19,930 --> 00:04:25,150 One more thing, though, we can also define what the attribute can be applied to. 46 00:04:25,510 --> 00:04:28,900 In our case, we want it to be applied to properties. 47 00:04:29,230 --> 00:04:34,840 To enforce that, we must actually use a built in attribute called attribute usage. 48 00:04:39,140 --> 00:04:39,760 All right. 49 00:04:40,460 --> 00:04:44,300 We added this metadata to the person and dog process. 50 00:04:44,690 --> 00:04:47,510 Now you must implement the validator across. 51 00:04:55,930 --> 00:05:03,220 This grass simply contains validated method, which can take an object for this object will look for 52 00:05:03,220 --> 00:05:04,320 its properties. 53 00:05:04,360 --> 00:05:07,420 Will the three grant validate attributes defined? 54 00:05:26,810 --> 00:05:34,010 Here we select those properties of this type for which this attribute is defined using the links were 55 00:05:34,010 --> 00:05:37,490 matured along with attribute is defined method. 56 00:05:37,850 --> 00:05:43,970 Now we can iterate those properties and for values, check their legs are correct. 57 00:05:44,210 --> 00:05:47,630 But first, we must make sure that the property is a string. 58 00:05:47,900 --> 00:05:53,870 If not, we want to throw an exception because it means that a developer others this attribute to a 59 00:05:53,870 --> 00:05:55,610 different type by mistake. 60 00:06:18,560 --> 00:06:23,600 Now, when we are sure that the value of the property is string, we can validate it. 61 00:07:03,260 --> 00:07:03,890 All right. 62 00:07:04,190 --> 00:07:05,390 Let's see what happens here. 63 00:07:05,960 --> 00:07:09,500 First of all, I've cast the property value to string. 64 00:07:09,890 --> 00:07:12,680 I know it's safe because I already checked it here. 65 00:07:13,280 --> 00:07:21,110 Then I needed to access the attribute object to do so, I called the Get Custom Attributes metric on 66 00:07:21,110 --> 00:07:21,920 the property. 67 00:07:22,400 --> 00:07:28,760 It takes the type of the attribute and a Boolean saying if you should also check the attributes that 68 00:07:28,760 --> 00:07:30,890 have been inherited from the base type. 69 00:07:31,190 --> 00:07:34,910 This returns a correction and they took the first element from it. 70 00:07:35,300 --> 00:07:41,240 Here happens the actual validation and based on it, I returned either false or true. 71 00:07:41,990 --> 00:07:44,360 All right, let's make sure it works. 72 00:07:46,370 --> 00:07:47,750 The result seems correct. 73 00:07:48,110 --> 00:07:54,530 The person is valid, but the dog isn't because the property name has only one letter. 74 00:07:55,850 --> 00:07:58,760 As you can see, attributes can be quite powerful. 75 00:07:59,270 --> 00:08:04,430 They are widely used in native grasses as well as external libraries. 76 00:08:04,880 --> 00:08:10,730 For example, if you ever used and you need, you must have used some of its attributes. 77 00:08:11,630 --> 00:08:15,830 Let's summarize attributes at metadata to a type. 78 00:08:16,190 --> 00:08:22,820 In other words, they are a way to have information about a type or some of its members to the metadata 79 00:08:22,820 --> 00:08:27,200 which describes it to add attributes to our member or type. 80 00:08:27,560 --> 00:08:32,510 We must simply write its name in the brackets above the thing we want to add it to. 81 00:08:33,110 --> 00:08:39,080 There are plenty of built in attributes in such a standard library, but we can also create attributes 82 00:08:39,080 --> 00:08:39,890 of our own. 83 00:08:40,220 --> 00:08:47,450 Simply by creating classes derived from the attribute based grass during the interview, you might be 84 00:08:47,450 --> 00:08:53,240 asked what this method they call metadata is data describing of our data. 85 00:08:53,600 --> 00:09:01,130 Like with databases, the metadata is the structure of tables and relations between them in programming. 86 00:09:01,280 --> 00:09:04,730 Metadata describes types used in the application. 87 00:09:05,150 --> 00:09:08,630 Who can access it by using reflection and the this way? 88 00:09:08,660 --> 00:09:15,350 Good information about some type, for example, what methods or what constructors it contains. 89 00:09:16,040 --> 00:09:23,390 How to define a custom attribute simply by defining a class derived from that attribute based class. 90 00:09:24,080 --> 00:09:26,360 All right, that's it for this lecture. 91 00:09:26,570 --> 00:09:29,270 Thanks for watching, and I'll see you in the next one.