The Onion Architecture : part 1 : Jeffrey Palermo (.com)

The Onion Architecture : part 129 July 2008This is part 1.  part 2. part 3. part 4.  My feed (rss).I’ve spoken several times about a specific type of architecture I call “Onion Architecture”.  I’ve found that it leads to more maintainable applications since it emphasizes separation of concerns throughout the system.  I must set the context for the use of this architecture before proceeding.  This architecture is not appropriate for small websites.  It is appropriate for long-lived business applications as well as applications with complex behavior.  It emphasizes the use of interfaces for behavior contracts, and it forces the externalization of

Source: The Onion Architecture : part 1 : Jeffrey Palermo (.com)

Service Locator example in c#

publicinterfaceICustomer  { string GetCustomerName(); 
 }
  • Implement ICustomer interface.
 classCustomer : ICustomer
    {            publicstring GetCustomerName()          {             return"Customer 1";          	 }      }
  • Create a ServiceLocator as shown below and add the mapping of ICustomer and its real implementation class.
 publicinterfaceIServiceLocator
    {         T GetService<T>();     }      	publicclassServiceLocatorSimple : IServiceLocator
    {    	// map that contains pairs of interfaces and
// references to concrete implementations
privateIDictionary<object, object> services;         	 public ServiceLocatorSimple()          {              	services = newDictionary<object, object>();              // fill the map
this.services.Add(typeof(ICustomer), newCustomer());         }         	 public T GetService<T>()         	 {   try
            {                 	    return (T)services[typeof(T)];               }              catch (KeyNotFoundException)  { thrownewApplicationException("The requested service is not registered"); }          }     }
  • Create a client application to consume it .
 
 staticvoid Main(string[] args)          {           
  IServiceLocator serviceLocator = newServiceLocatorSimple();     
ICustomer customersaervice = serviceLocator.GetService<ICustomer>();  
 Console.WriteLine(customersaervice.GetCustomerName());          }

Now Client application does not know about the concrete implementation. we can easily change the implementation without effecting 
client application.

Automatic file checkout is missing in VS2010 and TFS

I have recently came to a strange behavior. I am using VS 2010 (ultimate edition) and TFS 2010. In past the  with VSS when even I edit any file it gets automatically checked out and see the small icon for it in with filename.

 

Now I do not get it any more Sad smile

Reason: You solution is not binded with the source control.

Solution:

1. Go to File—> Source Control—> Change Source Control

image

 

image

2. Select all the projects in your solution file and BIND them . 

 

 

That’s it

Running websites in asp.net 2.0 and 4.0 on same IIS

This sounds like a very difficult task but its really now.

Lets take it a scenario to make it happen .

I have a Widows 2008 server with IIS 6.1 installed on it . I have some applications running on it already which are built on .net2.0 framework.

I want to host new application built in .net 3.5 or 4.0 framework .

Steps

  1. Download the new framework from below link

http://www.microsoft.com/download/en/details.aspx?id=17851

2. install it .

3. Restart computer

4.now register the new framework on IIS server

   image

 

5. Now you will get new framework application pool in your IIS

image

6. Assign this application to your application.

 

That’s it!!

Make List Pageable

I have doing some work these days on MVC .I needed an functionality to show data on the screen with paging.

In the beginning I thought this could be really difficult to add new method for  IEnumerable list objects. Here I am going to share the concept to achieve this and some code snippet..(some of  from my application and some of them are from internet.)

Steps

1.  First we need an interface which has properties to provide necessary information for paging like Page count, page number etc. My interface looks like this :

 

 

	public interface IPagedList<T> : IList<T>
	{
		int PageCount { get; }
		int TotalItemCount { get; }
		int PageIndex { get; }
		int PageNumber { get; }
		int PageSize { get; }
		bool HasPreviousPage { get; }
		bool HasNextPage { get; }
		bool IsFirstPage { get; }
		bool IsLastPage { get; }
	}
 

2. Next we need to simply implement this interface but with

 
System.Collections.Generic.List
 
which look like this 
(I have reformatted the code to fit in the post )
	public class PagedList<T> : List<T>, IPagedList<T>
	{
public PagedList(IEnumerable<T> source, int index, int pageSize, int? totalCount = null)
	: this(source.AsQueryable(), index, pageSize, totalCount)
{
}

public PagedList(IQueryable<T> source, int index, int pageSize, int? totalCount = null)
{
if (index < 0)
throw new ArgumentOutOfRangeException("index", "Value can not be below 0.");
if (pageSize < 1)
throw new ArgumentOutOfRangeException("pageSize", "Value can not be less than 1.");

if (source == null)
source = new List<T>().AsQueryable();

var realTotalCount = source.Count();

PageSize = pageSize;
PageIndex = index;
TotalItemCount = totalCount.HasValue ? totalCount.Value : realTotalCount;
PageCount = TotalItemCount > 0 ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) : 0;

HasPreviousPage = (PageIndex > 0);
HasNextPage = (PageIndex < (PageCount - 1));
IsFirstPage = (PageIndex <= 0);
IsLastPage = (PageIndex >= (PageCount - 1));

if (TotalItemCount <= 0)
return;

var realTotalPages = (int)Math.Ceiling(realTotalCount / (double)PageSize);

if (realTotalCount < TotalItemCount && realTotalPages <= PageIndex)
AddRange(source.Skip((realTotalPages - 1) * PageSize).Take(PageSize));
else
AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
}

#region IPagedList Members

public int PageCount { get; private set; }
public int TotalItemCount { get; private set; }
public int PageIndex { get; private set; }
public int PageNumber { get { return PageIndex + 1; } }
public int PageSize { get; private set; }
public bool HasPreviousPage { get; private set; }
public bool HasNextPage { get; private set; }
public bool IsFirstPage { get; private set; }
public bool IsLastPage { get; private set; }

#endregion
}

3..  Than we just need a simple extension

 

 

public static class PagingExtensions
{
#region IEnumerable<T> extensions

public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex,
 int pageSize, int? totalCount = null)
{     return new PagedList<T>(source, pageIndex, pageSize, totalCount);
}

#endregion
}

4. and we are ready to go:::::

private static IList<Group_tbl> lstGroups = new List<Group_tbl>();
var pagedList=   lstGroups.ToPagedList(currentPageIndex, pageSize);

 

 

Enjoy..