8

Here's the generic class I'm working with:

 public interface IRepository<T> where T : EntityObject
{
    RepositoryInstructionResult Add(T item);
    RepositoryInstructionResult Update(T item);
    RepositoryInstructionResult Delete(T item);
}
public class Repository<T> : IRepository<T> where T : EntityObject
{

    RepositoryInstructionResult Add(T item)
    { //implementation}
    RepositoryInstructionResult Update(T item);
    { //implementation}
    RepositoryInstructionResult Delete(T item);
    { //implementation}
 }

Now I'm looking to occasionally alter the behavior of the methods when t : a specific type. Is something like the following possible? This particular attempt gives an error (Error 5: Partial declarations of 'Repository' must have the same type parameter names in the same order).

public class Repository<Bar> //where Bar : EntityObject
{
    RepositoryInstructionResult Add(Bar item)
    { //different implementation to Repository<T>.Add() }
    //other methods inherit from Repository<T>
 }
2
  • 2
    BarRepository : Repository<Bar> ..., and mark the augmentible methods as virtual/override in the base/child classes, respectively. Jun 14, 2012 at 13:40
  • @Anthony: Why not post that as an answer? (Which I was about to post =P)
    – benjer3
    Jun 14, 2012 at 13:42

4 Answers 4

9
public class BarRepository : Repository<Bar>
{ 
    RepositoryInstructionResult Add(Bar item) 
    { //different implementation to Repository<T>.Add() } 
    //other methods inherit from Repository<T> 
} 
5
  • 1
    My objection to this solution is that you need to instanciate new BarRepository() explicitly. If you call new Repository<Bar>(), you get the generic (ie wrong) implementation. Since Repositories are already being created and consumed accross my codebase, I'd rather not have to hide the instanciation in a factory. Jun 14, 2012 at 14:10
  • This code would obviously need methods to be exposed as public in order to fulfill the interface and need virtual/override modifiers in the base/child in order to use the behaviors polymorphically. Jun 14, 2012 at 14:17
  • @daveharnett, to best use the repository pattern, the code or logic that is depending upon the repository would be coding to the interface instead of the implementation. That code, then, would be looking at IRepository<Bar>, and thus would not need to change. Although you are correct, any code in charge of creating the concrete implementations would need to be updated. Jun 14, 2012 at 14:18
  • 1
    @daveharnett Short of littering Repository<T> with hardcoded type checks in the methods you wish to 'override' I'm not sure there's another way around it. A repository factory is the correct way to handle this situation and the rest of the code should only ever care about IRepository<T>, giving you the flexability to modify your implementations by type at will.
    – roken
    Jun 14, 2012 at 14:20
  • @Anthony Unless somebody swoops in with a magic solution, this is looking like the way to go. Fortunately there are only 3/4 areas of code handling creation (a sort of lazy instanciation pattern), so if I point them at a factory the change shouldn't be too painful. Thanks folks! Jun 14, 2012 at 14:33
2

Name your Repository class RepositoryBase and make interface methods virtual. implement them in e general way inside your RepositoryBase class, but because u marked methods as virtual u will be able to override functionality in your derived classes your code will look something like this.

 public interface IRepository<T> where T : EntityObject
 {
    RepositoryInstructionResult Add(T item);
    RepositoryInstructionResult Update(T item);
    RepositoryInstructionResult Delete(T item);
 }

 public class Repository<T> : IRepository<T> where T : EntityObject
 {
    virtual RepositoryInstructionResult Add(T item)
    { //implementation}
    virtual RepositoryInstructionResult Update(T item);
    { //implementation}
    virtual RepositoryInstructionResult Delete(T item);
    { //implementation}
  }

If U Need some custom logic to be executed for update method for Bar Object simply create derived class Name it BarRepository and override update method of the Repositorybase class here u can either call base implementation or just process with its' own logic

 public class BarRepository : Repositorybase<Bar>
 {
    public override RepositoryInstructionResult Update(Bar item);
    {
       //Call base method if needed
       //Base.Update(item);

       //implement your custom logic here
    }
  }
3
  • Ditto my reply to roken. It will get the job done, but it will make consumption more difficult. Jun 14, 2012 at 14:19
  • I meant that you need to instanciate new BarRepository() explicitly. If you call new Repository<Bar>(), you get the generic (ie wrong) implementation. Ideally I was looking for a solution that would be hidden from the repository creation code. Jun 15, 2012 at 10:33
  • u do not have to do this manually u could bring in same naming convention so that those repositories can be created automatically. U would have to write this instantiation logic once using Cativator.CreateInstance. there are lots of ways to achieve what to try to do, but this conversation could turn in to huge framework development so I am leaving decisions up to u
    – Rati_Ge
    Jun 15, 2012 at 11:06
0

As a direct answer to your question: The closest possible thing to what you've shown is checking the actual value of T at runtime. In your add method, you can write something like this:

if (typeof(T) == typeof(Bar)) {
    // your Bar-specific code
}
// ...

Note that this may not be very good in terms of performance, in particular if you have more than one or two such special types that you want to treat differently.

Other than that, the only solution is a subclass that specifies the actual type argument for the base class, as outlined in the other answers.

2
  • Thanks. Performance aside, this would be very messy if there are more than a few special cases. Jun 14, 2012 at 14:27
  • 1
    @daveharnett: Not necessarily; in that case, you might want to use a Dictionary<Type, Handler> of Handler objects (a class of your own) that execute type-specific actions. Like this, it's basically just three to four lines of code (possibly less with an extension method) in each method that needs special treatment. Jun 14, 2012 at 14:32
0

use an extension method :

public static void DoSomething(this repository<Bar> repo)
{
  //your custom code goes here
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.