Posted by: ayhan | 03/05/2016

Mapper kullanmadan iki sınıfı birbirine maplemek

Converting Business Entities Using Explicit Keyword

Use the explicit keyword to define the conversion of one business entity to another. The conversion method will be invoked when the conversion is requested in code. Here is the sample conversion code.

class Program
    {
        static void Main(string[] args)
        {
            ExternalEntity entity = new ExternalEntity()
            {
                Id = 1001,
                FirstName = "Dave",
                LastName = "Johnson"
            };
            MyEntity convertedEntity = (MyEntity)entity;
        }
    }
 
    class MyEntity
    {
        public int Id { get; set; }
        public string FullName { get; set; }
 
        public static explicit operator MyEntity(ExternalEntity externalEntity)
        {
            return new MyEntity()
            {
                Id = externalEntity.Id,
                FullName = externalEntity.FirstName + " " + externalEntity.LastName
            };
        }
    }
 
    class ExternalEntity
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: