Get all associate/composite objects inside an object (in Abstract way)
I think you might want to step back from the design for a moment. What I've heard is this:
A payment consists of one or more components, and each component can be one of a variety of types
What it sounds like you need is a Payment
table, then a PaymentComponent
table with a foreign key relation back to the Payment
table. You can then implement inheritance on the PaymentComponent
table for your various forms of payment.
Scala - Create a List of case objects that extend an sealed abstract class
Because those objects don't exist unless they are referenced from an instance of the type of Peculiarity
because they are declared as internal objects to Peculiarity
.
val pec = new Peculiarity("","") {}
val peculiarities = List[Peculiarity](
pec.CompositeSpectrum,
pec.NebularFeaturesPresent,
...
But I doubt that what you after, you can just move the declaration to outside the abstract class
sealed abstract class Peculiarity(val abbreviation: String, val description: String)
case object CompositeSpectrum extends Peculiarity("comp", "composite spectrum")
case object NebularFeaturesPresent extends Peculiarity("neb", "nebular features present")
...
And then your declaration would work just fine.
Composite pattern with different types of objects
Composite pattern allows you to treat a collection of objects the same as a leaf object.
I'd say that you could do something like this:
public interface Command
{
void execute(Object parameter);
}
public class LeafCommand implements Command
{
public void execute(Object parameter)
{
// do something for a leaf
}
}
public class CompositeCommand implements Command
{
private List<Command> commands;
void execute(Object parameter)
{
for (Command child : commands)
{
child.execute(parameter);
}
}
}
That's what Composite means to me. You're right - if you have to use instanceof
you've done it wrong.
How can I associate a new entity with an existing entity in EF4?
I am not sure I completely understand what you are trying to do but according to your schema, the Patient table is a parent of TreatmentPlanPatients table (one to many relationship). Hence, you should add the TreatmentPlan to the Patient itself and not the Patients table. So rather than doing container.Patients.AddObject(treatmentPlanPatient), you should do patient.TreatmentPlanPatients.Add(treatmentPlanPatient). In this case, TreatmentPlanPatients should be a navigational property on the Patient table/entity.
OR, if you really want to use inheritance, then you have to use the same primary key (Id in this case) in both tables and have a one-to-one relationship.
Deriving from classes generated by Entity Framework in C#
Why do you need to inherit from entity class first of all? If you want to add some simple behavior, use partial class.
Update: Based on comments, it appears that there is possibility that behavior will be extended over the time. In this case, I would recommend using composition/aggregation, not inheritance. Let the classes that need to be extended have an entity as a field. In Raheel's scenario, it would be a class called TemplateLogic with field/property of type Template.
Multiple Inheritance in LINQtoSQL?
Entity Framework supports table per type now: http://msdn.microsoft.com/en-us/library/bb399159.aspx
Inheriting from entity classes and extending the class
One thing that you need to keep in mind is that ORM's like the Entity Framework don't actually create a simple data access layer (when viewed through the general 3-tier architecture). What they give you is much more of a business layer with a slightly more granular level of control over the data access interaction. I think it can be argued that EF essentially becomes your DAL and the context and entity types can be the BLL.
The line's a lot easier to see when viewed through more of an MVC or MVVM architecture, where you have a model (your EF layer), a controller or viewmodel (where the business logic sits, which encapsulates the model), and a view.
Anyhow, because EF actually has to instantiate the entity types directly, inheriting wouldn't do you much good because EF won't use your subtype as the returned entity. The solution for validation and similar tasks in EF is to make use of both partial classes (which you obviously know about) and partial methods. The default code-generation templates in EF produce partial methods for all scalar properties for OnPROPERTYNAMEChanging
and OnPROPERTYNAMEChanged
.
For instance, if you have an int UserId
property on your User
entity type, you can create a partial class that looks like this:
public partial class User
{
partial void OnUserIdChanging(int newUserId)
{
// do something
}
partial void OnUserIdChanged()
{
// do something
}
}
You can, of course, only use one or the other if you like. The advantage that partial methods would have over an inherited call (assuming it was even possible) is that partial methods are non-virtual (so there's no virtual table lookup to call the correct member) and are only compiled as part of the class if there's an actual implementation.
In other words, even though you can go into the designer code and see the declarations for
partial void OnUserIdChanging(int value);
partial void OnUserIdChanged();
If you don't actually add a method body to the function, then the C# compiler completely removes the function and all calls to it from the code, as if it had never been there in the first place. This makes the types smaller and the calls to other properties faster, since it won't have to bother with calling an empty function.
Related Topics
Custom Xmlwriter to Skip a Certain Element
What Is Applicationexception for in .Net
Comboboxes Are Linked for Some Reason
Cannot Convert Lambda Expression to Type 'String' Because It Is Not a Delegate Type
Wcf - Design Parameter Decision
Wpf Listview: Attaching a Double-Click (On an Item) Event
How to Reuse Code for Selecting a Custom Dto Object for a Child Property with Ef Core
Explicitly Freeing Memory in C#
How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#
How to Connect and Use a SQLite Database from C#
How Better to Resolve Dependencies in Object Created by Factory
Raise Events in .Net on the Main UI Thread
External Handler for Msiexec Msisetexternalui
How to Derive Xml Element Name from an Attribute Value of a Class Using Annotations
Using C# to Search a CSV File and Pull the Value in the Column Next to It
String Interpolation VS String.Format
How to Create "Embedded" SQL 2008 Database File If It Doesn't Exist