views:

775

answers:

1

I've been working on a code generater for my projects and, while all has worked well. But now, I've stumbled upon a detail that I'm not sure how to resolve. This is for my DAL layer, which is intended to allow implementations for diferente dataSources.

so I've got an interface:

public interface IUsersDALBase
{
    //defined methods
}

a generic class that implements it:

public class UsersDALBase<T> : DBDataAccessBase where T : DBDataAccessBase, IUsersDALBase, new()
{ 
    protected T _UsersInstance = default(T);

    public string connectionString
    {
        get { return _UsersInstance.connectionString; }
        set { _UsersInstance.connectionString = value; }
    }

    //constructors (...)

    //interface method implementations, that call _UsersInstance methods (...)
}

For reference, DBDataAccessBase just holds the connection string and has disposing methods to standardize the *DALBAse classes. There is also an empty derived definition to extended (and there for not needing the "Base" suffix):

 public partial class UsersDAL<T> : UsersDALBase<T>  where T : DBDataAccessBase, IUsersDALBase, new()
{
    //empty in generated file
}

this class is just a wrapper around UsersDALBase and is the one I want to extend in a new file. T type classes are mainly autogenerated also, but with DB specific implementations.

So finally, I want to extend UsersDAL in another file with custom methods that the generated code doesn't, and represented by a new interface

public interface IUsers2
{
   UsersList GetAppRoleUsers(int NS_AppRole);
}

I'm trying something like:

public partial class UsersDAL<T> : UsersDALBase<T> where T : DBDataAccessBase, IUsers2, new()
{
    //interface method implementation

    //compile error: Partial declarations of 'DAL.UsersDAL<T>' have inconsistent 
    //constraints for type parameter 'T' -supose i can't have diferente interface 
    //implementations for T...  

}

I basicly want UserDAL to have all the autogenerated methods (that implement IUsersDALBAse) and the methods that I've defined in the new interface IUsers2. Any Ideas on how I can get this???

Thanks!

+3  A: 

If multiple files specify type constraints, those constraints have to match I'm afraid.

From section 10.2.3 of the C# 3 spec:

When a partial generic type declaration includes constraints (where clauses), the constraints must agree with all other parts that include constraints. Specifically, each part that includes constraints must have constraints for the same set of type parameters, and for each type parameter the sets of primary, secondary, and constructor constraints must be equivalent. Two sets of constraints are equivalent if they contain the same members. If no part of a partial generic type specifies type parameter constraints, the type parameters are considered unconstrained.

Could you supply any extra interfaces you want in the partial class declaration as input to the code generator?

Jon Skeet
currently, no, But I guess I should include it in the next iteration. My work around for this now is just to implement a specific set of classes for the new interface. Kind of like a single responsability implementation.Thanks for the clarification.
MytyMyky