ury
ury

Reputation: 1160

Hiding implementation of value types in .NET/C#

Please consider my use case: I'm developing a software system in which there are uniquely identified "items" of different types (users, documents, tickets etc.).

I've started the implementation, and while I'd like to move forward with the implementation, I'm not exactly sure what would be the most suitable unique identifier of the "items". It might be an auto-incremented integer, a GUID, a string containing a prefix + the creation time etc.

Obviously, variables of the unique identifier type are declared all over my classes, and if I intend to change it later - well, it can't be good...

My question is, is there a .NET mechanism for typedef-ing value types, so I can change the 'real' type and the type-specific functionality in a single location (for example, using extension methods) and not worry about the dozens of other occurrences in the code.

Upvotes: 0

Views: 90

Answers (2)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

What about inheritance?

public class UniqueIdentifiedObject
{
    public Guid Id { get; set; }
}

public class User : UniqueIdentifiedObject 
{
    // You don't need to stupidly repeat yourself (DRY)
}

If you can't use inheritance because you've already derived these classes from who know what other class... Then you can use generics:

public class User<TId> where TId : IEquatable<TId>
{
    public TId Id { get; set; }
}

public class Employee<TId> where TId : IEquatable<TId>
{
    public TId Id { get; set; }
}

This means that you'll need to provide the type of the whole unique identifier during your class instantiations:

Employee<Guid> employee = new Employee<Guid>();

I believe generics gets closer to what you're looking for, because it allows you to define a set of classes with an Id property but the class itself won't know what type of unique identifier is being used until some code instantiates the whole class. Obviously this has a big drawback: if you want to change the unique identifier type, you'll need to change all class instantiations...

Anyway, a massive find & replace using a regexp like (?:User|Employee|Boss|Document)<([A-Za-z0-9-_]+)> or something like this would be enough to change the type of unique identifier for every domain class in your project!).

Upvotes: 1

Jcl
Jcl

Reputation: 28272

No, there isn't. Not globally. However you can make a base class, like:

 public class BaseEntity
 {
    public int Id { get; set; }
 }

Then derive all of your objects from there:

 public class MyItem : BaseEntity
 {
 }

Then you'd only change your type in one place.

You can create an alias for a type, but you need to define it on every module, can't be done globally:

 using MyIdType = System.Int32;

Documentation for aliasing is here

That said: I don't think it's good practice to change these kind of things around... think twice, code once.

Upvotes: 1

Related Questions