Question

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>
 }
Was it helpful?

Solution

public class BarRepository : Repository<Bar>
{ 
    RepositoryInstructionResult Add(Bar item) 
    { //different implementation to Repository<T>.Add() } 
    //other methods inherit from Repository<T> 
} 

OTHER TIPS

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
    }
  }

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.

use an extension method :

public static void DoSomething(this repository<Bar> repo)
{
  //your custom code goes here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top