开发者

Converting an existing instance of a class to a more concrete subclass

Situation: I have a large shrink wrapped application that my company bought. It is supposed to be extensible, yada, yada. It has a DB, DAL and BLL in the form of SQL and DLLs. It also has a MVC project (the extensible part) but 95% of the "Model" part is in the DAL/BLL libraries.

Problem: I need to extend one of the "Models" located in the BLL. It is an User object with 47 properties, 0 methods and no constructor. What I started was a simple deivation of their class like:

public class ExtendedUser : BLL.DTO.User
{
    public bool IsSeller { get; set; }
    public bool IsAdmin { get; set; }
}

This works fine if I just create a new ExtendedUser. However, it is populated by another call into their BLL like:

BLL.DTO.Use开发者_运维问答r targetUser = UserClient.GetUserByID(User.Identity.Name, id);

I tried the straight forward brute force attempt, which of course throws a Cast Exception:

ExtendedUser targetUser = (ExtendedUser)UserClient.GetUserByID(User.Identity.Name, id);

I am drawing a complete blank on this very simple OO concept. I don't want to write a Constructor that accepts the existing User object then copies each of the properties into my extended object. I know there is a right way to do this. Can someone slap me upside the head and tell me the obvious?

TIA


If you do want to use inheritance, then with 47 properties, something like Automapper might help you copy all the values across - http://automapper.codeplex.com/ - this would allow you to use:

// setup
Mapper.CreateMap<BLL.DTO.User, ExtendedUser>();

// use
ExtendedUser extended = Mapper.Map<BLL.DTO.User, ExtendedUser>(user);

Alternatively, you might be better off using aggregation instead of inheritance - e.g.

public class AggregatedUser
{
    public bool IsSeller { get; set; }
    public bool IsAdmin { get; set; }
    public BLL.DTO.User User { get; set; }
}


What about this approach (basically Aggregation):

public sealed class ExtendedUser  
{
    public ExtendedUser(BLL.DTO.User legacyUser)
    {
        this.LegacyUser = legacyUser;
    }

    public BLL.DTO.User LegacyUser 
    { 
        get; 
        private set; 
    }
} 


I don't want to write a Constructor that accepts the existing User object then copies each of the properties into my extended object.

This is typically the "right" way to do this, unless you have compile time access to the BLL. The problem is that a cast will never work- an ExtendedUser is a concrete type of User, but every User is not an ExtendedUser, which would be required for the cast to succeed.

You can handle this via aggregation (contain the instance of the User as a member), but not directly via inheritance.

This is often handled at compile time via Partial Classes. If the BLL is setup to create the classes (ie: User) as a partial class, you can add your own logic into a separate file, which prevents this from being an issue. This is common practice with many larger frameworks, ORMs, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜