0

I am new to Repository Pattern and trying to use it in my project. I have following entities in my project:

  • UserProfile
  • UserTypeA
  • UserTypeB
  • UserTypeC

I have 3 types of users and UserProfile containing the general information about all three of them like Password, UserName etc. There are many other entities also and each type of user have different type of relationship with other entities. Now i have decided to create the following repository pattern for this:

public class UserProfileRepository
{
 ....
}

public class UserTypeAReository: UserProfileRepository
{
 .....
}

public class UserTypeBReository: UserProfileRepository
{
 .....
}

public class UserTypeCReository: UserProfileRepository
{
 .....
}

So i want to know is this (repository inheritance) comes under good practices OR is there any other better way to do this ??

EDIT

There is a 1 to 0..1 shared primary key relationship b/w UserProfile and (UserTypeA, UserTypeB and UserTypeC).

Gaurav
  • 8,367
  • 14
  • 55
  • 90
  • Yes, this is fine. Especially since you store the properties of UserProfile in a seperate table, that keeps things DRY. Altough it's probably better to ask this at http://codereview.stackexchange.com/ Note: There's no need to specify "Repository" in every class name, you should only use patterns where they are usefull, dont lock yourself into one. – MrFox Jan 27 '14 at 12:42
  • If your user types are disjunkt, i'd go for inheritance and let ef handle everything neccessary. If not your Approach seems fine. – TGlatzer Jan 27 '14 at 12:42
  • check out this too http://stackoverflow.com/questions/5809316/repository-pattern-and-inheritance-in-net – Arun Chandran Chackachattil Jan 27 '14 at 12:46

1 Answers1

0

Depending on what you're trying to achieve, you can also use an Interface or an Abstract Class.

If you want all the classes to have a common contract, but the implementation of each method is different, then you can use an Interface, and this will give more flexibility since you have multiple interface inheritance v's singe class inheritance with C#.

With the Abstract class, you can set up the methods, along with an implementation, but allow the derived class to override the base implementation.

This SO post might also help: Interface vs Base class

Community
  • 1
  • 1
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82