2

See the code below. I'd like do some check on some properties (on IsActive for example). Could you tell me how do this in my case how implement this in the GetList() ?

Thanks,

   public interface ILookup
    {
        int Id { get; set; }
        string FR { get; set; }
        string NL { get; set; }
        string EN { get; set; }
        bool IsActive { get; set; }
    }

    public class LookupA : ILookup
    {

    }
    public class LookupB : ILookup
    {

    }

    public interface ILookupRepository<T>
    {
        IList<T> GetList();
    }


    public class LookupRepository<T> : ILookupRepository<T>
    {
        public IList<T> GetList()
        {
            List<T> list = Session.Query<T>().ToList<T>();
            return list;
        }       
    }

2 Answers 2

3

If you know T will be of type ILookup you need to put a constraint on it like such:

public interface ILookup
{
    int Id { get; set; }
    string FR { get; set; }
    string NL { get; set; }
    string EN { get; set; }
    bool IsActive { get; set; }
}

public class LookupA : ILookup
{

}
public class LookupB : ILookup
{

}

public interface ILookupRepository<T>
{
    IList<T> GetList();
}


public class LookupRepository<T> : ILookupRepository<T> where T : ILookup
{
    public IList<T> GetList()
    {
        List<T> list = Session.Query<T>().Where(y => y.IsActive).ToList<T>();
        return list;
    }       
}
Sign up to request clarification or add additional context in comments.

4 Comments

Darn, beat me by seconds ;p Aside: in my code I also had where T : ILookup on ILookupRepository<T>, since it sounds like it is always used with ILookup - maybe one for the OP to ponder...
I had constraint on ILookupRepository also but removed it. The reason for this is that I don't see a reason to constrain ILookupRepository to just one type, even though the name of the interface suggest so. I wouldn't constrain myself more than necessary.
Imagine, I'd like add as parameter the field name I want the value back, example : GetById(myIdValue, thefield), thefield has the value EN, FR or NL
If you don't have that many parameters I would sort of hard code it for performance. But if you have more than EN, FR and NL I would use reflection. Is your string some kind of lookup for translations? If that is the case I would use some kind of dictionary instead of having one variable per language.
0

You should be able to leverage Generic Constraints to help you out.

First, change your interface definition:

public interface ILookupRepository<T> where T : ILookup
//                                    ^^^^^^^^^^^^^^^^^

Second, change your class definition to match the constraints:

public class LookupRepository<T> : ILookupRepository<T> where T : ILookup
//                                                      ^^^^^^^^^^^^^^^^^

The constraint will require the generic type parameter to implement ILookup. This will let you use the interface members in your GetList method.

1 Comment

He doesn't need the constraint on ILookupRepository to get his thing working. Read my comment on my answer why I think that is not necessary.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.