Is it possible to extend LINQ-to-SQL entity-classes with constructor-methods and in the same go; make that entity-class inherit from it’s data-context class?–In essence converting the entity-class into a business object.
This is the pattern I am currently using:
namespace Xxx { public class User : Xxx.DataContext { public enum SiteAccessRights { NotRegistered = 0, Registered = 1, Administrator = 3 } private Xxx.Entities.User _user; public Int32 ID { get { return this._user.UsersID; } } public Xxx.User.SiteAccessRights AccessRights { get { return (Xxx.User.SiteAccessRights)this._user.UsersAccessRights; } set { this._user.UsersAccessRights = (Int32)value; } } public String Alias { get { return this._user.UsersAlias; } set { this._user.UsersAlias = value; } } public User(Int32 userID) { var user = (from u in base.Users where u.UsersID == userID select u).FirstOrDefault(); if (user != null) { this._user = user; } else { this._user = new Xxx.Entities.User(); base.Users.InsertOnSubmit(this._user); } } public User(Xxx.User.SiteAccessRights accessRights, String alias) { var user = (from u in base.Users where u.UsersAccessRights == (Int32)accessRights && u.UsersAlias == alias select u).FirstOrDefault(); if (user != null) { this._user = user; } else { this._user = new Xxx.Entities.User { UsersAccessRights = (Int32)accessRights, UsersAlias = alias }; base.Users.InsertOnSubmit(this._user); } } public void DeleteOnSubmit() { base.Users.DeleteOnSubmit(this._user); } } }
Update:
Notice that I have two constructor-methods in my User class. I’d like to transfer those to the User entity-class and extend the User entity-class on it’s data-context class, so that the data-context is available to the entity-class on ‘new-up’.
Hope this makes sense.
It doesn’t seem to make sense to make an entity a type of DataContext. It doesn’t need to be a DataContext in order to be considered a business object, nor do you necessarily need to create a type that contains the original entity. It might be better to just extend the entity class and contain a reference to a DataContext using composition:
If you just want the property names to be different than the database column names, do that in the mapping file.