About Me

Wednesday, December 29, 2010

Why abstract method can't be private

Not so far ago I had to use abstract class for one business implementation.
I quickly typed the construction of abstract class and then found that the intelisense mark some code as invalid:
My first question was "Why?!".
Why abstract method can't be private ?

Here is the answer that came into my brain in the next second:

Abstract clause means that the method should be overriden in the classes that are inheriting from the base class.
In this case, each inheritor from Foo class should implement Method1, Method2 and Method3
Inheritor, can see only public and protected members of the base class. 
So, there is no way to implement Method3 because it marked as private.

No need to remember all the rules about all that OOP tricks in .NET, its only need to use your brain and ask "Why?" at the right moment. There is always logical explanation for it.

Monday, December 13, 2010

"Could not load file or assembly 'Castle.Core, Version=1.0.3.0 ...' or one of its dependencies...." issue

Today I got this error during configuring IoC container based on Windsor library.

We are working as team on this project, and when i got this error, i started to think that somebody mistakenly changed the version of the library.
After review, i found that all libraries has correct version. We are already using version 2.5.1.0 of that library and there are no places in the project's code or configuration files where it may referense to the old version of Core library.
I had to spend an hour to realize that the problem is not with version of the library but with components configuration.

We had to use forwarded types and some namespaces was specified wrong.
So, I've just changed a namespace for IReportRepository and IUserRepository to the correct one and it started working properly

Wednesday, December 1, 2010

How to write unit test for DateTime.Now

Yesterday I tried to use test driven development (TDD) to create logic for the application I'm working on.

One of requirements was to make the module to set the date when the object was modified.
The object has a property UpdatedOn and i needed to write a test that checks setting of DateTime.Now value for it.

The problem is, regular solution like the one below, will not work:


Assert.AreEqual(DateTime.Now, someObject.UpdatedOn);

The reason of why that example will always failed, is because the result of execution of DateTime.Now in Assert and DateTime.Now in business logic will be always different.
There will be few miliseconds between them and the test will always failed.

Here is what i used instead:


Assert.That(someObject.UpdatedOn, Is.EqualTo(DateTime.Now).Within(1).Seconds);


Get closest Monday SQL function

From time to time I have to build reports for the application I'm working on.
Today they asked me to build the report that can accept any date and build results within Monday to Sunday.

The problem was, how to find date of Monday, since the user may enter any week date.
So, for instance, if the user enters '12/03/10' , for instance, that Friday, the system should find date of Monday '11/29/10'. And so on...

I wrote small sql function that helps to get it:

if exists (select * from sysobjects where name = 'GetMonday')
    drop function GetMonday
go

create  function [dbo].[GetMonday](@date DateTime)
-- Returns closest Monday date that going before passed date
returns datetime
as
begin
    declare @datePart int
    set @datepart = datepart(weekday, @date )

    --in case if sunday treats the system as first day of the week,
    --convert passed sunday date into the last day of the week
    -- 12/26/10 was sunday, so we are testing first what day of the week it returns
    if (datepart(weekday, '12/26/10') = 1 AND @datepart = 1)
    SET @datepart = 8

    return dateadd(dd, (@datePart - 2) * -1, @date)
end
go

select dbo.GetMonday(getdate())

Monday, November 22, 2010

How to find usage of sql within the database

Have you ever was in that situation as I do from time to time ?

  • Remember the column or table in the database, but don't remember, where its exactly using and how ?
  • Trying to  remember all stored procedures, functions or triggers that uses  some text, not matter its sql word or some name?
  • Aasking yourself something like "A month ago, i wrote the procedure that has 'hallo world' text in it. What's the name of it?"

I'm falling in this situation quite often so decided to wrote something to make my life easier.
What I have now is small script that's actually kind of full-text search within database's programmability objects (procedures, function, triggers and so on).

Here is it:

declare @textToSearch varchar(max)
set @textToSearch = '%EmailLog%'

declare @total int;

with t (
    rownumber,
    name)
as
(
    select
    
    ROW_NUMBER() OVER (ORDER BY so.name) as rownumber,
        so.name 
    from
    
    syscomments sc 
        inner join sysobjects so on so.id = sc.id 
    where text like @textToSearch
)
select @total =count(*) from t

declare @currentPos int
set @currentPos = 1

declare @name varchar(max)

while (@currentPos <= @total) 

BEGIN 
    with t ( rownumber, name) as ( 
        select 
            ROW_NUMBER() OVER (ORDER BY so.name) as rownumber, 
            so.name 
        from
            syscomments sc 
            inner join sysobjects so on so.id = sc.id 
        where text like @textToSearch 
    ) 
    select @name = [name] from t where rownumber = @currentPos 
    Print '----------------------------------------------------------------------------------------------------' 
    Print @name Print '----------------------------------------------------------------------------------------------------' 
    EXEC sp_helptext @name; 
    Print '----------------------------------------------------------------------------------------------------' 
    set @currentPos = @currentPos + 1 
END 
go


Enter what are you looking for to the @textToSearch variable, between two '%' in example above.

In my example, i tried to find all places (stored procedures, functions, triggers and so on) that has text 'EmailLog'

To make result more readable, switch output view from Grid to Text. Use Ctrl+T or menu Query -> Results To -> Results To Text.

Just run it and  after a ninebt you will have text of each procedure or function that contains specified word

Thursday, November 18, 2010

Maximum length of Excel worksheet name can't be more then 31 characters

Today I was faced with very weird issue.
In our programm we are generating Excel reports.
There are many page in it. 
Each of the page is describing its own location.
Location names are storing in the database and putting to the Excel document as the name of the sheet.

Today, people started  complaint that they can't open the file.
During opening, Excel show very strange exception about incorrect format of the file:


The content of the log file told me, that the content of the file is wrong, but did not tell me where exactly is the issue. So, i had to start searching  it by myself.
I found that nothing has changed in the database except of  few new locations were added.
After comparing that locations with other that was added earlier, we found, that few of them are very long.

I had to cut the name of the location to 31 characters to solve the issue.



If you editing Excel file manually, Excel does not let you to type more then 31 characters in the name of the sheet. But, if you creating document dynamically, as we do with some .NET component, its easy to avoid this limitation and enter any length you want.

So, even in Microsoft Office 10, the name of the Excel sheet has so huge limitation.