patterncsharpMinor
Generic Object Editing Classes with Controller, ViewModelProvider and DBUpdater
Viewed 0 times
genericdbupdatereditingwithcontrollerclassesandobjectviewmodelprovider
Problem
I am beginning to use generics and would like feedback on the following set of classes that will serve as the basis for an app that contains a large number of basic CRUD-y entities.
I have used
My aim is to make maintenance / adding more of these classes as painless as possible, but also to allow for deviations when they arise. With that in mind, for the
The Repository layer should be flexible and allow non-enumerated selections. The ViewModelProvider is the cut off point and all methods should return enumerated data.
I'd really appreciate your opinions on this code and any suggestions for improvement.
Entity Base Classes
```
public interface IDBEntity
{
int ID { get; set; }
string Title { get; set; }
List PreventDeleteReasons { get; }
bool IsDeletable { get; }
}
public class DBEntity : IDBEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Display(Name = "Name",
Description = "")]
[StringLength(250, ErrorMessage = "{0} cannot be more than {1} characters long ")]
public virtual string Title { get; set; }
// just override this to prevent delete
// protected: The type or member can only be accessed by code in the same class or struct, or in a derived class.
protected virtual List GetPreventDeleteReasons()
{
return new List();
}
// no need to override these
public virtual List PreventDeleteReasons
{
get
{
if (_PreventDeleteReasons == null)
{
_PreventDeleteReasons = GetPreventDeleteReasons();
}
return _PreventDeleteReasons;
}
}
[NotMapped]
private List _
I have used
Car and Person classes as examples, but there will be a whole bunch of basic classes like these, perhaps 50 +. My aim is to make maintenance / adding more of these classes as painless as possible, but also to allow for deviations when they arise. With that in mind, for the
Person class I have:- added a custom action / viewmodel
- added a different method signature for Index, that will allow filtering by the Starsign property
The Repository layer should be flexible and allow non-enumerated selections. The ViewModelProvider is the cut off point and all methods should return enumerated data.
I'd really appreciate your opinions on this code and any suggestions for improvement.
Entity Base Classes
```
public interface IDBEntity
{
int ID { get; set; }
string Title { get; set; }
List PreventDeleteReasons { get; }
bool IsDeletable { get; }
}
public class DBEntity : IDBEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Display(Name = "Name",
Description = "")]
[StringLength(250, ErrorMessage = "{0} cannot be more than {1} characters long ")]
public virtual string Title { get; set; }
// just override this to prevent delete
// protected: The type or member can only be accessed by code in the same class or struct, or in a derived class.
protected virtual List GetPreventDeleteReasons()
{
return new List();
}
// no need to override these
public virtual List PreventDeleteReasons
{
get
{
if (_PreventDeleteReasons == null)
{
_PreventDeleteReasons = GetPreventDeleteReasons();
}
return _PreventDeleteReasons;
}
}
[NotMapped]
private List _
Solution
IDBEntity, IGenericRepository, IItemListVM, IAttemptDeleteVMYou are exposing a
List via a property which shouldn't been done. Assume you want/need to change this to another type which say is implementing IList or ICollection but isn't a List then the caller/user of your interface would need to change his/her implementation. A List is an implementation detail which shouldn't be exposed.See also: CA1002: Do not expose generic lists
DBEntity// no need to override these
public virtual List PreventDeleteReasonsso why is this property
virtual ? If it shouldn't be able to be overridden then remove the virtual keyword and also that comment.General##
Based on the Net naming guidelines variables should be named using
camelCase casing. See DB and Table in GenericRepository.Code Snippets
// no need to override these
public virtual List<string> PreventDeleteReasonsContext
StackExchange Code Review Q#116509, answer score: 2
Revisions (0)
No revisions yet.