Archive for the tag '.NET (C#)'

I have also lost my virginity (and you should too)

UPDATE: I have lost my virginity in regards to attending my first software conference, just in case that wasn’t clear… you know who I am talking to.

I just read Brian Mavity’s latest post and am obviously rowing the same proverbial boat that he is. This is not only my first software development conference but the first software development conference our company has ever sent anybody to. I personally have already taken more away from this than I ever imagined. Just sitting around at dinner or socializing with the same people whose blogs I follow is somewhat intimidating, but they are all very welcoming and want to help us new guys get going. I have had conversations (okay, mostly absorbing) with Ayende, Chad Myers, and Jeremy Miller. I have also been able to speak one on one with others and will most definitely get the opportunity to do some more.

Same as Brian Mavity, if you your company does not care enough about their development team to spare a little time and money to get you involved, you should consider finding a place that does or making your voice heard better to accomplish this.

I am really looking forward to the next few days worth of activities.

KaizenConf Workshops – Advanced NHibernate

As some of you know I am attending KaizenConf for the next few days. I am going to use my blog as my notepad as I take notes during different workshops and sessions. My note taking skills sometimes have a lot to be desired so be patient if these notes look very sketchy. I will try my best to come back through and make sense of them later.

Advanced NHibernate

I am currently sitting through Ayende’s workshop on Advanced NHibernate and there is some really good stuff going on here. Three of the topics I have found most interesting is Multi-Tenancy, Full text indexing with NHibernate Search, and caching with NHibernate. NHibernate search is a project that combines Lucene, a proven java index tool, and NHibernate to alleviate strain on the database engine by keeping indexes on disk and performing queries on that data faster. Indexing is accomplished by decorating entities with attributes. Of all these topics caching with NHibernate has really grabbed my attention as it probably has the greatest results for the applications I am currently working.

Caching with NHibernate

1st Level cache –Contains Identity Map – Session – Single instance of an object in a particular session

2nd Level Cache – contains the SessionFactory – lives for the entire application lifetime, it also has 4 levels of cache within it:

timestamp
query cache
collection cache
entity cache

 

Caching Configuration

Enable query cache <property name=”cache.use_second_level_cache”>True</property> in NHConfig – A single on off switch in the application to enable/disable caching is not a good solution because caching is complex and could affect business rules and performance. So you have to enable caching for a specific entity by using Cache_usage = CacheMode (ReadWrite for Ayende’s samples) in the entity mapping file. The NHibernate cache hashtables are stored like below:

Entities Cache:
————————-
Blog#1 : { Name: “…”}
Post#1 : {…}
Post#2 {…}

Collection Cache
—————————
Blog#1.Posts { 1,2,3,4,5,6} – stores id’s only

Query Cache:
—————————
“select top 5 * from Post” : {1,2,3,4} // stores id’s only

Ayende used his blog sample for all of the code examples today. When he put caching on the Blog, and the collection of posts in the blog map and on the post entity the fewest calls to the database occurred. To enable query caching the property cache.use_query_cache = true in the config file.

Session.Load vs Session.Get

Load == I know the entity exists in the database, it returns a proxy to the object. Throws an exception if the object is not lazy loaded and cannot be found.

Get == NH really goes to the database and fetches a row or throws an exception if it does not exist.

Which one of these to use depends on the business rule for the entity.

 

NHibernate Contrib project descriptions

NHibernate cache libraries:

PrevelanceCache – persistent caching for smart client applications

SysCache – using ASP.net caching – gives it the ability to put things in the cache and forget about it – only works if you have one machine.  Other wise you get cache poisoning.

SysCache2 – ASP.net caching in the farm – maybe application is not the only thing that updates the database. This is good when the application is not the sole owner of the database.

MemCache – a distributed hashtable, thats it basically it is distributed caching.– Server sitting on TCP with a hashtable – the Client caches the key and always goes back to same server in farm for cache retrieval. Very simple, works very well. This is also known as MemCacheD and is the same caching strategy that Facebook uses.

SharedCache – SharedCache from CodePlex

Velocity – From Microsoft – more complex than memcache, more options but they validate the cache space you have.

 

Other NHibernate Contrib Projects

NHibernate.Linq – pretty good, handles most of the common scenarios, does not handle the crazy scenarios that are capable in LINQ. Currently working on a fully functional LINQ library.

NHibernate.Validator– Just as you can specify constraints in SQL Server. You can specify constraints in your mapping file and the validator will validate that for you.

Spatial – GIS applications

Shards – partition the data across multiple db’s – written by Google to deal with problem of having multiple database. You can teach NHibernate a sharding strategy so that it knows which database to use. This is a strategy that is used by companies such as Facebook.

 

Another very interesting topic Ayende talked about is self optimizing queries. This would be used by session.Future<TYPE>() you could use this in your repositories on all lists/CreateCriteria methods. This creates a MultiCriteria which returns multiple result sets in one database trip. This is very similar/exactly like multiple result sets in stored procedures.

ALT.NET a College Curriculum?

There has been a lot of discussion recently on the ALT.NET list about how the ALT.NET way is so hard and complicated that it may be equivalent to a college computer science curriculum. This all started as a discussion that was geared towards building some type of cascading curriculum guide to ALT.NET software engineering principles such as TDD, S.O.L.I.D, continuous integration, unit testing, composition over inheritance, etc… I agree with this totally, We need to make some type of curriculum guide to ALT.NET principles. I am fairly new to ALT.NET and I am also fairly young. Where I work there are only two developers and we are also testers/designers/everything so when we decided that we could write better software by following the ALT.NET principles we had a hard time finding a starting place amongst all the available information. It is simply overwhelming when you are first starting, and I know we have been talking about easing the entrance barrier, but it’s just not that easy. There has to be some difficulty or is the reward is not as great in the end. However, I do believe that some type of curriculum guide would be a great tool for everyone involved with ALT.NET at any level. I think the best thing about this chart is just to show how the different principles build on each other and relate to each other so you have a reference point you can find yourself on.

Now back to the discussion on Computer Science (CS) curriculum in colleges, I graduated from a small college in Oklahoma with a degree in CS a few years ago. I personally gained a lot from college because I had a professor who had previously worked in the real world, and I worked in the real world developing software while in college so we could have intelligent conversations that only a few other students understood. This was great for me, a self motivator, but not for everybody else. I was able to demand the one on one time that I needed and to ask questions and have problems that forced the “super” students into thinking about problems other than writing a counter in a nested loop. I have now been asked to teach some software courses at the college and help, along with other alumni to revamp the curriculum that is taught in the school. In January I will begin teaching two semesters worth of ALT.NET curriculum. This will be a huge leap ahead for the students, but also for the faculty as I feel so much college faculty is out of touch with the real world anyway.

Ultimately though I think what college is really designed to do is teach students HOW to think and HOW to learn since a college cannot be expected to keep up with all the latest and greatest trends in Software engineering but it can teach students some higher level principles such as ALT.NET or at least get them started down the right path to learning ALT.NET.

So many doors, so many options, what a headache…

My head has finally stopped spinning enough that I can finally write this post. Before I dig into it though let me give some context. A few weeks ago a post by Kyle Baley shows up in my feed reader which is in response to a post by Roy Osherove. Now both of these posts were on the subject of whether the entry barrier to Software Development is to high. More specifically though these posts were directly targeting TDD, Agile, Mock Objects, Inversion of Control, Dependency Injection, NHibernate, etc… Just imagine for a minute you are in a round room with doors all around you. Each door looks the same and has a small yellow light hanging above it. Now, you have to pick a door, but which door do you pick? But You can peek into the door and see whats behind it without fully understanding whats in there, but that just makes your decision even harder. That’s kind of how I have been feeling the past few weeks trying to get my head wrapped around these things. With so much information out there about these technologies you may argue that it is not hard learn any one of them well. I agree with that, in fact I am not going to try to teach you about these technologies unless I find a gaping void somewhere. The complex part comes when you are trying to learn about these technologies together and/or how they are linked to each other. This all started from a conversation I had with Colin Jack  in reference to the above posts, which by the way were not the only discussion in blog land on the topic, we decided a road-map of some sort would be a good idea for people that are new to these “New Era Development Tools”. 

A disclaimer before we begin though. I am not an expert at any of these technologies, as such I welcome comments, criticism and just good ole’ corrections for anything I post that may be misleading or wrong. What I am though is a software developer that wants to understand and learn these technologies and principals and put them into practice developing software. So again, a little background. I am a Developer/Project Manager/many more hats for a small manufacturing company in Oklahoma. Our development team consists of 2-3 people at any given point. We have successfully developed and deployed several line of business applications over the past several years using C# and Rhockford Lhotka’s CSLA Business Objects. Now I know a lot of people embracing these new technologies don’t like CSLA but it works for us and the new version of the framework is going to play even nicer with these new technologies. Two months ago I didn’t even know what TDD (Test Driven Development) stood for let alone how to do it. But I got some books, did some research and I understand WHY we should do it and as awkward as it seems we are putting it in practice on a daily basis now.

This will be the first in a series of posts where I show you the “Roadmap” that I am taking to learn these technologies and make them work together. As I said above, I am by no means an expert on these topics, but I am learning and hopefully you will be learning as well. I will start at the bottom of these Building Blocks and work my way to the top. I may also add more blocks in the middle somwhere based on things learned on the way up.

CAB & SCSF: Maintaining Active View across Multiple workspaces

Recently I ran into a problem where I have multiple windows open in my application at once. This is great for presenting multiple views to the uesr at one time, but it is not so good for a developer trying to figure out what to do when a common toolbar command is clicked. I found some help on the internet by Chris Holmes who had come across a similar problem and thus created a solution. See: Chris Holmes - CAB: Solving the Active View Problem Chris’ solution however was only good for watching one workspace at a time. And my problem was that if I switched between MDI tabs in the MDI workspace everything was fine. But if I switched from a tab in the UltraMDITabWorkspace to a pane in the UltraDockWorkspace. The smartpart activated command never fired.

So I posted my question over in the SCSF forum and Mariano Converti replied back with his solution. Now this was great this is exaclty what I was looking for. With a little tweaking I could make this work. One problem though: Marianos solution relies on getting the workspace as a Control. Now this is great for all of the out of the box CAB Workspaces. I however am using Infragistics Workspaces, which do not derive from System.Control. So what I have here is the first of a series of posts on this subject. The code below is a service that I created in Infrastructure.Library. I will also post the interface for the service and a sample implementation. I am also going to be posting a modified version of Chris Holmes UIService that manages toolbars and menu items for an infragistics ribbon.

ActiveWorkspaceViewService.cs


[Service]
public class ActiveWorkspaceViewService : IActiveWorkspaceViewService
{
[EventPublication(EventTopicNames.ActiveViewChanged, PublicationScope.Global)]
public event EventHandler<ActiveViewChangedEventArgs> ActiveViewChanged;

IUIService _uiService;

[InjectionConstructor]
public ActiveWorkspaceViewService([ServiceDependency] IUIService uiService)
{
_uiService = uiService;
}

#region IActiveWorkspaceView Members

/// <summary>
/// Registers a Workspace to be monitored for changes such as SmartPartActivated and
/// then watches the controls for the OnEnter event
/// </summary>
/// <param name="workspace"></param>
public void AddWorkspaceToMonitor(IWorkspace workspace)
{
workspace.SmartPartActivated += new EventHandler<WorkspaceEventArgs>(OnSmartPartActivated);

if(workspace.ActiveSmartPart != null)
ActiveView = workspace.ActiveSmartPart;

}

private object _activeView;

/// <summary>
/// Returns the active SmartPart as an object
/// </summary>
public object ActiveView
{
get { return _activeView; }
private set
{
_activeView = value;
_uiService.SetActiveView((IView)value);
OnActiveViewChanged(new ActiveViewChangedEventArgs(((IView)value).Guid));
}

}

#endregion

/// <summary>
/// Handles the SmartPartActivated event for the Workspace
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnSmartPartActivated(object sender, WorkspaceEventArgs e)
{
if (e.SmartPart != null)
{
ActiveView = e.SmartPart;
Control c = e.SmartPart as Control;

c.Enter -= new EventHandler(OnEnter);
c.Enter += new EventHandler(OnEnter);

#if (DEBUG)
Console.WriteLine("SmartPart Activated: " + c.ToString());
#endif
}
}

/// <summary>
/// Provides a method of setting the smart part to the "Active"
/// SmartPart in the UIService by handling the OnEnter event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnEnter(object sender, EventArgs e)
{
Control cntrl = sender as Control;

if (cntrl != null)
{
ActiveView = cntrl;
#if (DEBUG)
Console.WriteLine("SmartPart Entered: " + cntrl.ToString());
#endif
}

}

protected virtual void OnActiveViewChanged(ActiveViewChangedEventArgs eventArgs)
{
if (ActiveViewChanged != null)
{
ActiveViewChanged(this, eventArgs);
}
}

}

IActiveWorkspaceViewService 

This is the interface for the class above.

[c#]
public interface IActiveWorkspaceViewService
{

void AddWorkspaceToMonitor(IWorkspace workspace);

object ActiveView { get; }

}
[/c#]

Now to use the service you need to register your workspaces with the service when the application starts. Make the following changes in your ShellApplication.cs file:

Put the following services in the AddServicesMethods()
[c#]RootWorkItem.Services.AddNew&amp;amp;amp;amp;amp;lt;ActiveWorkspaceViewService, IActiveWorkspaceViewService&amp;amp;amp;amp;amp;gt;(); [/c#]

The next bit of code needs to be placed in the AfterShellCreated function. This will actually register the workspace for use in the service.

[c#]/// Add the workspaces to the ActiveWorkSpace Monitor
/// This will ensure that we always know the active smartpart even
/// if switching between different workspaces
///
_activeWorkspaceService = RootWorkItem.Services.Get&amp;amp;amp;lt;IActiveWorkspaceViewService&amp;amp;amp;gt;();
_activeWorkspaceService.AddWorkspaceToMonitor(RootWorkItem.Workspaces[WorkspaceNames.MainContent]);
_activeWorkspaceService.AddWorkspaceToMonitor(RootWorkItem.Workspaces[WorkspaceNames.MainDock]); [/c#]

Like I said earlier, this is just one piece of the puzzle to make dynamic toolbars, menus and commands work across your entire application. I will post more details, source code, and a complete sample project soon.