Wednesday, May 4, 2011

Benefits Of An Employee System For Your Business

An employee information management is a crucial part of any business and a valuable investment. The daily work and contributions of your team members play a major role in the success of your company, and it is important that you manage them accordingly. Many businesses are now considering the use of an employee information system to help manage the needs of the human resources department and utilize all of their employees to the highest ability level. When this type of management system is used successfully within an organization, HR operations will run more efficiently and a significant costs savings can be seen.

Digital employee data management is generally run through an easy to use web based platform. In a digital system, information can be easily stored and retrieved. This includes items such as employee contact information, payroll reports, job descriptions, government laws and current and historical leave tracking. Additional information stored in a web based system includes application and hiring data, human resources policies, handbooks and emergency contacts. The management system will track information about each employee, including the time they devote to a certain project, daily work output, performance records, training and management skills. This information can be helpful when deciding on promotions and writing evaluations.

When deciding on an employee information management system, make sure to choose one that works with the existing set up of your company's human resources department. This will ensure high productivity and the best use of employee time. Make sure that the management system offers a variety of helpful tools and is user friendly and can be easily accessed by your employees, manager and the human resources team. Decide what features your business finds the most important and make sure the system you choose offers these items in an easy to use format.

Your human resources team will see improved productivity and a variety of other benefits when using an employee information management system. These tools will help reduce the HR daily workload by eliminating the redundancy of paperwork and administrative tasks and also ensuring data accuracy. Employee data management is an important part of any business and maintaining an organized and smoothly run HR department is crucial to a satisfying work experience. By streamlining the daily tasks of your human resources team with the technology of a web based data system, your company will experience increased productivity, employee satisfaction and a significant cost savings.

For additional information on how can you implement an HR portal based on SharePoint, see www.365kin.com

Principles Of Design

By Guillermo Cedillo

There are four principles of design: balance, emphasis, rhythm, and unity. These principles of design help you to combine the visual elements into a good design.

Balance

Visual interest is what you balance in design. Different colors, shapes sizes, etc. create different degrees of interest. It is the distribution of this interest that you need to control.

Each element on a layout has visual weight that is determined by its size, darkness or lightness, and thickness of lines. There are two basic approaches to balance: symmetrical and asymmetrical.

Symmetrical balance is an arrangement of elements so that they are evenly distributed to the left and to the right of center. Symmetrical balance can communicate strength and stability and is appropriate for traditional and conservative publications, presentations, and web sites. Asymmetrical balance can imply contrast, variety, movement, surprise, and informality. It is appropriate for modern and entertaining publications, presentations, and web sites.

1

Symmetrical balance is achieved by placing elements in a very even fashion in the design. If you have a large, heavy element on the right side, you'll have a matching heavy element on the left. Centering is the easiest way to get a symmetrically balanced page. But be careful, as it can be difficult to create a centered design that doesn't look flat or boring. If you want a symmetrically balanced design, it's better to create the balance with different elements - such as an image on the left and a large block of heavier text to the right of it.

Asymmetrical balance is an arrangement of unlike objects of equal weight on each side of the page. Color, value, size, shape, and texture can be used as balancing elements. Asymmetrically balanced pages can be more challenging to design - as they don't have elements matched across the centerline of the design. For example, you might have a large element placed very close to the centerline of the design. To balance it asymmetrically, you might have a small element farther away from the centerline. If you think of your design as being on a teeter-totter or seesaw, a lighter element can balance a heavier one by being further away from the center of gravity. You can also use color or texture to balance an asymmetrical design.

2

Sometimes the purpose of the design makes an off-balance or discordant design work well. Designs that are off-balance suggest motion and action. They make people uncomfortable or uneasy. If the content of your design is also intended to be uncomfortable or make people think, a discordantly balanced design can work well.

Rhythm

Rhythm in design is also known asrepetition. Rhythm is a pattern created by repeating elements that are varied that allows your designs to develop an internal consistency that makes it easier for your customers to understand. Once the brain recognizes the pattern in the rhythm it can relax and understand the whole design.

3

Repetition (repeating similar elements in a consistent manner) and variation (a change in the form, size, or position of the elements) are the keys to visual rhythm. Placing elements in a layout at regular intervals creates a smooth, even rhythm and calm, relaxing mood. Sudden changes in the size and spacing of elements creates a fast, lively rhythm and an exciting mood.

Gestalt

Gestalt is a general description for the concepts that make unity and variety possible in design. It’s an ability of the mind to see unified “wholes” from the sum of complex visual parts. Some principles of gestalt are Proximity, Similarity, Continuance, Closure, Uniform Connectedness, and 1+1=3 Effects.

4

Emphasis

Emphasis (or dominance) in design provides the focal point for the piece. It is a way of making the element that is most important stand out in the design. Every layout needs a focal point to draw the reader’s eye to the important part of the layout. Generally, a focal point is created when one element is different from the rest but is necessary to avoid too many focal points, because it will defeat the purpose. When everything has equal emphasis at best it can make the piece appear busy, and at worst the design will be boring and unappealing.

Emphasis can be achieved by:

· Using semantic markup will provide some emphasis, even without any styles.

· Change the size of fonts or images to emphasize or de-emphasize them in the design. Use bold, black type for headings and subheads and much lighter text for all other text. Place a large picture next to a small bit of text.

· Using contrasting colors can provide emphasis. Use a series of evenly spaced, square photographs next to an outlined photograph with an unusual shape.

· Put an important piece of text on a curve or an angle while keeping all of the other type in straight columns.

· Use colored type or an unusual font for the most important information.

Unity

Unity (or proximity) helps all the elements look like they belong together. Readers need visual cues to let them know the piece is one unit-the text, headline, photographs, graphic images, and captions all go together. Elements that are positioned close to one another are related while elements that are farther apart are less related.

5

Unity can be accomplished by:

· Been consistent with the type font, sizes, and styles for headings, subheads, captions, headers, footers, etc. throughout the publication, presentation, or web site.

· Positioning elements that are to one another are related while elements that are farther apart are less related.

· Using only one or two typestyles and vary size or weight for contrast throughout the report.

· Repeat a color, shape, or texture in different areas throughout.

· Choose visuals that share a similar color, theme, or shape.

References

Web Style Guide: Basic Design Principles for Creating Web Sites, by Patrick J. Lynch and Sarah Horton.http://webstyleguide.com/

Gestalt psychology
http://en.wikipedia.org/wiki/Gestalt_perception

Unity In Design: Creating Harmony Between Design Elements, by Steven Bradley
http://www.vanseodesign.com/web-design/design-unity/

Art 104: Design and Composition
http://daphne.palomar.edu/design/Default.htm

The Basics of Graphic Design
http://www.online.tusc.k12.al.us/tutorials/grdesign/grdesign.htm

No-brainer Web Services for Ajax using .NET

By Guillermo Cedillo

Use Microsoft Ajax to extend you services

If you want your service works as a REST service and return JSON is not necessary to do some complex modifications in the web.config or import special libraries. When you have a WCF or an asmx web service already implemented, the easiest and cleaner way to extend your services is using the ScriptManager Control of Microsoft Ajax.

First, enable your service to work with the Script Manager add or uncomment [ScriptService] in every web service that you want to extend.

1

Then in your page add the Script Manager and add the reference to your services like this:

<asp:ScriptManagerID="ScriptManager1"runat="server">

<Services>

<asp:ServiceReferencePath="~/WebService1.asmx"/>

Services>

asp:ScriptManager>

And that’s all you need. Now you can use your web services with javascript!

$.getJSON("WebService1.asmx/HelloWorld", function(data) {

alert(data.d);

});

Use ADO.NET Data Services

If you want an extreme REST service with complex queries, inserts, updates and deletes, there’s a very easy way to add this functionality in .NET.

First add the ADO.NET Entity Data Model to your project.

2

Then configure your Model. It is as easy as doing drag and drop from the tables in the data base. If you want more complex functions as implement store procedures or complex relations please visit this reference.

3

Then add a ADO.NET Data Service:

4

Then just do a relationship between your Model and the REST Service adding the Model class to the generic implementation of the DataService and set the access permissions for your tables.

5

After this the service has the ability to do complex operations in the query like:

/Person?$filter=id eq 1
/Person?$filter=id eq 10&$select=Name

And use the request verb to perform selects, inserts, deletes and updates as follow:

GET – select
POST – insert
PUT – update
DELETE – delete

For more information please visit http://msdn.microsoft.com/en-us/data/ee720180.aspx. Here you will find very nice tutorials and examples.

Return JsonResult in an ASP.NET MVC Controller

The last one is for ASP.NET MVC and it’s very easy! Just add JsonResult to the controller actions:

6

And that’s it!

I hope to see you again around the blog J

Using the: SharePoint Data-View web part

A data view web part is very useful when comes to SharePoint display forms customization. You can add this web part by using the SharePoint designer tool.

1. Insert a data view web part onto the page you want to display in the list by selecting Data View > Insert Data View from the main menu.

1

2. Using the Data Source Library Task Pane in SharePoint designer select ‘Connect to another library‘ and browse to the site that contains the list you wish to display.

2

3. Expand the new site in the Data Source Library Task Pane and select the list or library that contains the information you wish to display. From the drop down select ‘Show Data‘ to populate the Data Preview tab.

3

4. From the list of columns, select the ones that you wish to display and select ‘Insert selected fields as multiple item view from the drop down menu at the top of the window. This will populate the data view with sample information (in a table by default). At this stage you may get an error in the design view that states:

“The server returned a non-specific error when trying to get data from the data source. Check the format and content of your query and try again. If the problem persists, contact the server administrator”.

4

This can be corrected by modifying the tag of the element of the data view to contain the reletive url of the site for the WebUrl parameter (look at the default parameters to find this out – should be something similar to ‘/news/’).

5

Extending ObservableCollection to notify when an Item has changed.

By Miguel Juárez

There are several times that we would like that our collection would have the ability to notify not only when an element is added or removed from the collection itself (like the ObservableCollection does), but also to notify when any of the items’ property changes (when the items themselves are already implementing the INotifyPropertyChanged interface). Here I propose a solution for this problem.

What I’ll basically do is to extend ObservableCollection to provide for such capabilities. I will also define a new EventHandler (with a new EventArgs type) which the consumer can subscribe to get information from the PropertyChanged, such as the Property Name, or the Item’s Index in the collection.

public class NotifyCollectionChangeEventArgs : PropertyChangedEventArgs

{

public int Index { get; set; }

public NotifyCollectionChangeEventArgs(int index, string propertyName)

: base(propertyName)

{

Index = index;

}

}

public class NotifiableCollection: ObservableCollection where T: class, INotifyPropertyChanged

{

public event EventHandler<NotifyCollectionChangeEventArgs> ItemChanged;

protected override void ClearItems()

{

foreach (var item in this.Items)

{

item.PropertyChanged -= ItemPropertyChanged;

}

base.ClearItems();

}

protected override void SetItem(int index, T item)

{

this.Items[index].PropertyChanged -= ItemPropertyChanged;

base.SetItem(index, item);

this.Items[index].PropertyChanged += ItemPropertyChanged;

}

protected override void RemoveItem(int index)

{

this.Items[index].PropertyChanged -= ItemPropertyChanged;

base.RemoveItem(index);

}

protected override void InsertItem(int index, T item)

{

base.InsertItem(index, item);

item.PropertyChanged += ItemPropertyChanged;

}

private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)

{

T changedItem = sender as T;

OnItemChanged(this.IndexOf(changedItem), e.PropertyName);

}

private void OnItemChanged(int index, string propertyName)

{

if (ItemChanged != null)

{

this.ItemChanged(this, new NotifyCollectionChangeEventArgs(index, propertyName));

}

}

}

The Importance of building a correct social media strategy for IT companies

Nowadays, many IT companies have realized the importance of using social media to promote their products and services. However, most of these companies do not have a structured plan for making this strategy work properly.

There are IT companies who believe that developing a social media strategy consists only of creating a Twitter and a Facebook account, hoping that costumers will instantly arrive and start following.

However like any other marketing plan, there is a process that must be followed in order to develop a proper social media strategy. First of all it is important to identify the market that the company wishes to send the final message, in other words, you need to identify which is your target market.

Some questions you can do to identify your target market are:


1. Who are they?
2. What solutions are looking for?
3. Which websites they visit frequently?
4. Which products they have purchased?
5. What language do they use?

Once you have identified your target market it is necessary that all the information you offer in the social networks be focused on those people and their needs. It is important not to jump directly to start trying to sell your services using social media, you need first to try to inform and help people that are possibly going to buy your products.

A great way to do this is by creating a company blog and sharing your articles or news related to the technology industry. By doing this you build trust and automatically positioned as a credible company in the industry.

As you could see, is of great importance to create a proper social media strategy to promote and supply the products of technology companies today. Remember that the most important aspect is to provide solutions and great content that will help your target market; sales and leads will come on their own.

negocios por internet

WCF service in IIS with multiple host-headers

By Alejandro Villarreal

Hosting WCF services in IIS is usually a very straightforward process with no weird configuration settings or obscure errors. You create a site in IIS, publish your service in the site’s folder, and you’re ready to go.

However, things start to get a bit complicated in more advanced scenarios like the one I encountered some days ago: if your site in IIS answers to multiple host-headers, then when you try to access the service through a web browser, instead of seeing the usual page with the service description you will receive a screen indicating a Runtime Error, and you should be able to find an Error entry in Window’s Event Viewer (under “Windows Logs”/Application) whose Source is System.ServiceModel 3.0.0.0 (or whatever version you happen to be using), that says something like this:

Exception: System.ServiceModel.ServiceActivationException: The service '/YourService.svc' cannot be activated due to an exception during compilation. The exception message is: This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

This message was certainly more obscure than the errors I was used to when dealing with IIS-hosted WCF Services, and it didn’t make much sense until I found the solution and understood what the error was talking about. In summary, when the WCF Service is hosted in an IIS site, IIS passes all the host-headers to which the site answers as base addresses for the service when it instantiates it, and it in turn complains because it only expects 1 base address of a particular type. Several host-headers imply several base addresses for http, and thus the error.

A code-based solution is to create a custom class than inherits from ServiceHostFactory and filters out the unwanted addresses, and then tell IIS to use that class to instantiate your service. The class is nothing special:

1

Instead of returning the first address that was passed in (baseAddresses[0]) –which could impact your service if it depends heavily on this address, and the order of the host-headers changes in IIS– you can implement your own custom logic to determine which address the service is going to use.

Then you must tell IIS to actually use this class, which is achieved by modifying the .svc file that was generated when publishing your service, so it looks like this (notice the second line):

<%@ ServiceHost Service="Your.Namespace.YourService"

Factory="Your.Namespace.CustomHostFactory" %>

“CustomHostFactory” is just the name of the class you created, so it will depend on the actual name you give to it.

And that’s it! If you try to access your service through the web browser again, you should get the .NET generated help page. According to the site linked earlier, there is also a way to make this happen purely by editing the configuration file, but I haven’t tested it. However, feel free to go ahead and try it!

Have a nice day!

Quirks of Distributed Transactions (MSDTC) and SQL Server

By Alejandro Villarreal

Distributed transactions can be of great help when dealing with complex operations that must be atomic across servers, but then again for their very nature –distributed– they can be hard to debug when something fails. A good example of this is an error I encountered recently. Here’s a bit of background:

We have a Web Application hosted in Server01 sending messages to a WCF Service through an MSMQ endpoint (we’re using transactional queues to leverage their reliability), and this Service saves the content of the messages it receives in a SQL Server database hosted in a remote server (Server02). The queue in Server01 plus the database in Server02 make this a distributed transaction, and that’s why we need MSDTC in the first place.

The whole setup worked perfectly… until suddenly it just didn’t anymore. The WCF Service started throwing the following error:

System.Data.SqlClient.SqlException: Warning: Fatal error 8510 occurred at May 21 2010 9:50AM. Note the error and time, and contact your system administrator.

A severe error occurred on the current command. The results, if any, should be discarded.

Fatal error with almost no description? Out of nowhere? We didn’t deploy a new version (nor redeployed the same one, for that matter); we didn’t change anything in the configuration files; we didn’t install OS updates. Then why could it possibly start to fail out of nothing? Both servers had gigs of free disk space, and plenty of RAM, so that couldn’t be the problem. I checked the SQL Server logs, and found this:

1

The full message is “Enlist operation failed: 0x8004d01c(XACT_E_CONNECTION_DOWN). SQL Server could not register with Microsoft Distributed Transaction Coordinator MS DTC) as a resource manager for this transaction. The transaction may have been stopped by the client or the resource manager.”

Accompanied by a second message: “Error: 8510, Severity: 20, State: 3.”

Ok, that gave us some clue about what was happening, but didn’t shed any insight on why. After quite some time looking for the cause of this issue, I finally reached this article, which doesn’t clearly state why did their solution solve the problem but references this other article, which again, doesn’t quite pose a solution to the problem, but is very close. The key information I obtained from that article is

“As MSTC [sic] is the core Transaction Service, its restarting will cause other Transaction depended [sic] services stop working normally, such as SQL server”.

The core idea is pretty clear. And combining this with a comment from the first article I mentioned (“Restarting services on the sql server that could not initiate distributed transactions resolved the problem”), I imagined that the MSDTC and the SQL Server windows services must be started in that order for everything to work.

Back to our setup, I recalled that I did restart the MSDTC service at some point, so I obviously tried restarting SQL Server, and…

Another error! It seemed the user we had been using all along to connect to the database didn’t have permissions anymore. Oh, but wait! There isn’t only 1 database server, there are 2, configured for mirroring, and a third one that acts as witness to allow for automatic failover. Restarting SQL Server in the first database server triggered the failover, so when it came back up, it wasn’t the primary anymore, but the mirror. And since you cannot connect to a mirror, the error makes sense. Whew! I started a manual failover back to this server, and surely enough, distributed transactions started working again!

So, even though it is not explicitly documented anywhere (that I could find), I think it is safe to say the following, which generalizes the title of this post a bit: always make sure that any server hosting resources that make use of MSDTC, sets up its services and resources in the correct order, and be careful with restarting the MSDTC service.

I hope this saves somebody the time I spent debugging this issue.

Getting Started with PowerShell Remoting

By Irvin Gomez

As a software engineer, more often than not I find myself writing code in C#, and things tend to be pretty neat inside Visual Studio. However, when we need to deploy our new application to the servers where the application will be hosted, the number of Remote Desktop sessions we require can be overwhelming.

Here is where PowerShell Remoting comes to the rescue. Basically it allows a client machine to connect to another machine and then you can run PowerShell commands and scripts typing from the client. Think of Telnet on Unix machines.

Remoting is a feature introduced on PowerShell v2.0 and basically will let you run any PowerShell command or script (like deployment and configuration scripts) for all your farm servers from a single computer. And the sweet thing is that you will not need to start Remote Desktop connections to each server separately.

So to start working with PowerShell Remoting you need to follow these steps:

1. Log on the Host computer, the one you want to connect to, and start an elevated (run as Administrator) PowerShell session.

2. Configure the WinRM Service (Windows Remote Management) using the cmdlet:

Enable-PSRemoting

3. Configure the trusted hosts, this is, specify which other machines are allowed to connect to this one. For example if you want to allow remote connections from two machines named SERVER-2 and MYDESKTOP you will need to run the following:

set-item wsman:localhost\client\trustedhosts -value “SERVER-2,MYDESKTOP”

4. Once you have done this, you are ready to go. You can log into your computer and start a remote session to your recently configured server, suppose we are trying to start a session into SERVER-1 machine:

Enter-PSSession SERVER-1

5. Now you are ready to type PowerShell commands and they will run on SERVER-1 machine. Once you are finished, you exit the session:

Exit-PSSession

6. So far so good, but what when you have a full script that you want to run on the other machine? For instance, if you want to run myScript.ps1 you will need to use New-PSSession and Invoke-Command

$remSes = New-PSSession -ComputerName SERVER-1

$job = Invoke-Command -Session $remSes -FilePath myScript.ps1 –AsJob

This will run myScript.ps1 on SERVER-1 machine as a job from your very desktop. At first, this may seem like a lot of things to do just to be able to run scripts remotely on a single server.

But when you have several servers to manage and you have scripts that do exactly the same on all servers then PSRemoting will be handy, more if you use New-PSSession and Invoke-Command as part of a larger script that handles all your servers. Automating this type of tasks, in the long term will make your life easier and they will save you much more than a few hours, which you can use to create more PowerShell scripts to automate something else.

ASP.NET 4.0, MVC and request validation

By Alejandro Villarreal

If you’ve ever dealt with the front-end part of application development in ASP.NET, you might have encountered the following error at some point:

A potentially dangerous Request.QueryString value was detected from the client

This happens when the user submits a value (either in a form or in the query string) that the ASP.NET Framework considers dangerous, in the sense that it might be an HTML/script injection attack.

Sometimes it is necessary to turn that feature off (see here and here on how to do it). It might be the case that you expect the user to provide valid XML/HTML, and thus you don’t want the Framework to abort the request with an Exception. As explained in the links I mentioned before, this can be done by disabling this validation completely in the Web.Config file or on a per-page basis (when working with WebForms), or disabling it for a controller or specific controller actions (when working with MVC).

Since I’m currently working on an application that uses the MVC Framework, this post focuses on the problem I had while trying to disable request validation for specific controller actions.

According to this post by Stephen Walther (and several others), the only thing you need to do is to add the ValidateInput attribute to your ActionMethod, like this:

[ValidateInput(false)]
public ActionResult MyActionMethod(string myParameter)
{
// Method implementation goes here...
}

Seems simple enough… but it just wouldn’t work for me! I kept receiving the “potentially dangerous” error when making a request to the URL that triggered “MyActionMethod”, when “myParameter” included an HTML tag. The method wasn’t even being activated.

After a bit of research I stumbled upon this post, which had the solution. The key was this:

So the MVC team gave us the [ValidateInput(false)] attribute to disable this annoying feature. But just setting it on an action will also fail, you still have to set one more setting at the web.config for this to work (if you are working with asp.net 4)

Oh, I am working with ASP.NET 4! So I had to add this to my Web.Config file:


...

...

As explained in the article (and originally in this whitepaper that explains the breaking changes in ASP.NET 4), previous versions of the Framework only performed request validation for ASP.NET pages (.aspx files and their class files), while version 4 performs it for all requests, before their BeginRequest phase. In my particular scenario, this means that the attributes in the controller don’t even have a chance to act, since the Framework has already decided that the request will be aborted. And that’s why reverting the validation behavior to that in version 2.0 of the Framework (through that additional line in the config file) makes everything work as expected again.

This made me think on how is it that we can prevent request validation on MVC-based applications developed under Framework 4.0 without resorting to that behavior, but couldn’t find any documentation on it. Hopefully Microsoft did think about this and we’ll soon start seeing solutions to that problem…

How to Handle the WCF Error on Silverlight

By David Espino.

When sending a call to a WCF service from a Silverlight application and the service returns an error, the Silverlight client backs up only a very generic error:

The remote server returns an error: NotFound

This message does not help to know the exact error neither for the user nor the developer. Even more, it is necessary for more security to make a division of the error information: one message for the user and one detailed message for the system administrator or developer.

The first part of the error dedicated to the application administrator consists on writing the exception that the code generates on a detailed way. There are a lot of tools that allows tomake this log of errors (Log for Net, Microsoft Enterprise Library and some other). For this example, we need to provide an easy log in to the server application log where our service would be found. This method captures two parameters: One “Entity” that indicates the business entity where the error occurred (for example the Countru, State, Employee, etc) and an action, which indicates the operation that could not be performed (Save, Select, Delete, etc). With these two parameters we can deliver a friendly message to be sent to the user.

public class ExceptionLogger

{

public static string LogAndReturnError(Exception ex, string entidad, string accion)

{

StringBuilder cadenaError = new StringBuilder();

cadenaError.Append("Ocurrió un error en el servicio de la aplicación: \n\n");

cadenaError.Append("Error: ");

cadenaError.Append(ex.Message);

cadenaError.Append("\n\nStackTrace: \n\n");

cadenaError.Append(ex.StackTrace);

EventLog.WriteEntry("NombreAplicación", cadenaError.ToString(), EventLogEntryType.Error);

cadenaError = new StringBuilder();

cadenaError.Append(string.Format("Ocurrió un error al intentar {0} {1}. Contacte al administrador del sistema.", accion, entidad));

return cadenaError.ToString();

}

}

The next step would be to generate an entity as a DataContract for the object that stores errors that occur during the service.

[DataContract]

public class ServiceError

{

[DataMember]

public string Type { get; set; }

[DataMember]

public string Message { get; set; }

}

The next thing to do is to make our service operations to return us an error object into the interface layer. We will use an output parameter in our methods of service and use the kind of login error.

public void DoWork(string Parameter1, out ServiceError errorService)

{

errorService = null;

try

{

//Operaciones a realizar

}

catch (Exception ex)

{

errorService = new ServiceError

{

Message = ExceptionLogger.LogAndReturnError(ex, "Acción", "Entidad"),

Type = "Aplicación"

};

}

Finally we need a Silverlight application to consume our service. For this, we need to add a Silverlight application and then a reference of our service. Once we call our service we are going to get the following:

MyServiceClient client = new MyServiceClient ();

client.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted);

client.DoWorkAsync("Hola Mundo");

void client_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)

{

//Error propio de WCF

if (e.Error == null)

{

//Nuestro Parámetro de Error!!!

if (e.errorService == null)

{

if (e.Result != null)

{

//Manipular resultado

}

}

}

}

This is a very easy way to implement error management on Silverlight.

Synchronous and Asynchronous Validations using the Silverlight 4.0 and the MVVM pattern

By David Espino.

In every application that we develop, it is always an excellent practice to validate the entry data of an opt-in form. In common web applications, Visual Studio provides several mechanisms and controls to perform such validations. In the case of Silverlight 4.0 we can find some existing interfaces that allow us to do such validations when developing applications using the MVVM pattern.

Introduction to the MVVM pattern.

This design pattern allows applications to divide them into separate layers and provides additional benefits such as implicit validation, ease of including unit tests and an increase in the reuse of components.

The pattern defines three main parts, the Model, the View and the View-Model. The image below briefly explains the objectives of each of the layers.

1

For further information of this pattern visit the following link:

http://weblogs.asp.net/dwahlin/archive/2009/12/08/getting-started-with-the-mvvm-pattern-in-silverlight-applications.aspx

Synchronous and Asynchronous Validations

There are validations that can take place when the user captures the information (or fails to capture) in length, structure or valid characters of the information captured. These validations can be done synchronously. However, when trying to validate whether data is duplicated in the database, it is necessary Silverlight to link to a repository of data through a service handler or WebClient. These validations are executed after the user captures the information and after running the service function asynchronously.

Procedure: To implement synchronous validations we are going to use IdataErrorInfo interface and INotifyDataErrorInfo for asynchronous validations.

This will require several elements:

1. ValidationHandler class will be working with notifications when an error occurs. This class contains a dictionary called BrokenRules which will contain the errors that are occurring in the model.

public class ValidationHandler

{

public Dictionary<string, string> BrokenRules { get; set; }

public event EventHandler IsValidated;

public void OnIsValidatedChanged()

{

if (IsValidated != null)

{

this.IsValidated(this, new EventArgs());

}

}

public ValidationHandler()

{

BrokenRules = new Dictionary<string, string>();

}

public string this[string property]

{

get

{

return this.BrokenRules[property];

}

}

public bool BrokenRuleExists(string property)

{

return BrokenRules.ContainsKey(property);

}

public bool ValidateRule(string property, string message, Func<bool> ruleCheck)

{

if (!ruleCheck())

{

if (!BrokenRuleExists(property))

{

this.BrokenRules.Add(property, message);

}

else

{

this.BrokenRules[property] = message;

}

this.OnIsValidatedChanged();

return false;

}

else

{

RemoveBrokenRule(property);

this.OnIsValidatedChanged();

return true;

}

}

public void RemoveBrokenRule(string property)

{

if (this.BrokenRules.ContainsKey(property))

{

this.BrokenRules.Remove(property);

}

}

}

A base class for View-Model of the application that implements the INotifyPropertyChanged Interface

public class ViewModelBase : INotifyPropertyChanged

{

public event PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged(string propertyName)

{

PropertyChangedEventHandler handler = PropertyChanged;

if (handler != null)

{

handler(this, new PropertyChangedEventArgs(propertyName));

}

}

}

A service that will do an Asynchronous validation of an object called "Person" whose business logic would be on finding a data repository if the captured Id is duplicated or not. For purposes of this example, this logic is omitted. Do not forget the policy files required by Silverlight to consume the service.

public class PersonServiceObject : IPersonService

{

public string GetData(int value)

{

return string.Format("You entered: {0}", value);

}

public bool GetDataUsingDataContract(PersonType composite)

{

return false;

}

}

[ServiceContract]

public interface IPersonService

{

[OperationContract]

string GetData(int value);

[OperationContract]

bool GetDataUsingDataContract(PersonType composite);

// TODO: Add your service operations here

}

[DataContract]

public class PersonType

{

int id = 0;

string nombre = "Hello ";

[DataMember]

public int Id

{

get { return id; }

set { id = value; }

}

[DataMember]

public string Nombre

{

get { return nombre; }

set { nombre = value; }

}

}

Next, we need to add a referral to the service in our Silverlight application to use the service. We do it from "Add Service reference" which is in the Project Explorer.

2

Now, with the reference of the service we can create our model to be validated.

public class PersonViewModel: ViewModelBase, IDataErrorInfo, INotifyDataErrorInfo

{

private string nombre;

private int id;

private ValidationHandler validator = new ValidationHandler();

private PersonService.PersonServiceClient personProxy = new PersonService.PersonServiceClient();

public int Id

{

get { return id; }

set {

id = value;

RaisePropertyChanged("Id");

bool valid = validator.ValidateRule("Id", "Id del cliente no válido ", () => (id > 100));

personProxy.GetDataUsingDataContractAsync(new PersonService.PersonType { Id = value, Nombre = this.Nombre });

}

}

public string Nombre

{

get { return nombre; }

set {

nombre = value;

RaisePropertyChanged("Nombre");

}

}

public PersonViewModel()

{

if (!DesignerProperties.IsInDesignTool)

{

this.personProxy.GetDataUsingDataContractCompleted += new EventHandlerGetDataUsingDataContractCompletedEventArgs>(personProxy_GetDataUsingDataContractCompleted);

}

}

void personProxy_GetDataUsingDataContractCompleted(object sender, PersonService.GetDataUsingDataContractCompletedEventArgs e)

{

if (e.Error == null)

{

bool valid = validator.ValidateRule("Id", "Id del cliente duplicado. ", () => (e.Result == true));

OnErrorsChanged("Id");

}

else

{

MessageBox.Show("No fue posible obtener la validación en el servicio.");

}

}

#region IDataErrorInfo Members

public string Error

{

get { return null; }

}

public string this[string columnName]

{

get

{

if (this.validator.BrokenRuleExists(columnName))

{

return this.validator[columnName];

}

return null;

}

}

#endregion

#region INotifyDataErrorInfo Members

public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

public System.Collections.IEnumerable GetErrors(string propertyName)

{

IEnumerable<string> errors = null;

if (propertyName == null)

{

errors = validator.BrokenRules.Select(dict => dict.Value);

}

else

{

if (validator.BrokenRuleExists(propertyName))

{

errors = validator.BrokenRules.Select(dict => dict.Value);

}

}

return errors;

}

public bool HasErrors

{

get { return validator.BrokenRules.Keys.Count > 0; }

}

protected void OnErrorsChanged(string propertyName)

{

if (ErrorsChanged != null)

ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));

}

#endregion

}

The line bool valid = validator.ValidateRule("Id", "Id del cliente no válido ", () => (id > 100));

Will cause when you assign the property, it is going to be confirmed that the data captured is greater than 100, otherwise an error will add to the dictionary of errors. At the end a call is going to be made to the service for validating asynchronously using the method:

personProxy.GetDataUsingDataContractAsync(new PersonService.PersonType { Id = value, Nombre = this.Nombre });

Upon returning from the service, the validation will be done again with the result of the service and invokes a method that fires the event ErrorsChanged, typical of INotifyDataErrorInfo interface to notify whether or not there was an asynchronous error.

Once you have the model we must now add this model as a model for a hearing to test our validation:

<UserControl

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

xmlns:ic="http://schemas.microsoft.com/expression/2010/interactions"

xmlns:local="clr-namespace:AplicacionPrueba"

mc:Ignorable="d"

x:Class="AplicacionPrueba.MainView"

d:DesignWidth="640" d:DesignHeight="480">

<UserControl.Resources>

<local:PersonViewModel x:Key="MainViewModelDataSource" />

UserControl.Resources>

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}" d:DataContext="{d:DesignData /SampleData/MainViewModelSampleData.xaml}">

<StackPanel>

<TextBlock Text="ID:">TextBlock>

<TextBox x:Name="txtNombre" Text="{Binding Path=Id, Mode=TwoWay, ValidatesOnDataErrors=True}" >TextBox>

StackPanel>

Grid>

UserControl>

At that view we will add our View-Model as a resource to be used, in addition to make a Bind to the control in which we are going to capture the Id for the change of ownership. If we try to be less than 100, the control will display an error:

3

If we try to proof a value greater than 100, it will trigger the asynchronous validation, and after a while you will receive an error of duplicity. In this example it will always mark the error because the method of service is not doing the validation, but in fact is always returning false as the value of validation.

4