0

More Informations:

EDIT: better sample:

I have a class in a Libary called UserAccount, its abstact. then i have some functionality like this in the library:

class UserAccountService
{
    public static UserAccount CreateUserAccount(String username, String password, String email)
    {
    UserAccount account = new UserAccount();
    account.Username = username;
    account.HashedPass = //Some crypting stuff with password
    account.Email = email;

    UserAccountRepository db = new UserAccountRepository();
    db.UserAccounts.Add(account);

    return account;
    }
}

Cuz this is a independent library the UserAccount has not all Propertys i want to use:

class ExtendedUserAccount : UserAccount
{
// define some additional methods and propertys
public Contact Contacts{get;set}// this property is only used in one application where i use the Library....
}

Then i want to do this:

ExtendedUserAccount newAccount = UserAccountService.CreateUserAccount(new UserAccount);

But this will not work. i now its not correct but i need something similar...

anyone has an idea??

  • 1
    Basic OOP - this can't work - what do you want to achieve? – BrokenGlass Nov 05 '13 at 13:50
  • `ExtendedUserAccount` is a _subclass_ of `UserAccount`, which is a _superclass_ of `ExtendedUserAccount`. – groverboy Nov 05 '13 at 13:56
  • basicly i whant to dynamic extend the class UserAccount from the Library i use... i have the Soruce Code of this library so im thinking how i can change it to provide this i autonomy –  Nov 05 '13 at 14:19

3 Answers3

3

That looks like a code smell and you probably need to redesign your types... But anyway, this should work:

class UserAccountService
{
    public static TAccount CreateUserAccount<TAccount>(TAccount account)
          where TAccount : UserAccount, new()
    {
        //create new useraccount...
        return account;
    }
}

This generic method takes an instance of a type that must extend UserAccount (or be an UserAccount itself), and declare a parameterless constructor. This last restriction will allow you to do this: TAccount account = new TAccount().

dcastro
  • 66,540
  • 21
  • 145
  • 155
0

I recommand the factory pattern

class UserAccountService
{
    // ctor with interfaces
    public IUserAccount CreateUserAccount()
    {
        // create instance
        return result;
    } 

    // ctor with generic
    public IUserAccount CreateUserAccount<T>() where T : UserAccount
    {
        var account = Activator.CreateInstance<T>();
        return account;
    } 
}

class ExtendedUserAccount : UserAccount
{
    // define some additional methods and propertys
}

class UserAccount : IUserAccount
{

}

internal interface IUserAccount
{
}
lordkain
  • 3,061
  • 1
  • 13
  • 18
  • That's not the abstract factory pattern, nor the factory method pattern. You're using flags to tell the method what you want it to do - which is not a pattern. – dcastro Nov 05 '13 at 13:57
  • you are right, most of the time you wan the factory to decide what to create and return. not with parameters / generics. – lordkain Nov 05 '13 at 14:03
  • i improve my question that u can see what i want to do –  Nov 05 '13 at 14:11
0

Just to make it clear

Are this you conditions?

  • UserAccountService & UserAccount are in Library A
  • ExtendedUserAccount is, and only will be in Library B
  • you can't/dont't want to edit library A

then the answer can be: Make 1 entry point in Library B that make this possible

lordkain
  • 3,061
  • 1
  • 13
  • 18
  • Almost: i can Edit library A but it should be still autonomly... What do you mean with a entry point ? –  Nov 05 '13 at 14:44