How to define recursive Property in Castle ActiveRecord? - castle-activerecord

Suppose you have a class named MyClass. MyClass should have a property named Parent, Parent must be of type MyClass itself. It is necessary because MyClass wants to hold a tree structure.
How can it be done?

It's pretty straightforward:
[ActiveRecord(Lazy = true)]
public class MyClass {
[BelongsTo]
public virtual MyClass Parent { get;set; }
}
You might also want to map the collection of children.
See these articles for more information on how to run recursive queries over this:
http://ayende.com/Blog/archive/2006/11/22/ComplexQueriesWithActiveRecord.aspx
http://web.archive.org/web/20090806071731/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx
http://ayende.com/Blog/archive/2009/08/28/nhibernate-tips-amp-tricks-efficiently-selecting-a-tree.aspx

Related

Static class in Android

I have a question, I confuss about when I must use class static.
I understand that I have use class static when I need some methods that going to use many times in the code, and that class doesn´t need to be declarate, but in a example in android I find that code.
Where they declare a static class and call it with an instance...
Why did they do that?
public View getView(int position, View view, ViewGroup viewGroup) {
//View holder pattern
**ViewHolder holder;**
if(view ==null){
LayoutInflater layoutInflater=LayoutInflater.from(this.context);
view=layoutInflater.inflate(R.layout.list_item,null);
**holder=new ViewHolder();**
holder.txtView =(TextView) view.findViewById(R.id.txtView);
view.setTag(holder);
}
return view;
}
**static class ViewHolder{
private TextView txtView;
}**
Thanks for your explanation..
The advantage of having a static nested class over an non static one is, that to create an instance of the static nested class you don’t need an instance of the outer class. If you only have a non static inner class you need an object of the outer one to be able to create an instance.
Note that only nested classes can be static.

Use of Wrapper class for deserialization in callout?

I found the following use of a wrapper class, and was wondering if it is a good practice or whether its just duplication of code for no reason.
//Class:
public class SomeClass{
public Integer someInt;
public String someString;
}
//Callout Class:
public class CalloutClass{
public SomeClass someMethod(){
//...code to do a callout to an api
SomeClass someClassObj = (SomeClass)JSON.Deserialize(APIResponse.getBody(), SomeClass.class);
return someClassObj;
}
}
//Controller:
public class SomeController {
public SomeController(){
someClassObj = calloutClassObj.someMethod();
SomeWrapper wrapperObj = new SomeWrapper();
for(SomeClass iterObj : someClassObj){
wrapperObj.someWrapperInt = iterObj.someInt;
wrapperObj.someWrapperString = iterObj.someString;
}
}
public class someWrapper{
public Integer someWrapperInt{get;set;}
public String someWrapperString{get;set;}
}
}
The wrapper class "someWrapper" could be eliminated if we just use getters and setters ({get;set;}) in "SomeClass."
Could anyone explain if there could be a reason for following this procedure?
Thanks,
James
My assumption (because, code in controller is extra pseudo) is
SomeClass is a business entity, purpose of which is to store/work with business data. By work I mean using it's values to display it (using wrapper in controller), to calculate smth in other entities or build reports... Such kind of object should be as lightweight as possible. You usually iterate through them. You don't need any methods in such kind of objects. Exception is constructor with parameter(s). You might want to have SomeObject__c as parameter or someWrapper.
someWrapper is a entity to display business entity. As for wrapper classes in controllers. Imagine, that when you display entity on edit page and enter a value for someWrapperInt property, you want to update someWrapperString property (or you can just put validation there, for example, checking if it is really Integer). Usually, as for business entity, you don't want such kind of functionality. But when user create or edit it, you may want smth like this.

Cast Observable Collection from base Class to inherited class

I'm writing a WPF application and I'm currently refactoring some reused code to a base ViewModel Class which my other viewmodels can inherit from.
One Property field on this base class is
public class MessageParentBase
{
MessageParentBase() {}
public string Name;
}
internal ObservableCollection<MessageParentBase> _GridData = new ObservableCollection<MessageParentBase>();
I have a subsequent property declaration
public ObservableCollection<MessageParentBase> GridData
{
get { return _GridData; }
set { _GridData = value; }
}
This works great and everything my issue is that the inerited classes actually use the follow class
Public class ChatMessage : MessageParentBase
{
public string Message;
}
and the view contains a grid of data which is bound to this GridData property but the column which should be bound to the Message field from the ChatMessage class is blank and the fields found in the MessageParentBase class are populated.
So I presume there is an issue with the view not knowing to cast up to the ChatMessage from the MessageParentBase class.
Can I inform the view that the objects will be of the type "ChatMessage".
I did try moving the property declaration up to the inherited viewmodel as
public ObservableCollection<ChatMessage> GridData
{
get { return _GridData; }
set { _GridData = value; }
}
but this gives me the following error:-
Cannot implicitly convert type 'System.Collections.ObjectModel.ObservableCollection' to 'System.Collections.ObjectModel.ObservableCollection'
Do I need to cast at the view level or can I change the viewmodels to implement this better?
Any suggestions would be greatly appreciated.
Emlyn
Change the collection to this:
public ObservableCollection<MessageParentBase> GridData { get; set; }
then add into your constructor
this.GridData = new ObservableCollection<MessageParentBase>();
Since WPF uses reflection to retrieve bound data from the data context it should be able to get the values of the derived classes stored in that collection.
Also when you run your application check the output window with Debug selected, the XAML engine will output any binding errors there.
Your ViewModel should contain a list with the type that your grid will show (in this case, the ChatMessage type). You can still use the inheritance to call common methods, but the binded list must be of the ChatMessage type

Exposing collection methods when creating a custom collection

I have to develop a custom collection of objects. The reason is two fold, I need to be able to assign an internal name to the collection and the collection also needs to implement some abstract methods to be treated like any other entity that I have.
So I created an EntityList class. Below is a snippet of the class. It contains a id and a list of entities, plus a bunch of methods. My question is this, so far I have put in the list management methods that I require, such as Add, Insert, Remove and Clear. If you have an EntityList reference called myEntityList you could perform something like myEntityList.Add(newEntity). I do like this approach, but really these methods are just handing off the work to the list. I could also not implement any of these methods and you could perform the same action as above by using myEntityList.Items.Add(newEntity). However, here you are directly accessing a method of a property of the object. I wanted to remove the Items property altogether, however I often need to iterate through the list using a foreach and for that I need access to the actual list.
Here is my class definition, it does not have the overrides to the abstact methods included.
class EntityList
{
String entityId;
List<EntityBase> _entities;
public EntityList()
{
_entities = new List<EntityBase>();
}
public List<EntityBase> Items
{
get { return _entities; }
//set { _entities = value; }
}
public void Add(EntityBase entity)
{
_entities.Add(entity);
}
public void Insert(int index, EntityBase entity)
{
_entities.Insert(index, entity);
}
public void Remove(EntityBase entity)
{
_entities.Remove(entity);
}
public void Clear()
{
_entities.Clear();
}
}
Am I violating some cardinal rules here? How should I manage the list when it is a member of another class?
Just inherit from List<EntityBase> then you won't need to redeclare and implement the list methods.
i.e.
class EntityList : List<EntityBase>
{
String entityId;
//add your extra methods.
}
you should make your class implement IList<EntityBase> (or at the very least, IEnumerable<EntityBase>) this way, you can treat it just like a "normal" list. Before you do this, though, you should probably read the documentation and decide which is best for your needs.
MSDN on IList is here
MSDN on IEnumerable is here

How to extend abstract Entity class in RIA Services

I want to add a bool variable and property to the base Entity class in my RIA services project so that it is available throughout all the entity objects but seem unable to work out how to do this. I know that adding properties to actual entities themselves is easy using .shared.cs and partial classes but adding such properties to the Entity class using similar methods doesn't work.
For example, the following code doesn't work
namespace System.ServiceModel.DomainServices.Client
{
public abstract partial class Entity
{
private bool auditRequired;
public bool AuditRequired
{
get { return auditRequired; }
set { auditRequired = value; }
}
}
}
All that happens is that the existing Entity class gets totally overriden rather than extending the Entity class.
How do I extend the base Entity class so that functionality is available thoughout all derived entity classes?
You won't be able to add a property to the Entity class. This class is already compiled and cannot be modified (partial classes only work because your have the source code of the class in your solution and the code can be merged at compile time).
One option may be to create a class that inherits from Entity, then add your property in this class, and have your entities inherit from your custom class instead of Entity. This might not be practical for use with designers, though.
public class MyEntityBase : Entity
{
private bool auditRequired;
public bool AuditRequired
{
get { return auditRequired; }
set { auditRequired = value; }
}
}
public class Entity1 : MyEntityBase
{
}

Resources