Who is Ali Özgür?

RecentComments

Comment RSS
aliozgur posted on May 18, 2009 16:46

Why?

Some time ago while I was working on a project, that was a top secret project so can not give more details Smile,  I realized that I've produced some sort of weird code that checks if an interval (Start,Stop integer value pair) intersects with another interval. Right after unit testing and commiting the code I felt like there is something wrong wih me. Here are the details

Nothing Fancy

Here is the code of my Interval structure. There is nothing fancy about this structure it is used to hold two integer values and performs range checking in the constructor to guarantee that start value is always smaller or equal to stop value.

public struct Interval
{
private int _startValue;
public int StartValue
{
get { return _startValue; }
private set { _startValue = value; }
}

private int _stopValue;
public int StopValue
{
get { return _stopValue; }
private set { _stopValue = value; }
}

public Interval(int startValue, int stopValue)
{
if (startValue > stopValue)
throw new TypeInitializationException("Interval",
new Exception("Provided start value is greater than the provided stop value."));

_startValue = startValue;
_stopValue = stopValue;
}

public bool IntersectsWith(Interval interval)
{
//TODO: Check if this intersects with the provided interval
}
}

 

Conventional Way

Conventional way of implementing that IntersectsWith method is to 1) write some if/else blocks or 2) to combine a single return statement  to cover all of the cases illustrated on the following image

My Problem

But somehow I did not choose the conventional implementation and I decided, in fact by reflex, to re-model Interval objects as rectangles with 1px in height , place them on xy coordinate system and check if two rectangles intersect or are tangent to each other. Here is my weird IntersectsWith implementation

 

public bool IntersectsWith(Interval interval)
{
  Rectangle r1 = new Rectangle(StartValue, 0, StopValue - StartValue, 1);
  Rectangle r2 = new Rectangle(interval.StartValue, 0, interval.StopValue - interval.StartValue, 1);

  return r1.IntersectsWith(r2) || (r1.X + r1.Width == r2.X) || (r2.X + r2.Width == r1.X);
}

 

Questions to myself

  • Is this weird implementation is a result of too much analytical thinking?
  • Is this weird implementation is a result of too much abstract modeling I have to do to perform my job well?
  • Shall I see a therapist?
  • Is this weirdness a common pattern among developers?
  • Shall I ask this as an interview question? And what shall I do with people implementing this method like me and not like me?
  • How will my colleagues feel when they have to read my wierd IntersectsWith implementation?
  • Shall I be ashamed of myself? 

Code

WeirdIntersectsWith.rar (23.00 kb)


Posted in: .NET Development , General Programming   Tags:

I'm currently working on some small projects and an enterprise level case study project where I decided to use SubSonic which is a lightweight open source ORM for .NET led by Rob Conery. I can say that SubSonic is really lightweight and is really easy to use, but as might be expected has some minor configuration overhead. SubStage developed by Eric Kemp is a must have if you are using SubSonic, but this tool lacks a very important feature IMHO. All your subsonic related project configuration is kept inside a predefined MasterStore.xml file which means you only have a single file for all your subsonic projects. In this scenario it is not easy/handy to attach your substage configuration to your project source code.

To overcome this problem I modified SubStage source code (built against SubSonic 2.2) and added "Load Master Store" and "Save Master Store As" File menu items. I posted the patch to Eric Kemp but I do not know if the patch will be applied in newer version so I publish both compiled SubStage and the Subversion Patch file.

Downloads

SubStage_2.2_With_MasterStore_LoadSave.rar (1.69 mb)

SubStage_Patch_AliOzgur.patch (242.82 kb)


Posted in: .NET Development , C# , SubSonic  Tags:
aliozgur posted on January 30, 2009 13:10

The sample Ruby code has no syntax errors. What does the code through lines 3-5 mean? And what is printed to the screen/console?

Any Suggestions ? Smile

 

class Sample
  attr_writer :name
  attr_reader :name do
     "Mr. " + @name
  end
end

s = Sample.new
s.name = "Ali"
puts s.name

 


Posted in: General Development , Ruby  Tags:
aliozgur posted on January 26, 2009 09:11

Based on my previous article Artem Smirnov[^] posted a question about how to test a Repository(DAL) method in a project using Nhibernate as ORM. Here is his question

"I wanted to unit test a similar, but more common, problem: a Repository method. I.e., create a test Order for a particular date, and test that the FetchOrdersByDate method returns this order if the date matches. My guess was that I could just create an Order, attach it to the session without saving it to the database, then somehow stub the database and make NH fetch it from the cache. After doing a lot of search, I discovered that this is impossible, so I had to hit the database for every test. Given that NH looks extremely flexible, i.e., Interfaces everywhere, this is kind of strange.."

Here is my answer

The problem related to testing a Repository method is a very common one and people suggest different solutions to this problem. Here are some of them

* Mock your Repository method by using a Mocking library or by hand. But this suggestion is not valid all of the time. If you have native SQL or use NH Transformers to produce DTO's mocking is not an option.

* Use an in-memory database like SQLite for your tests. But if you have native sql or develop against a legacy database or use part of a legacy database you can not follow this approach

* Take script of your production database and create an empty test database. Run your tests against the test database. This method also have some drawbacks, to name few, tests take longer to run, you have to keep your test database up to date with the live one, you have to deal with some phantom objects not directly managed by your domain ( for example your domain contains Instructor class just for integrity reasons and you do not have code to deal with Instructor instantiation because you do not actually need this piece of function. But somehow you have to create Instructor objects for testing). You can solve the first two problems with Continuous Integration, but the later can be tricky to solve. However this last approach has one big outcome, that is, you will likely have failing tests if something goes wrong with your database.

I personally tend to follow the last approach for NH specific testing and write pure unit tests otherwise. 


Posted in: .NET Development , C# , NHibernate  Tags:
aliozgur posted on January 16, 2009 08:38

Download NHibernate.Caches.Testing.zip (24.75 kb)

Motivation

It has been a long time since the last time I dropped a new entry in my blog. I was very busy with our new project and at last we finished coding and moved to Acceptance Testing phase. That new project was a little bit challenging from architectural point of view. Some challenges to name were

  • We had to develop against a legacy database
  • We had to replace an existing system with a new one, it was a little bit problematic process to introduce some new concepts
  • Our system was designed targeting a small part of the legacy database, hovewer that part was accessed by some external processes/systems bringing some synchronization issues.
  • We placed very strict code coverage and testing goals

In this blog entry I will try to share a very specific problem, writing good tests for NHibernate Level2 caching related functionality, we experienced and how we solved that problem.
More...


Posted in: .NET Development , C# , NHibernate  Tags:
aliozgur posted on September 22, 2008 12:07
During evolution of .NET related technologies (almost lasted a decade) Microsoft published bunch of experimental technologies, frameworks and packages which caused developers to feel a little bit confused. This confusion made developers feel uncomfortable about the future of these technologies and we simply preferred, to the extent we can survive, not to use new .NET related stuff. We had the feeling that we were fighting to find our way out of a huge dust cloud, I think even Microsoft felt a little bit exhausted and as a result was late to set a clear vision for all these technologies. But with the release of .NET framework 3.0 and 3.5 it seems that Microsoft managed to settle down a clear vision about the future of .NET development. I think next decade (by 2010) we will talk/hear about and develop software based on the following .NET related technologies.

Posted in: .NET Development  Tags:

Here is aother strange problem related with NHibernate

The Problem

I have a Parent class and two child classes Child1 and Child2 mapped to different tables on the database.
Lets assume that we specified cascade='all' for child bags defined on Parent.hbm.xml. Sample workflow of instantiating parent and child objects is as the following

- Create a parent object.
- Insert 2 Child1 instances to child1 bag
- Insert 3 Child2 instances to child2 bag.
- Flush the session
- Refresh parent object
- We get 6 instances for each child bag (child1 and child2). But we expect 2 Child1 instances in child1 bag and 3 Child2 instances in child2 bag.

Ther problem is : NHibernate performs left outer join on Child1 and Child2 tables when Refresh is called for the parent object. This is unaccaptable, I think NHibernate should initialize child collections with seperate selects commited to the database, or may be distinguish the duplicated child instances automatically in the collections. (using idbag instead of bag is not an option)

Download the test case

Requirements

Watch This


Posted in: .NET Development , C# , NHibernate  Tags:

I discovered this problem while developing a new system with the latest NHibernate.Burrow distribution (which in turn uses the latest NHibernate distribution).I spent some time Googling around to check if anyboy else met the same problem and found some entries but none of them specified exactly why this failure was happening and how we can solve this problem.
More...


Posted in: .NET Development , C# , NHibernate  Tags:

I've already developed some projects with NHibernate and I can say that NHibernate is a real time saver. But I must admit that it takes some time to get used to NHibernate. As an ORM NHibernate has fantastic features but if you want to develop successfull projects with NHibernate you must know that NHibernate is only one part (ORM) of a successfull layered architecture. In the attached solution I tried to give you the basic idea of a layered architecture and how NHibernate can be used within such an architecture

Attached solution includes the following projects

  • Core: Our domain objects (entities) reside in this layer. Also DAO (Data Access Objects) interfaces are defined in this layer.
  • DAL (Data Access Layer): Default implementation of DAO interfaces reside in this layer.
  • BLL (Business Logic Layer): Business logic code resides in this layer.
  • Common: Some utility calsses reside in this project
  • Tests.Disconnected: Includes unit tests for domain objects and business logic. Notice that unit tests in this project do not require domain objects to be persisted (NHibernate behaviour will not be tested), so the need for a database connection is eliminated which in turn makes our unit tests fast. Notice how we replaced our default DAL with mocked one.
  • Tests.Connected: This project includes unit tests too, but this time we want to test how our domain objects and bussiness logic perform NHibernate. NHibernate provides very cool features like native sql, lazy loading and cascading and we will likely want to test how our domain objects behave when armored with these cool features of NHibernate. It is also very likely that we will have some mapping errors (typos likely) in our Core, these tests will help us catch these errors. Performance bottlenecks possibly caused by our NHibernate mappings (for example we may discover that we need to make a child collection to be lazy loaded) can also be identified with help of these tests.

In the sample solution you can also find a simple usage of Castle Windsor Inversion of Control (IoC) container. We use IoC to be able to load different implementations of our DAL (DAO implementations). 

Another very important point you need to understand really well is session management of NHibernate when used in web applications. Thanks to Burrow contribution project this task is made very simple, you do not even need to write single line of NHibernate session management code. You only need to inherit your DAO implementation classes from GenericDao<T> class and you are ready to go. 

Additional Notes

  • Database script is included in Tests.Connected project under DBScript folder. 
  • Sample project uses NHibernate  2.0 Aplha1 and NHibernate.Burrow is also Alpha1.
  • ASP .NET MVC Preview 3 to run Sales.MVC sample
  • TestDriven .NET to run NUnt tests

Suggested Readings

Downloads

Update History

  • 09 June 2008
    • Castle files under Libs folder updated to Castle RC3
    • SQLite references removed
    • TestFixtureTearDown override of CustomTestBase class in Sales.Tests.Connected assembly commented out

Posted in: NHibernate  Tags:

GoF book says that "Observer pattern should defina a one-to-many dependency between objects so that when one object changes state, all its dependenst are notified and updated automatically". Subscribing to RSS feeds is a nice analogy.  You subscribe to RSS feeds to show interest and you become an observer who demand for notification and RSS feeds become the subject and are responsable for providing information to all subscribers. I think this bit of information describing the pattern is enough, now lets see how we implement observer pattern.
More...


Posted in: C# , GoF Patterns  Tags: