January 29, 2010 Book Reviews No comments
January 29, 2010 Book Reviews No comments
Domain Driven Design by Eric Evans is one of the books that every architect must read if he wants to develop really extensible enterprise level application, which absorbs knowledge of the problematic domain.
Book talks about Ubiquitous Language which is the language between Domain Experts and Developers and helps them build model of the application.
Once you have this language you could easily start build your model with Entities and Value Objects, making Associations between them and other elements of the system. Then you use Services to apply operations on your domain. To interact with Infrastructure you need Repositories and Factories, this makes Persistace Ignorance Layer to hide and isolate your model from technical impacts of your infrastructure.
Refactoring Toward Deeper Insight plus Distilling of model takes your system to the breakthrough. If you have clear model, future Developers could easily work with it addin new functionality and speaking with Customer on common language they will know it, because code raises it to them.
This is one of most valuable book I’ve ever read!
January 29, 2010 Errors, NHibernate No comments
Error:
I had something like this in the SaveCustomer method:
And this don’t work correctly. Simple change to
resolved my issue.
January 27, 2010 Deployment, Errors 2 comments
I got error which says “MyApplicationName has stopped working” once deployed simple tool application to the deployment machine which is MS 2008 Server.
But application works just fine on my Dev Machine.
I took a look on event viewer and it says:
Problem signature:
P1: myapplicationname.exe
P2: 1.0.0.0
P3: 4b607991
P4: MyApplicationName
P5: 1.0.0.0
P6: 4b607991
P7: 83
P8: f
P9: System.IO.FileNotFoundException
P10:
It looks like something I was referencing is something in the framework that wasn’t installed on the server.
But what?
I remembered that when worked with UI I inadvertently took some thing from ToolBox. I immediately removed that thing, but REFERENCE were left in projects references.
It was Microsoft.VisualBasic and Microsoft.VisualBasic.PowerPacks.Vs.
Moral:
Once you get errors on deployment machine check carefully if you haven’t referenced something that is not needed. And remember about Event Viewer.
January 27, 2010 .NET, Frameworks, MEF 4 comments
MEF is the Framework which allows you to load extensions to you application easily. It does discovery and composition of parts you need to be included in your application at run-time. You could extend your behavior simply with adding new Plugin. Managed Extensibility Framework will do everything for you.
Hello MEF World!
Assume we have really simple application and we want it to say “Hello MEF World!“:
That is absolutely how this look and we want to make this work. The main issue is to have instance in property ProgrammGreeter to be real instance of Greeter.
Lets accomplish this with MEF
In order to do this we need to include reference to System.ComponentModel.Composition.
Accordingly to MSDN: “MEF is an integral part of the .NET Framework 4 Beta 1, and is available wherever the .NET Framework is used. You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET. In addition, there are plans to add MEF support for Silverlight applications.“
Most likely MEF will be included into .NET Framework 4.0, but for now we could download it from codeplex site here.
Once we added reference we could add Import and Export attributes.
Export stands for exposing some capabilities and Import stands for dependency on some other capability.
Our Greeter provides some capabilities so we add Export there:
We want to use that capability in our Programm class, we depend on that functionality. Add Import:
Usually our capabilities lives in other asseblies, but also they could live in called assembly. Anyway we need to say how to compose all our dependencies to MEF.
This could be done with method Compose which we be calling before we are using capabilities, usually at the start of app:
I ran my application and got:
How does Managed Extensibility Framework work?
I think that you are bit concerned/confused regarding that Compose method. Just read comments in code of Compose, which I made more detailed:
In Managed Extensibility Framework there are primitives called ComposablePart which holds extensions to your application grabbed from Catalog. But also it might be that they have Imports which also needed to be composed.
Take a look on this picture from Managed Extensibility Framework wiki page.
More complex Example
In this example we will have application which parses XML file (contains information about some developer). Then when we want to deploy our application to Clients we want that XML to pass some rules, but they could differ from Client to Client. To accomplish this we put rules into separate dll and will work with it as with plugin.
We have two assemblies:
Like on picture below. I simply don’t want to bore you with explaining of Rules logic. If you want sources just ping me.
Our Programm class wants to load rules from other assembly so we put Imports.
As you noticed we are using ImportMany, name says for itself.
We need to change our Compose method to search for Exports in Directory where execution assembly is located. For that we use DirectoryCatalog.
Put those dlls in one folder, and run MEF.DEMO. You will get:
The goal of MEF is to seamlessly glue together classes which import services from other parts of the application, and some classes which export those services. You could say that MEF is like DI container. Yes it has a lot of it, but Managed Extensibility Framework is concentrated on composition. And that composition could be done at run time at any point of time you want that to happen. Also you could do re-composition whenever you want to do that. You could load capabilities with Lazy technical or add some metadata to your exports. Discovering of dependencies in Catalog could be done with Filtering.
Managed Extensibility Framework is great feature, that Microsoft added to .NET 4.0 Framework.
January 26, 2010 QuickTip, UI, WindowsForms No comments
Do you want to see your button to be in pressed or unpressed state when you hit it once?
Did you catch that this behavior is not really button’s behavior – it is more checkBox’s behavior.
So to accomplish your goal you need move checkBox to your form and set appearance from “Normal” to “Button”. Like here:
Kick me if you don’t want to see such posts on my blog. :)
January 26, 2010 Opinion, RandomThoughts, Success No comments
I have 18th post this month that is more than in past three months. This means that I increased frequency of posting and I think that did not lost quality of its content. What do you think?
I think I’m good on this. I also increased amount of blogs I read (near 30).
Why do I think that blogging helps me?
To get more readers I need to have some certification that my blog is good to read and this could be reached only if I will continue learn something.
I also found one interested post where guy explains why Why Blog Post Frequency Does Not Matter Anymore. Yes, but my blog has defined content boundary and my posts are not something like posts on twitter, so I believe that it will lead me to Success.
Honestly I think we should see people in light of theirs attitude to something, but not in light of theirs knowledge of something.
Leave your comment with link to your blog. I will follow you immediately. ;)
January 25, 2010 Design Patterns 3 comments
Generally saying Specification is a predicate that determines if an object does or does not satisfy some criteria. By using Specifications you could easily recombine business logic together using boolean logic.
Have you ever thought that bool TryParse(string s, out int result) could be seen as some pattern? Yes we could talk about this method like about specification of integer represented in string, and this is validation usage. This pattern also could be used not only for Validation, but also for Queuring and Building purposes.
Let us imagine that we need to validate if some Patient is eligible for drugs procedures at home provided by nurse.
So this we will need two specifications. If they are both satisfied we could say that this patient is eligible for drugs procedures at home.
First specification
}
Second specification
As you guess ISpecification interface looks like:
Usage could look like:
You could say that we can put all verification in our method FetchPatientsForVisitWithDrugs. Yes, but this is not right way, because your specifications could be used in different locations and also if see this from DDD perspective you always need to bring concept things into the light.
Another question: Don’t you see this systax
if(drugsSpec.IsSatisfiedBy(patient) && nurseVisit.IsSatisfiedBy(patient))
to be boring systax? Yes, especially if you have many specifications.
Let us improve our design.
First we will add some methods to our interface like here:
}
And also will add CompositeSpecification which will be abstract base class for our two existing.
Let’s move to our existing specifications and how they changed with our new design:
}
This all gives us ability to have build specifications quering in more sweet way.
New Usage
Now we work with this new specification as with simple single one: if(specification.IsSatisfiedBy(patient))
With other specifications like OrSpecification and NotSpecification we could build more complicated queries.
For example I will show some possible usage of this in Nhibernate:
Advantages of the Specification:
January 23, 2010 Design Patterns 2 comments
Consider you need to develop some searching engine. Engine will look for different messages that has been sent. Searching process consists with few operations which make sense for each of message, BUT could have some characteristic which differs.
You want to write Searcher, which will allow you encapsulate algorithm of searching, but you also want to leave ability override behavior of some operations for specific messages. How could you accomplish this easily?
This pattern is intuitive as well as realization of it. You will need base class which holds primitive operations and some Template method (Search) which operates with those operations. Each operation could be overridden in derived classes.
I wrote naive implementation of this pattern, because I call primitive operations one by one in my template method. In real world you could hold complex algorithm built on your primitive operations. And you will just need to override parts which differs from standard implemented in base class. Please note that you could make primitive operations abstract. This will require implementation in each derived class.
My example implementation
protected Date DateSent;
protected String PersonName;
protected int ImportanceLevel;
public MessagesSearcher(Date dateSent, String personName, int importanceLevel){
DateSent = dateSent;
PersonName = personName;
ImportanceLevel = importanceLevel;
}
//primitive operations
protected void createDateCriteria(){
System.out.println(“Standard date criteria has been applied.”);
}
protected void createSentPersonCriteria(){
System.out.println(“Standard person criteria has been applied.”);
}
protected void createImportanceCriteria(){
System.out.println(“Standard importance criteria has been applied.”);
}
//TEMPLATE METHOD
public String Search(){
createDateCriteria();
createSentPersonCriteria();
System.out.println(“Template method does some verification accordingly to search algo.”);
createImportanceCriteria();
System.out.println(“Template method verifies if message could be so important or useless from person provided in criteria.”);
System.out.println();
return “Some list of messages…”;
}
}
public class ImportantMessagesSearcher extends MessagesSearcher{
public ImportantMessagesSearcher(Date dateSent, String personName) {
super(dateSent, personName, 3); // 3 means important message
}
//primitive operation overriden
protected void createImportanceCriteria(){
System.out.println(“Special importance criteria has been formed: IMPORTANT”);
}
}
public class UselessMessagesSearcher extends MessagesSearcher {
public UselessMessagesSearcher(Date dateSent, String personName) {
super(dateSent, personName, 1); // 1 means useless message
}
//primitive operation overriden
protected void createImportanceCriteria(){
System.out.println(“Special importance criteria has been formed: USELESS”);
}
}
Following usage:
Produces output:
Standard date criteria has been applied.
Standard person criteria has been applied.
Template method does some verification accordingly to search algo.
Special importance criteria has been formed: IMPORTANT
Template method verifies if message could be so important or useless from person provided in criteria.
Hope my example was not painful to understand. Honestly I do not see it to be 100% clear example on this pattern, but I think I did a great job on this. Of course you could find dozens of examples of it over internet. But this one is mine and it have put something in my mind.
January 22, 2010 .NET, Opinion 7 comments
Today I was reading some book on the .net development and found there interesting thing.
Guy explains “Why to use Generics?“
He wrote that Frameworks 1.0 and 1.1 did not support Generics, so developers were using Object.
He says, that generics offers two significant advantages over using the Object class:
1) Reduced run-time errors
That is because type-safety.
2) Improved perfomance
Casting requires boxing and unboxing, which slows performance. Using of Generics doesn’t require casting or boxing, which improves run-time performance.
And then funny thing…
He put a box which looks like:
OMG! I could not believe in his words. As per me this should be BULLSHIT, unless I 100% missed something there.
Test
I wrote a really quick verification like:
And the result is:
As you see generics are much faster. Also I searched over internet and found a lot of different stuff that says that generics provide better performance.
My Opinion
You would said what kind of book do I read. Is it “C# for Complete Dummy“?
No, that is Microsoft’s training Kit for exam 70-536.
Even if this was a book “C# for Dummy” it should not contain mistakes. And even if this is a book for kids it should not contain wrong thing. Yea.. it could be very simple, but not wrong!
I thought to write e-mail to that guy, but then decided that there is no need in this.
Just be careful when you read books and others thoughts, even mine :)
Have you ever faced with need to have your custom configuration?
Probably yes, on MSDN you can find a good example on custom configuration here.
That example was not 100% what I need, since I needed few included one in others collections. Like below:
As you see I want to get list of services which of them could be enabled or disabled also it could have interval to send one or many messages, which are also configured.
As you see I already added RoadMap.CustomConfiguration.ServicesSection to map ServiceSettings section to my appropriate class:
ServicesSection
Next you will probably will want to take a look is ServicesCollection:
ServicesCollection
ServiceElement
As you see it has Messages property which is again collection (similar to ServicesCollection) of MessageElements:
MessageElement
To ensure you that this works as expected just take a look on following screenshots:
We have one Greeter service:
It has two messages and second one is “World”:
Hope you could just copy-paste my code and also add MessagesCollection and you are done, just change naming to your needs.