Subsonic and sub tables - subsonic

I love NHibernate's ability to have one table to store multiple types based on a discriminator. When I fetch an RegularItem, it will come back as the sub type of the discriminator is correct.
Does SubSonic have this ability?

Do you mean, you have a table with different values stored in it and, dependend on a value you want to return different objects?
e.g. you have a table pet
id type name
---------------------
1 dog bello
2 cat scott
3 cat tiger
and you want to get dog and cat objects from it?
I have a similar case, and I solved it by creating a Dog class and a Cat class that both inherit from subsonic's autogenerated pet class and implement my IPet interface stub, in conjunction with a factory method, where I cast my objects to the new Type:
public Class Dog : Pet, IPet { }
public Class Cat : Pet, IPet { }
public Interface IPet { }
public static IPet GetAllPets()
{
List<IPet> pets = new List<IPet>();
foreach Pet pet in PetCollection.FetchAll()
{
IPet newpet;
if (pet.Type == "dog")
newpet = new Dog();
else if (pet.Type == "cat")
newpet = new Cat();
else throw new InvalidOperationException("Unknown pet type " + pet.Type);
pet.CopyTo(newpet);
newpet.MarkOld();
pets.Add(newpet);
}
}
Typed from memory, not guaranteed to compile. But the theory should be clear.

The short answer is no, SubSonic does not have this feature built-in. You might be able to sort of recreate that with ExecuteTypedList<>, but it would be a lot of manual work (you'd probably be rewriting most of the functionality of the NH feature).

Related

Suggestions how to break up projects with #XmlSeeAlso limiting me

I'd like to start breaking apart a large domain project into various smaller sub projects. The issue is that I have an external project that has some code that needs to unmarshal some xml into a top level base class that all the subprojects can share as a common base. This would be fine, except that I will need an #XmlSeeAlso on the base class that would include every possible subclass (so it seems.) This seems to make it impossible for me to break up the projects in the way I planned.
For example, using animals just for illustration.
Without the XmlSeeAlso on Animal defining the subclasses, I'm pretty much stuck.
//Core project jar
public abstract class Animal {
}
//Mammals jar
#XmlSeeAlso({ Dog.class, Cat.class})
public abstract class Mammal extends Animal {
}
//Birds jar
#XmlSeeAlso({ Sparrow.class, Hawk.class})
public abstract class Bird extends Animal {
}
//Third party jar
//use Bird or Mammal xml
input = new ByteArrayInputStream(birdXml.getBytes());
JAXBContext jc = JAXBContext.newInstance(Animal.class);
u = jc.createUnmarshaller();
//return Animal - FAILS since Animal would need the XmlSeeAlso of subclasses
Animal animal = (Animal)u.unmarshal(input);
I didn't realize that JAXBContext.newInstance can take an array of classes. So in my third party jar I just need to load them all into some common Array and pass them in there to the newInstance method ... JAXBContext.newInstance(myClassesArray);

Initializing objects using a super class instead of the subclass

I recently saw this example code and I didn't know how I'd be able to easily find the answer to this question I had. In the code the Dog object, Cow object and Snake object are all declared as Animal objects. So is it valid to declare an object using a more generic class? For instance, could I declare any object as an Object (since all classes are subclasses of the object class)? What are the advantages/disadvantages of declaring specific or more general? Is it all for ease of readability?
class Animal {
void whoAmI() {
System.out.println("I am a generic Animal.");
}
}
class Dog extends Animal {
void whoAmI() {
System.out.println("I am a Dog.");
}
}
class Cow extends Animal {
void whoAmI() {
System.out.println("I am a Cow.");
}
}
class Snake extends Animal {
void whoAmI() {
System.out.println("I am a Snake.");
}
}
class RuntimePolymorphismDemo {
public static void main(String[] args) {
Animal ref1 = new Animal();
Animal ref2 = new Dog();
Animal ref3 = new Cow();
Animal ref4 = new Snake();
ref1.whoAmI();
ref2.whoAmI();
ref3.whoAmI();
ref4.whoAmI();
}
}
The output is
I am a generic Animal.
I am a Dog.
I am a Cow.
I am a Snake.
You could instantiate every other class with object, but not primitives, though even they have wrappers that make them classes.
It's just not good object oriented practice because you want the program to be as fail safe as possible.
Put more varied methods into those classes and that's when you start to see the problems.
lets say Dog also has the method chewButt().
Animal dog = new Dog();
dog.chewButt();
No problem there. But that's not all you're allowed to do.
Animal dog = new Cow();
that compiles...
dog.chewButt();
now we have a problem. Cow can't reach its butt.
You want to be as stringent as possible when declaring the superclass but loose enough to get your job done fast. A reason you may want to be looser is something like this.
dogs and snakes are a little too different. I'mma create a new subclass Mammal that extends from Animal. I put dog and cow and whatever else in it.
now I can be assured I can do:
Mammal mouse = new Mouse();
mouse.suckleYoung();
cat.suckleYoung();
whale.suckleYoung();
but if I accidentally try:
Mammal snake = new Snake();
I want the compiler to complain now before I dig myself into a coding hole.
I hope that helps. Experience will definitely burn the points into your brain soon enough.
Edit: referencing comment: I still don't get what reasons there would be that I wouldn't always just declare my dog object like "Dog dog = new Dog();" vs. how it is in the example code "Animal dog = new Dog();
It's because your function working on dog is also assured to work with all Animals (dogs, cats, lizards). If you went with the other route you would have to go through the trouble of writing a separate function for each class of animal. What's worse is that every function you made would have the same code. Instead write the methods once in Animal and every extended class has that set of methods. Imagine having realized that the method you wrote was slightly wrong, you had to change one line but you had to do it in every animal's module because you didn't take the time to create an Animal superclass.

Aggregate root and instances creation of child entities

I have an aggregate that includes the entities A, AbstractElement, X, Y and Z. The root entity is A that also has a list of AbstractElement. Entities X,Y and Z inherit from AbstractElement. I need the possibility to add instances of X, Y and Z to an instance of A. One approach is to use one method for each type, i.e. addX, addY and addZ. These methods would take as arguments the values required to create instances of X, Y and Z. But, each time I add a new type that inherits from AbstractElement, I need to modify the entity A, so I think it's not the best solution.
Another approach is to use an abstract add method addAbstractElement for adding AbstractElement instances. But, in this case, the method would take as argument an instance of AbstractElement. Because this method would be called by entities located outside of the aggregate, following DDD rules/recommandations, are these external entities authorized to create instances of AbstractElement? I read in the Eric Evans book that external entities are not authorized to hold references of entities of an aggregate other than the root?
What is the best practice for this kind of problem?
Thanks
From Evan's book, page 139:
"if you needed to add elements inside a preexisting AGGREGATE, you might create a FACTORY METHOD on the root of the AGGREGATE"
Meaning, you should create a factory method on the root (A) which will get the AbstractElement's details. This method will create the AbstractElement (X/Y/Z) according to some decision parameter and will add it to its internal collection of AbstractElements. In the end this method return the id of the new element.
Best Regards,
Itzik Saban
A few comments. As the previous answerer said, it's a good practice to use a factory method. If you can avoid it, never create objects out of the blue. Usually, it's a pretty big smell and a missed chance to make more sense out of your domain.
I wrote a small example to illustrate this. Video is in this case the aggregate root. Inside the boundaries of the aggregate are the video object and its associated comments. Comments can be anonymous or can have been written by a known user (to simplify the example, I represented the user by a username but obviously, in a real application, you would have something like a UserId).
Here is the code:
public class Video {
private List<Comment> comments;
void addComment(final Comment.Builder builder) {
this.comments.add(builder.forVideo(this).build());
// ...
}
}
abstract public class Comment {
private String username;
private Video video;
public static public class Builder {
public Builder anonymous() {
this.username = null;
return this;
}
public Builder fromUser(final String username) {
this.username = username;
return this;
}
public Builder withMessage(final String message) {
this.message = message;
return this;
}
public Builder forVideo(final Video video) {
this.video = video;
return this;
}
public Comment build() {
if (username == null) {
return new AnonymousComment(message);
} else {
return new UserComment(username, message);
}
}
}
}
public class AnonymousComment extends Comment {
// ...
}
static public class UserComment extends Comment {
// ...
}
One thing to ponder on also is that aggregate boundaries contain objects and not classes. As such, it's highly possible that certain classes (mostly value objects but it can be the case of entities also) be represented in many aggregates.

How to dynamically create collections of derived objects?

This question may appear to have been answered before but I have been unable to find exactly what I need. Here is my situation:
// Base class
interface IAnimal {};
public abstract class Animal : IAnimal{}
// Derived classes
interface IDog {}
public class Dog : Animal, IDog { }
interface ICat { }
public class Cat : Animal, ICat { }
interface ITiger { }
public class Tiger : Animal, ITiger { }
interface ILion { }
public class Lion : Animal, ILion { }
// Collection Classes
interface IPets { }
public class Pets
{
IDog dog = new Dog();
ICat cat = new Cat();
}
interface ICircus { }
public class Circus
{
ITiger tiger = new Tiger();
ILion lion = new Lion();
}
I would like to create the collections at run time in an generic Event class by reading in a list animals from xml that would make up the collection. What would be the correct way to accomplish this?
Thanks in advance.
This is kind of an answer to my own question. Maybe this will help others.
I chose a very generic example to illustrate my situation because I have uses for this in many places in Windows Forms, XNA and Silverlight that are all very different.
When I used the Activator, I found out that it assumes the executing assembly. My method is in a library so I had to load a different assembly. Next I had to make sure that I had the right namespace. My base class is in a library and the derived classes are in another namespace so this will require refactoring to properly create the list.
Another problem I found was that the Activator assumes a constructor with no parameters. In my test case all my derived classes are XNA game components with a parameter of type Game.
Have to do some refactoring to test out the interfaces and how the game objects are to interact.
Will be back to this list when I have something further.
Does this sort of example help? (It's from some of my code I happened to have handy.) The key point here is the use of reflection in Activator.CreateInstance(...).
public static List<dynamic> LoadChildEntities(XElement entityElt)
{
var children = new List<dynamic>();
foreach(XElement childElt in entityElt.Elements("entity"))
{
// Look up the C# type of the child entity.
string childTypename = "MyNamespace." + Convert.ToString(childElt.Attribute("type").Value);
Type childType = Type.GetType(childTypename);
if(childType != null)
{
// Construct the child entity and add it to the list.
children.Add(Activator.CreateInstance(childType, childElt));
}
else
{
throw new InvalidOperationException("No such class: " + childTypename);
}
}
return children;
}
If you want a list of IAnimal instead, it wouldn't be too tricky to change.

Populating field from enum table

I have the following tables
Entity
id,name,categoryid
21,"Blah",1
EntityCategory(Enum table)
id, name
1,"New Blahs"
I have a FK relationship between Entities->categoryid and EntityCategories->id
I have generated SubSonic classes for both as well a corresponding Model object for Entity
class Entity{ID,Name,CategoryName}
I am trying to return the Model.Entity type with category name filled in i.e.
public Entity GetEntityByName(string name){
return new
Select(
Entity.IdColumn,
Entity.NameColumn,
EntityCategory.NameColumn)
.From(Entity.Schema)
.InnerJoin(Tables.EntityCategory)
.Where(Entity.NameColumn).IsEqualTo(name)
.ExecuteSingle<Model.Entity>();
Needless to say this is not working. I actually get a Model.Entity with the Entity.Name set to the EntityCategoryName.
If you use SubSonic 3.0 you can do this with projection:
var result = from e in db.Entities
where e.ID=1
select new Entity{
ID=e.ID,
name=e.Name,
CategoryName=(CategoryName)e.CategoryID
}
With SubSonic 2.x, I'd say to make it easy on yourself and extend the partial class with a readonly enum:
public partial class Entity{
public CategoryName{
return (CategoryName)this.CategoryID;
}
}

Resources