PostSharp. Overview.
Before turning directly to the theme of the story, it is necessary to say a few words about the system things.
Aspect-Oriented Programming
Aspect-oriented programming (AOP) - programming paradigm, based on the idea of separation of functionality to improve the partitioning the program into modules.
There are many classes of problems whose solution is impossible in the context of the OOP, or lead to a large rubbish code and connectivity modules that bad. For example, logging. In order to keep a log of the program, it is necessary in each service method of placing multiple lines of service call logging, as it may have to pass through the input parameters of methods. All this code would not apply to the implementation of real-world task entrusted to the method, but only an eyesore. Besides, how many extra lines of code have to write by hand!
Another example is the authentication and verification of access rights. In good, before the execution of important methods, one must each time test whether the current user rights to run the specified method. Here, too, can be a lot of hassle and code
In general, any cross-cutting code drops out of the opportunities the PLO. AOP programming, by contrast, provides all the tools to highlight "through the code in separate entities, which significantly simplifies the code for testing and for use.
Design pattern “Specification”
Disclaimer
This article is mostly for beginners at programming, who know only some programming patterns or don’t know them at all.
About design patterns
Let’s take definition of design pattern from Wikipedia.
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.
For instance we can imagine that we are accomplishing a task “by analogy”. Or, for example, solving the same equation, but with another concrete numbers.
Pattern “Specification”- is a pattern of supplement’s conduct. The result of the implementation will be a Boolean variable; giving you the input of the operator of conditional transfer you control the behavior of the program.
With the help of the following techniques you can:
- Make your code more readable and concise
- Avoid duplications of code
- Easier to make changes to the implementation
As always, the work of the complex mechanism better shows a concrete example. Let's start with simple things, gradually moving to the complex. I will try to avoid situations in which revealed only a simple example, but as it is applied in real life – unclear.
Featured Common Controls
I would like to share my little library, which make my life easier when I create an user interface. At all, there are the same standard components, but with smart tags. They adapt on general and private fields. After starting installer and successful end of its work, you can find a new toolbar in the studio.
The library determines to GAC, creates a new toolbar in Visual Studio, and adds to a dialog box «Add reference…». You can read how to do it on your own in the one of my previous articles.
You can load the installer or a source code for these components and make a light setup for personally you or add your components. Don’t be shy to give suggestions which other elements of the UX can make better, what often uses and can be useful.
Source code and Installer
Next I will describe in short components which a library includes.
ReSharper Recipes. File templates.
Now I’ll redeem a promise about which I’ve told in the previous article. I’ll tell you how to reduce your working hours which you spend on writing tests.
If you can create new classes with the help of alt +ins in the Solution Explorer, why don’t we create text classes with all we need at once?
It is really possible, but in a more trivial way. So, let’s start.
Again we need a draft, but now we can use it with different names and with all inclusions of «using ». So, let’s use this code for our experiment.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tests {
[TestClass]
public class Class1 {
[TestMethod]
public void TestMethod() { }
}
}
ReSharper: Code snippets for tests.
I’ve always felt too lazy to key in full words, write different standard formulatings, it usually drives me crazy or make me bored. Some time before it hasn’t been so ruefully, but during these two years of working with ReSharper everything redoubled, for instance I don’t type «public» at all, I prefer to type «р» and press space, so ReSharper will finish writing. The only thing that I write by myself is names of methods, classes and tests. I would like to stress that only the names and not all notes. And variables are always named by ReSharper too. =) I’m a real sluggard!
I would like to share my knowledge with you how to develop such laziness and how ReSharper helps in it. Because of we hold to Test Driven Development, all code snippets will hold to texts.
All texts begin with an add of text class.
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class classname { }
Everything doesn’t change except the title of text class. I physically suffer from the look how the others write it. In information about myself I’ve told that at work we practice XP and that’s why pair programming is the imprescriptible point.
After choosing the class you need to choose the text method.
[TestClass]
public class classname {
[TestMethod]
public void name() {
}
}
Look how much text there is! And there are only 2(two) comprehensible words! Typing everything isn’t the easy way, so we need to automatize it. With the help of the ReSharper it does quite fast and easy.
Technical delay
Due to technical reasons in last 2 weeks i have no time and possibilities to write and post new articles. But now everything is ok and I hope that in the short time I'll post something interesting.
Hard'n'heavy!
DDD & TDD. Part III
As the result of previous parts we have domain with core classes and services. Actually we can generate and process any amount of data with methods provided by domain services. But there are some issues:
- How user will input data;
- How data will reconstitute/persist.
Domain by nature and the general plan must be absolutely independent and free from technology aspects as GUI/user interaction (WPF, WinForms, Web) and the data layer implementation (MSSQL, MySQL, Oracle, etc.)
Layers
Step by step we realize that application should be multi layered. And there are minimum possible set of layers, as I see it:
- Domain –business logic layer. In this layer described HOW to work with data.
- Data layer. At this level implemented saving and recovering data to/from external data storage to domain objects. Extracting these actions in separate layer makes application more agile, i.e. it’s easy to deal with several different DB without changing other parts of the application.
- Presentation layer. Here we say WHAT to do with data: define specific sequence of domain services and return calculated result to GUI. So, this layer connects Domain and Front End.
- User Interface (Front End). This level can be console, win forms, web, even mental transmission to mind! =) Try to keep this layer as dumb as possible. No data changes, no application flow logic – only do what other says with any initiative.
Now when we know layers responsibilities, next goal is to understand how they interconnect with each other.
DDD & TDD. part II
Disposition
So we have domain classes with minimal set of properties and methods. Also we have several tests on these classes. The goal is to learn how deal with domain classes – i.e. write services which will do complex calculations and modifications. For example count students in selected course.
Domain services
Assume that one of the tasks is count classes for selected course and how many students in them.
Let’s think which domain classes known about each other:
- “School” knows how many “Class”es it contains.
- “Class” knows how many students it contains.
I think it will be enough for now. What is the easiest way that will implement most of novice? Add to “School” method:
public int CountStudent(int classNumber) { … }
But it is a bad idea because of next reasons:
- It leads to growing responsibilities of “School” class and the result is coupling;
- Became harder to maintain and change “School”;
- Also can appear issues for test writing.
DDD & TDD. part I
Disclaimer
Article is a simple presentation of DDD and TDD in order to show how to deal with it and give an opportunity to start development according describing techniques. Those who already practice TDD and DDD, please don’t be strict to words.
Magic abbreviations
DDD – Domain Driven Design, in few words it is a solution code manner. And your target is an extract main application logic, the core, in a separate independent module. Domain must be independent from specific technologies, it won’t relay on underlying data, graphical presentation and so on.
TDD – Test Driven Design, developing application by tests. “Test become before code”.
Both of these practices (maybe better to call it philosophy) come together in my projects and, to be honest, for me it’s hard to imagine their usage separately. But yes, you can use them absolutely separate and no one oblige you to apply them in every project together.
There is a dozen of books, articles and other stuff in any format and size. Somewhere it is simpler, somewhere with more complex vision. I wouldn’t like to make a historical excursion how DDD and TDD have been invented, because it’s easy to find out in web, to google Eric Evance books or visit Martin Fowler site. I’d like to share my vision of subject in simple words. Tell how it helps, works, evolves and supports. I hope I can achieve the goal.
Okay, let’s take a closer tour by these techniques.
Custom controls
What for?
During everyday work I create a dozen of custom control. Formally, every “User Control” is a custom control. I create a specific layout from standard controls and use it in a program. I don’t think about them as about “real custom control”, because they are useful only for me in my very particular application. In the most common cases I don’t reuse them at all out of application scope. But from time to time I discover that some of them are good for common usage.
Sometimes I find good examples of controls from other systems or chargeable control kit. If one man make something, other one could make it also. =) By the way it’s a great challenge.
When I get know a smart tag closer, I’d decided that some of controls can be extended. It’s just like using extension methods to create DSL, helping classes and so on to write faster and easier. The same story with controls.
So there are several reasons why a custom control appears:
- Occasionally – as a result of everyday work;
- Extracted same parts from occasional controls;
- Planned as a new control from the very beginning;
- As an extension of exists control.
