The extensions methods concept is a way to extend the
functionality of any class or data type, by adding new methods to that class or
data type and calling these methods directly through the instance of the class
or the data type.
There are 2 important notes regarding to the extension methods:
There are 2 important notes regarding to the extension methods:
- You cannot define an extension property, field, or event.
- Extension methods cannot be used to override existing methods.
Structure of Extension Methods in .NET :
- An extension method must be marked with the extension attribute <Extension()> from the System.Runtime.CompilerServices namespace.
- An extension method can be either a sub or a function.
- An extension method must include at least one argument with which it must be identical to the type of the class that it will be extending. (I.e. if extending a String method, then the first parameter must be of type String).
- An extension method must be declared within a Module. For ASP.NET , you can add a class and then change its definition to Public Module , instead of Public Class.
Open Source Libraries for .NET Extensions
There is a wide variety of hundredth of ready-made useful .net extensions provided by the community of .NET through CodePlex.
You can start using them today, just add a reference to the library from http://dnpextensions.codeplex.com/
Or even you can get this library from NuGet http://nuget.org/packages/PGK.Extensions/ and immediately start using it without any "import" statements.
Extension Method Sample
Below is a sample extension method that can be used to get the maximum length of a given EDMX EntityObject's field, as defined by the provider source ( i.e. SQL Server) , so if there is a field named 'FirstName' and defined in the database as Varchar(50) , then this extension method will return the number 50.
Imports System.Runtime.CompilerServices
Imports System.Data.Objects.DataClasses
Imports System.Data.Metadata.Edm
Module EntityFrameworkExtensionMethods
<Extension()>
Public Function GetMaxLength(EntityObject As EntityObject, PropertyName As String, DB As DB_DAO) As Integer
Dim queryResult = From meta In DB.MetadataWorkspace.GetItems(DataSpace.CSpace).Where(Function(m) m.BuiltInTypeKind = BuiltInTypeKind.EntityType)
From p In TryCast(meta, EntityType).Properties.Where(Function(p) p.DeclaringType.Name = EntityObject.[GetType]().Name AndAlso p.Name = PropertyName AndAlsop.TypeUsage.EdmType.Name = "String")
Select p.TypeUsage.Facets("MaxLength").Value
If queryResult.Count() > 0 Then
Return Convert.ToInt32(queryResult.First())
End If
Return 0
End Function
End Module
Then you can call this method normally , as it is a part of the instance EntityObject:
Dim _EmployeeRecord As New EmployeeRecord
Dim _FirstNameMaxLength As Integer = EmployeeRecord.GetMaxLength("FirstName", _DBContext)
Note that even though the Extension method has 3 parameters defined, we still call the method using 2 arguments. This is due the fact that, as mentioned above, the first parameter defined in the extension method is the type that has to be identical to the class or datatype, that is being extended.
No comments:
Post a Comment