Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3240610
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:06:18+00:00 2026-05-17T18:06:18+00:00

Hey guys, It’s 5 am here, I was doing some code… when this came

  • 0

Hey guys, It’s 5 am here, I was doing some code… when this came out:

public interface IViewModel { }

public interface IToViewModel<T> where T : IViewModel { }

public static class MvcExtensions
{
    public static T ToViewModel<T>(this IToViewModel<T> self)
        where T : IViewModel
    {
        var instance = Activator.CreateInstance<T>();

        //this line just copys the properties with the same name from one object to another
        self.Reflected().CopyMatchingPropertiesTo(instance /*destiny*/);

        return instance;
    }

}

My point with this code is do stuff like that:

public partial class UserInfo : IToViewModel<UserInfoViewModel> { }

public class UserInfoViewModel : IViewModel
{
    [DisplayName("UserId")]
    public Guid UserId { get; set; }

    [DisplayName("User name")]
    public string Username { get; set; }

    [DisplayName("Email address")]
    public string Email { get; set; }
}

public ActionResult Index(string username)
{
    UserInfo userInfo = UserInfoHelper.Load(username);

    UserInfoViewModel userInfoViewModel = userInfo.ToViewModel();            

    return View(userInfoViewModel);
}

Oks, this code runs ok, np.

But my question is about the uses of the interfaces…

My goal is convert a EntityFramework object into a ViewModel to use with ASP MVC views
without doing to much parameters passing, i found in the interfaces a way to say what is each of the objects are, what they perform.

What looks weird to me is these interfaces with no method inside.

So what you guys think about it?
Is there somewhere some cool blogpost/doc/etc about it?
Do u have any idea how do it better?
Should I go bed right now?

Other thing, .Net is bringing a lot of new ways of doing things… like linq, lambda, extension methods, expression trees… is there any study about “good practices” with these things?

I think that is huge, and makes your brain think different in the time that you are solving the problem… I would like to see what people are thinking/doing with that.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:06:19+00:00Added an answer on May 17, 2026 at 6:06 pm

    I guess it could be a question of marker interfaces vs. functional interfaces. I’m not usually a fan of marker interfaces, but I can see why you would want to use it this way.

    Personally, I think you should have an overloaded constructor:

    public class UserViewModel
    {
      // Constructor used in model binding.
      public UserViewModel() { }
    
      // Constructor used for mapping.
      public UserViewModel(UserEntity entity)
      {
    
      }
    }
    

    I have this preference because it cleanly defines a dependancy I need. I know you lose some of the fidelity of applying it to any types, but I think its cleaner.

    The other alternative is to go the AutoMapper route. With AutoMapper you can easily map between one type and another…e.g.

    Mapper.CreateMap<UserEntity, UserViewModel>();
    
    public static TTarget Map<TSource, TTarget>(this TSource instance)
    {
      return Mapper.Map<TSource, TTarget>(instance);
    }
    
    var user = DataContext.Users.Where(u => u.Username == "Matt").Single();
    return user.Map<UserViewModel>();
    

    Now, you have to create a specific mapping ahead of time, but then your extension method can be used on any types that you have created a mapping for. The default behaviour of AutoMapper is to map between properties that match, but you can get specific and map properties specific ways as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey right now I'm using jQuery and I have some global variables to hold
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey, I've been developing an application in the windows console with Java, and want
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey peoples, I've been studying Java for a couple of weeks, and have decided

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.