c# - Base Class Enum Implemented Differently in Derived Classes -



c# - Base Class Enum Implemented Differently in Derived Classes -

my scenario:

public class entitybase { public int id { get; set; } [required()] public string name { get; set; } //and getting me //i want "type" enum }

then derived classes have different enums assign type.

public class animalentity : entitybase { //type have 'animal type' value: land, sea or air //implementation code like: // myanimal.type = animaltype.land } public class personentity : entitybase { //type have 'person type' value: doctor, lawyer or engineer //implementation code like: // myperson.type = persontype.lawyer } public class monsterentity : entitybase { //type have 'monster type' value: goblinoid, undead }

so, big question trying do, right? trying create base of operations repository class, homecoming entities grouped type. entities have kind of "type", , want create generic "group type".

public abstract class repositorybase<t> : irepositorybase<t> t : entitybase { //our mutual getasync, getbyidasync, , our other crud //and this: public ienumerable<groupeddata<string, t>> getgroupedbytype(string searchterm) { var entities = s in dbset (searchterm == null || s.name.tolower().contains(searchterm)) grouping s s.type g select new groupeddata<string, t> { key = g.key.tostring(), info = g }; homecoming (entities); } }

when t animalentity, groups land, sea , air corresponding entities. personentity, doctor, lawyer, engineer groups.

if approach/design invalid or less ideal, please allow me know.

enum (please pardon me) kind of second class citizens first thing may think not work:

class entitybase<t> t : enum { public t type { get; set; } }

unfortunately doesn't compile, may think replace enum base of operations class:

class entitybase<t> t : entitytypebase { public t type { get; set; } }

implementing in entitytypebase need comfortable them (== , != operators, iconvertible interface , other boilerplate). it's lot of code , you'll need manage in ef (otherwise won't able utilize such property in queries unless load in memory objects). may force utilize of enums (with run-time check) break sql code generation in ef.

what's i'd suggest in case utilize type ef knows , understand. may utilize string (if wish so) or integer (as in example):

class entitybase public virtual int type { get; set; } }

in derived class:

class animalentity : entitybase { public override int type { { homecoming base.type; } set { if (!enum.isdefined(typeof(animaltype), value)) throw new argumentexception(); base.type = (int)value; } } }

in way still can utilize persontype.layer , animaltype.land keeping little of type safety. of course of study need maintain enums in-sync not have duplicated values (otherwise group by won't work).

as lastly please consider use...another entity. if have table entitytype:

id name applicableto 0 laywer person 1 programmer person 2 land animal ...

what have in setter check if type applicable or not , may have few convenience classes grouping them type:

public static class persontype { public static entitytype lawyer { { ... } } public static entitytype programmer { { ... } } }

imo scale better (easier add together new items , can delegate, in future, behavior entitytype items) , it safer hard-coded constants (because integrity granted db engine itself). of course of study cost pay overhead search in entitytype table (unless utilize caching mechanism).

c# inheritance enums

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -