Here is an example of declaring a single parameter generics class with single constraints.
public class TestClass<T> : ITestClass<T> where T : Teacher
I am looking for a way to declare the TestClass with multiple constraints. Where I will be able to use an interface for different classes.
How can I do something like this?...
public class TestClass<T> : ITestClass<T> where T : Teacher, Student, new()
My classes look like
public class Teacher{
public string Name { get; set; }
public string Designation { get; set; }
}
public class Student{
public string Name { get; set; }
public int Roll { get; set; }
public string Department { get; set; }
}
Related
I'm having a trouble configuring May-to-many with TPC inheritance
public class TestB
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ParentClass> ParentClasss { get; set; }
}
public abstract class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TestB> TestBs { get; set; }
}
[Table("Child_A")]
public class Child_A: ParentClass
{
public string childAName { get; set; }
}
[Table("Child_B")]
public class Child_B: ParentClass
{
public string childbName { get; set; }
}
the many to many relation is on the abstract class, the generated tables are
TestB
ParentClass
ParentClassTestB : the many to many relation
Child_A : have FK for the ParentClass
Child_B : have FK for the ParentClass
what I need is to have the many to many directly with Child_A and Child_B.
so the generated tables will be something like
TestB
Child_A
Child_ATestB : the many to many relation table between Child_A and TestB
Child_B
Child_BTestB : the many to many relation table between Child_B and TestB
regards
what i need is to have the many to many directly with Child_A and Child_B.
Then don't map ParentClass as an Entity. You can still have it as the parent class in your code, but as far as the database is concerned Clild_A and Child_B are unrelated.
This is especially important in TPH, which has serious performance implications. This way there's simply no database overhead for your inheritance hierarchy. And the only real downside is that you don't have a built-in search over all ParentClass entities.
The only change is that if you don't want ParentClass involved in the M2M in the database, you can't have a navigation property from TestB to ParentClass. So
public class TestB
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child_A> ParentClasssA { get; set; }
public ICollection<Child_B> ParentClasssB { get; set; }
}
public abstract class ParentClass
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TestB> TestBs { get; set; }
}
[Table("Child_A")]
public class Child_A : ParentClass
{
public string childAName { get; set; }
}
[Table("Child_B")]
public class Child_B : ParentClass
{
public string childbName { get; set; }
}
I have a model class that is extended from the realm object. In some cases, I use this model as both realm model and POST operations. Currently, the realm IList properties unable to support setter options. Is there any option or way to achieve this?
Here is my current code:
[JsonObject]
public class Product : RealmObject, IProduct
{
[MapTo("name")]
[JsonProperty("name")]
public string Name { get; set; }
[MapTo("skuDetails")]
[JsonProperty("skuDetails")]
public IList<SkuDetail> SkuDetails { get; }
}
My requirement :
[JsonObject]
public class Product : RealmObject, IProduct
{
[MapTo("name")]
[JsonProperty("name")]
public string Name { get; set; }
[MapTo("skuDetails")]
[JsonProperty("skuDetails")]
public IList<SkuDetail> SkuDetails { get; set; }
}
you need to derive your class according your need.
this is totally possible like the code below:
public interface ITest
{
IList<object> SkuDetails { get; }
}
public class OutTest : ITest
{
public IList<object> SkuDetails { get; set; }
}
Please note that extending property method is supported however opposite of it is not.
Ex:
public interface ITest
{
IList SkuDetails { get; set; }
}
public class OutTest : ITest
{
public IList<object> SkuDetails { get; }
}
is not possible.
I have 2 models which have exactly same fields, but I chose to make different models for them because I needed two different tables, one for each.
Earlier everything was working fine when I had two different tables for each model, but then I started using abstract base class because the code inside both the models were same.
Now I have a single table comprised of all the data that I save.
How can I create different tables for those two models.
public abstract class baseGrammar
{
[Key]
public int Id { get; set; }
[Required]
public string question { get; set; }
[Required]
public string ans { get; set; }
public string ruleId { get; set; }
public string ruleApplicable { get; set; }
[ForeignKey("ruleId")]
public virtual ruleTable RuleTable { get; set; }
}
The one shown above is my abstract base class.
public class article : baseGrammar
{
}
public class adjective : baseGrammar
{
}
Just if someone intrested in ruleTable model.
public class ruleTable
{
[Key]
public string ruleId { get; set; }
public string topic { get; set; }
public string rule { get; set; }
public string example { get; set; }
public virtual ICollection<baseGrammar> BaseGrammar { get; set; }
}
Am also adding context class so as to provide better description
public class english : DbContext
{
public english() : base("name=localServerEng")
{
Database.SetInitializer<DbContext>(null);
Database.SetInitializer<english>(new UniDBInitializer<english>());
}
public virtual DbSet<adjective> adjectiveDb { get; set; }
public virtual DbSet<adverb> adverbDb { get; set; }
public virtual DbSet<alternativeVerb> alternativeVerbDb { get; set; }
public virtual DbSet<antonyms> antonymsDb { get; set; }
public virtual DbSet<article> articleDb { get; set; }
private class UniDBInitializer<T> : DropCreateDatabaseIfModelChanges<english>
{
}
public System.Data.Entity.DbSet<StructureSSC.Areas.AreaEnglish.Models.baseGrammar> baseGrammars { get; set; }
}
Screenshot of SQL Server showing 1 table comprising of all columns instead of different tables
This set up will give you 2 tables: (1) adjectives (2) articles
The context should be like this:
public class SomeContext : DbContext
{
public SomeContext()
: base("name=SomeContext")
{
}
public virtual DbSet<article> Articles { get; set; }
public virtual DbSet<adjective> Adjectives { get; set; }
}
public abstract class baseGrammar
{
//... common properties/columns
}
public class article : baseGrammar
{
}
public class adjective : baseGrammar
{
}
Please note the naming convention. In .NET class names and property names should follow Pascal Notation. Therefore, they should be:
BaseGrammar
Article
Adjective
RuleApplicable // other properties should follow same convention
I have an employee object:
public class CreateEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
[Import(AllowDefault=true)]
public ExtendEmployee ExtendEmployee { get; set; }
}
public class ExtendEmployee
{
public string Id { get; set; }
}
I want to extend this ExtendEmployee during runtime using MEF.
[Export]
public class ExtendCreateEmployee : ExtendEmployee
{
public decimal Salary { get; set; }
}
My Question is: If I dont define this [Export], is there a way to
import the base class "ExtendEmployee" instead of the default null for
the import.
I considered decorating the base class with [Export] attribute but in that case, the import will consider both the classes and I have to filter the inherited class. This can be fine if there is a way to either choose from the base class or the inherited class.
Thanks
I'm working on application in which the user defines classes using dedicated editor. The result of this step is db table which holds a class name and list of properties attached to it by the user.
The classes can only hold only primitive types, but struct, as parameters.
The next step is to load the table rows as dynamic objects to another application.
Beside using reflection, is there another way to convert table row to POCO?
These are the models:
[Description("represents the base model")]
public class
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
}
public class ParameterModel : BaseModel
{
public string Type { get; set; }
public string DefaultValue { get; set; }
}
[Description("Represents dynamic activity")]
public class ActivityModel : BaseModel
{
public List<ParameterModel> Parameters { get; set; }
}
Thank you very much.