0

So what we'd like to have is a library that expands across many modules, and each add their own respective functionality. For example our base class.

public class Account {
  public string Username;
}

Then in a separate package (let's say the ActiveDirectory package)

public partial class Account {
  public UserPrincipal Principal;
}

which would extend our Account class to

public class Account {
  public string Username;
  public UserPrincipal Principal;
}

However using the partial keyword is only supported within the same project. I also don't want to just extend Account to an ADAccount because other modules will want to add their own properties and methods and there isn't a structured hierarchy of classes. Extension Methods is a possibility but they are only methods and they can't store any additional data to the class.

Practical Use-Case

I'd like for a user to be able to

Install-Package BaseLibrary

and get public class Account { public string Username } or run

Install-Package BaseLibrary
Install-Package BaseLibrary-ActiveDirectory
Install-Package BaseLibrary-Twitter

and get

public class Account {
  public string Username;
  public UserPrincipal Principal;
  public Twitter TwitterUser;
}

How can this be done?

Although ExpandoObject's can do this I would obviously prefer a Statically Typed option.

Community
  • 1
  • 1
xori
  • 716
  • 7
  • 9
  • how can this be done? – xori Oct 06 '14 at 19:28
  • 3
    Isn't this what the Decorator pattern is for?http://www.codeproject.com/Articles/479635/UnderstandingplusandplusImplementingplusDecoratorp – Novaterata Oct 06 '14 at 19:29
  • I believe the Decorator pattern implies that there is a shared interface between all of the modules. In this case the Twitter and AD modules are unrelated. – xori Oct 06 '14 at 19:31
  • It isn't possible to add new properties to types in this way. You can only dynamically extend objects by adding [extension methods](http://msdn.microsoft.com/en-GB/library/bb383977.aspx). The only way I can think to do this is to put the actual code files into your nuget package, rather than a DLL, and use partial classes as you suggested. – Rhumborl Oct 06 '14 at 19:39
  • @Rhumborl I didn't know you could do that with Nuget. Would it still be possible to upgrade to a newer version of a package? – xori Oct 06 '14 at 19:43
  • I feel like your are asking for something that is inherently not statically typed. If you get two or three different interfaces for a class depending on which plugins are installed then you have two or three different types. – Novaterata Oct 06 '14 at 19:44
  • @novaterata yes, I am very much bias to the Repository pattern that javascript is able to do. Being able to do `Company.Account()` and get an object that has all of the required methods and properties for a project is very seducing. – xori Oct 06 '14 at 19:49
  • 1
    @xori That's fine, thats what dynamic / ExpandoObject are for, but I found this question http://stackoverflow.com/questions/309939/should-you-use-a-partial-class-across-projects that might help you with other ideas. – Novaterata Oct 06 '14 at 19:49
  • @xori I just Googled c# Repository pattern and there seemed to be plenty of examples as well. – Novaterata Oct 06 '14 at 19:52
  • @novaterata yes I've been trying to understand how I should implement them – xori Oct 06 '14 at 19:56

0 Answers0