Data Annotation
Data Annotations
Data Annotations are nothing but certain validations that we put in our models to validate the input from the user. ASP.NET MVC provides a unique feature in which we can validate the models using the Data Annotation attribute. Import the following namespace to use data annotations in the application.
System.ComponentModel.DataAnnotations
It is very easy to use and the code becomes much cleaner as compared to normal ASP.NET validators.
Types of Data Annotations in ASP.NET MVC
- Required
This attribute specifies that the value is mandatory and cannot be skipped.
Syntax
[Required(ErrorMessage=”Please enter name”),MaxLength(30)]
- Datatype
This attribute is used to specify the datatype of the model.
Syntax
[DataType(DataType.Text)]
- Range
Using this attribute we can set a range between two numbers.
Syntax
[Range(100,500,ErrorMessage=”Please enter correct value”)]
- StringLength
Using this attribute we can specify maximum and minimum length of the property.
Syntax
[StringLength(30,ErrorMessage=”Do not enter more than 30 characters”)]
- DisplayName
Using this attribute we can specify property name to be displayed on view.
Syntax
[Display(Name=”Student Name”)]
- MaxLength
Using this attribute we can specify maximum length of property.
Syntax
[MaxLength(3)]
- Bind
This attribute specify fields to include or exclude for model binding.
Syntax
[Bind(Exclude = “StudentID”)]
- DisplayFormat
This attribute allows us to set date in the format specified as per the attribute.
Syntax
[DisplayFormat(DataFormatString = “{0:dd.MM.yyyy}”)]
- RegularExpression
We can set a regex pattern for the property. For ex: Email ID.
Syntax
[RegularExpression(@”^\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$”, ErrorMessage = “Email is not valid.”)]
3 Layered Architecture
A fundamental rule in a three tier architecture is the client tier never communicates directly with the data tier; in a three-tier model all communication must pass through the middle tier.
- Business Logic Layer
A BAL contains business logic, validations or calculations related to the data. Though a web site could talk to the data access layer directly, it usually goes through another layer called the Business Layer.
- The Business Layer is vital in that it validates the input conditions before calling a method from the data layer.
- This ensures the data input is correct before proceeding, and can often ensure that the outputs are correct as well. This validation of input is called business rules, meaning the rules that the Business Layer uses to make “judgments” about the data.
Data Access Layer
A DAL contains methods that helps the Business Layer to connect the data and perform required actions, whether to return data or to manipulate data (insert, update, delete and so on).
- In constructor of this class assign the connection string to the private string variable. That the can only access in this class
- DAL fetch data from database by using SQL Helper Class and return to the BLL.
& Presentation layer
SQL Helper class
SQL Helper class is used in Data Access Layer which interacts with database with the help of connection string provided and it contains several methods like below. And, it improves the performance for Business Layer & Data Access Layer.
- ExecuteNonQuery
- ExecuteDataset
- ExecuteDataTable
- ExecuteReader
- ExcuteScalar & more