开发者

How do you setup your domain model?

We are about to create a new site. The site is quite user-centric. We show up users along with different sort of information.

Now we are about to decide how to model this and we are thinking about some options.

First I want to describe some common scenarios. 1) Sometimes we want to show username, city and picture for the current user 2) Sometimes we want to show all userinformation 3) Sometimes we want to show only the username 4) Sometimes we want to show username and city

Each scenario is related to one or many pages on the web site. For example our page with forumposts might show information from scenario 1, our search might show information from scenario 4.

This can be solved in many ways and I would like to hear from you what you think is the best way.

I take up some solutions with pros/cons.

First, multiple classes. Create a base class from user which has all common properties. And then extend from there. For example.


public class BaseUser
{
    public string username;
}

public class User : BaseUser
{
    public string city;
}

public class UserWithPicture : User
{
    public string picture;
}

public class CompleteUser : UserWithPicture
{
    public string SomeProperty;
}

Pros: Only need to fill exactly what you need at the time, which makes it scaleable from a performance point of view. Cons: Difficult to maintain whenever you need to change something in the future. Will be many dependencies.

Another solution could be to always have everything in one class and use that all the time.

public 开发者_运维技巧class User
{
    public string username;
    public string city;
    public string picture;
    public string SomeProperty;
}

Pros: You always have all information about the user Cons: Where do you draw the line? Can we put OrderLines and UserContacts in here? Everything is dependent on this one class, which makes it hard to maintain if you need to change something.

Another solution would be to create a unique context for nearly each page

public class ForumPost
{
    public string data;
    public string topic;
    public string username;
    public string usercity;
    public string userpicture;
}

public class SearchPost { public string searchdata; public string username; public string usercity; }

Pros: Not dependent on anything else, and only need to fill the necessary information Cons: Slower development, and if you want one new property (say user age) for all pages, it would require alot of changes.

Suggestions?


All of the scenarios you mention have very little to do with the domain model:

1) Sometimes we want to show username, city and picture for the current user 2) Sometimes we want to show all userinformation 3) Sometimes we want to show only the username 4) Sometimes we want to show username and city

What you want to show is only of concern in the presentation layer. When designing the data model, forget about what you want to show, and think about the best way to represent your business entities.


I would go for your middle option - one class with all attributes.

The first approach via inheritance looks like a nightmare to maintain/use.

The last approach similarly looks bad - lots of duplication.

As for OrderLines and UserContacts, I would separate these into their own class - a one to many from the User one.

~chris

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜