tags:

views:

112

answers:

3

Hello,

At this time, I have this piece of code for my "employee" class. But I have almost the same for "customer" and all the others.

Is there a way to create an equivalent of my class "EmployeeRepository" but more something like this MyRepo<Employee> but implement IEmployeeRepository in this case, ICustomerRepository if I do this MyRepo<Customer>. Of course the get method return Employee, Customer or other ...

public class EmployeeRepository : NHRepository<Employee>, IEmployeeRepository
{
    public Employee Get(Guid EmployeeId)
    {
     return base.Get(EmployeeId);
    }
}

public interface IEmployeeRepository : IRepositoryActionActor<Employee>
{

}

public interface IRepositoryActionActor<T>
{
    T Get(Guid objId);
}
+1  A: 

You can - except you would make the IEmployeeRepository interface generic as well, so you would have:

public class MyRepo<U> : NHRepository<U>, IRepository<U>
{
...

}

public interface IRepository<T> : IRepositoryActionActor<T>
{

}

public interface IRepositoryActionActor<T>
{
    T Get(Guid objId);
}

Hope that helps :)

Russell
Is it possible in "public interface IRepository<T> : IRepositoryActionActor<T>" to add a method to implement depending of T value ?
Kris-I
+1  A: 

At this time, I have this piece of code for my "employee" class. But I have almost the same for "customer" and all the others.

Figure out what they have in common and give a name to that set of data and create an interface for it. My guess would be IPerson would probably work.

Then you can create a get person repository which returns an IPerson object and can either be an Employee or a Customer.

Spencer Ruport
IRepositoryActionActor is common for all (Employee, Customer, ....) but IEmployeeRepository (ICustomerRepository, ...) add some specific declaration
Kris-I
+2  A: 

Yes, like Spencer Ruport already mentioned, gather your code in an Interface or abstract base class as I did:

public interface IPerson
{
    void DoSomething( );
}

public abstract class Person : IPerson
{

    public virtual void DoSomething( )
    {
     throw new NotImplementedException( );
    }
}

public class Employee : Person
{
    public override void DoSomething( )
    {
     base.DoSomething( );
     /* Put additional code here */
    }
}

public class Customer : Person { }

public class PersonRepository<T> : System.Collections.Generic.List<T> where T : IPerson, new( )
{
    public T Get( Guid id )
    {
     IPerson person = new T( );
     return (T)person;
    }
}
PVitt
Interface needed, I use IoC
Kris-I
I can't edit but there is some syntax error in the code, if you can correct. But that's work. thanks again
Kris-I