1

i wasn't able to find a similar issue but feel free to redirect me if i just missed it. I am trying to get familiar with the Repository pattern.

I'll give you an example of the code i'm trying to get to work unsuccessfully. These are the classes and interfaces that represent the entity i'm using.

public class AbsObj
{
    public string Code { get; set; }
}

public interface IAbsObj
{
    bool Save();
}

public class User : AbsObj
{
    public string Language{get; set;}
}

public class DbUser : User, IAbsObj
{
    public bool Save()
    {
        return true;
    }
}

Then to the repository Interface

public interface IRepository<T>
{
    void Add(T value);
    void Update(T value);
    void Delete(T value);
}

The generic Repository

public class Repository<T> : IRepository<T> where T : AbsObj, IAbsObj
{

    protected List<T> _lst;

    public Repository()
    {
        _lst = new List<T>();
    }
    public void Add(T value)
    {

    }
    public void Update(T value)
    {

    }
    public void Delete(T value)
    {

    }        
    public bool Save()
    {
        for (int i = 0; i < _lst.Count; i++)
        {
            _lst[i].Save();
        }
        return true;
    }
}

Then a more specific repository, which should handle the loading of the users from the db:

public class UserRepository<T> : Repository<T> where T : AbsObj, IAbsObj
{
    public void Load()
    {
        DbUser us = new DbUser();
        us.Code = "Cod";
        us.Language = "IT";
        _lst.Add(us);
    }
}

I created the DBUser class just to have the freedom to create an XMLUser in the future which would handle a different type of saving. It inherits from User which in turn inherits from AbsObj. It implements IAbsObj. Nonetheless i got a compile time error when i try to add to the list the DbUser object created, stating that it's impossible to convert from DBUser to T. Given the constraints i tought it was possible: what am i missing here? Thanks in advance for any help!

Alberto
  • 11
  • 1
  • you have declared protected List _lst; and trying to add DbUser on Load() to that list with out specifying should n't you can only do that when var repo = new UserRepository(); repo.load(); – Dipon Roy Apr 16 '15 at 13:56
  • Yes you're right! How dumb of me! How can i give you the credit for the correct answer in a comment? – Alberto Apr 17 '15 at 06:17

1 Answers1

0

Your UserRepository definition could be:

public class UserRepository : Repository<DbUser>
{
  ....
}

But since you want to make it generic for XMLUser as well:

public class UserRepository<T> : Repository<T> where T: User, new()
{

   public void Load()
   {
      User us = new T() as User;
      us.Code = "Cod";
      us.Language = "IT";
      _lst.Add(us);
   }
 }

To use:

    new UserRepostitory<DbUser>();
    new UserRepostitory<XmlUser>();
  • Sorry but it didn't work:(. I got a compile time error since User does not implement IAbsObj and therefore doesn't have the Save() method required to be in the Repository. Another note: if i create a User and not a DbUser during Load() i won't be able to save for the same reason. NB: Of course i'm talking about the second solution. I was interested to understand why it's giving me the error despite the constraints. – Alberto Apr 16 '15 at 13:46
  • You should move the IAbsObj from DbUser to User class. My example is not creating a User, if you look at the Load implementation it is creating a new instance of the generic type T. T is specified what should be created in my example how to use it, –  Apr 16 '15 at 13:49
  • IAbsObj has only the method Save() that has to be implemented differently between say DbUser and XmlUser. If i implement Save() into User i should create a "mocking" method or turn User into an abstract class (which would lose me the chance to initialize it and i'd prefer not). I already tried this approach but since it requires creating a "useless" method in the best case i was wondering if there's another way. On the other point you're right i misunderstood it. – Alberto Apr 16 '15 at 13:56
  • I mean about the user creation. The part about implementing the interface in User still puzzles me. I tought that giving the constraints in the class would ensure i could add the object into the list, since it's always T. – Alberto Apr 16 '15 at 13:57