August, 2013

  • Wriju's BLOG

    Using Repository Pattern in Entity Framework

    • 8 Comments

    One of the most common pattern is followed in the world of Entity Framework is “Repository Pattern”. Since this is something which is heavily used and being practiced, I am not going to talk about the core pattern. Rather, try to show how one can implement it.

    Objectives

    As mentioned in http://msdn.microsoft.com/en-us/library/ff649690.aspx

    • You want to maximize the amount of code that can be tested with automation and to isolate the data layer to support unit testing.
    • You access the data source from many locations and want to apply centrally managed, consistent access rules and logic.
    • You want to implement and centralize a caching strategy for the data source.
    • You want to improve the code's maintainability and readability by separating business logic from data or service access logic.
    • You want to use business entities that are strongly typed so that you can identify problems at compile time instead of at run time.
    • You want to associate a behavior with the related data. For example, you want to calculate fields or enforce complex relationships or business rules between the data elements within an entity.
    • You want to apply a domain model to simplify complex business logic.

    Simple approach to ADO.NET Entity Framework

    Let’s have one domain class called “Employee”

    public class Employee
    
    {
    
        public int Id { get; set; }
    
        public string FullName { get; set; }
    
    }
    Now using this we will have a simple context class
    public class HRContext : DbContext
    
    {        
    
        public DbSet<DomainClasses.Employee> Employees { get; set; }
    
    }
    After that, define the repository interface IEmployeeRepository
    public interface IEmployeeRepository : IDisposable
    
    {
    
        IQueryable<Employee> All { get; }
    
        IQueryable<Employee> AllIncluding(params Expression<Func<Employee, object>>[] includeProperties);
    
        Employee Find(int id);
    
        void InsertOrUpdate(Employee employee);
    
        void Delete(int id);
    
        void Save();
    
    }
    Then the Repository class called EmployeeRepository
    public class EmployeeRepository : IEmployeeRepository
    
    {
    
        HRContext context = new HRContext();
    
        public IQueryable<Employee> All
    
        {
    
            get { return context.Employees; }
    
        }
    
        public IQueryable<Employee> AllIncluding(params Expression<Func<Employee, object>>[] includeProperties)
    
        {
    
            IQueryable<Employee> query = context.Employees;
    
            foreach (var includeProperty in includeProperties) {
    
                query = query.Include(includeProperty);
    
            }
    
            return query;
    
        }
    
        public Employee Find(int id)
    
        {
    
            return context.Employees.Find(id);
    
        }
    
        public void InsertOrUpdate(Employee employee)
    
        {
    
            if (employee.Id == default(int)) {
    
                // New entity
    
                context.Employees.Add(employee);
    
            } else {
    
                // Existing entity
    
                context.Entry(employee).State = EntityState.Modified;
    
            }
    
        }
    
        public void Delete(int id)
    
        {
    
            var employee = context.Employees.Find(id);
    
            context.Employees.Remove(employee);
    
        }
    
        public void Save()
    
        {
    
            context.SaveChanges();
    
        }
    
        public void Dispose() 
    
        {
    
            context.Dispose();
    
        }
    
    }
    Then you should be implementing it in your apps (any type Windows or Web), like a Console Application
    namespace ConsoleApplication
    
    {
    
        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                GetSomeEmployee();
    
            }
    
            private static void IntiateData()
    
            {
    
                using (var repo = new EmployeeRepository())
    
                {
    
                    Employee em = new Employee() { FullName = "Wriju" };
    
                    repo.InsertOrUpdate(em);
    
                    repo.Save();
    
                }
    
            }
    
            private static void GetSomeEmployee()
    
            {
    
                using (var repo = new EmployeeRepository()) 
    
                {
    
                    foreach (var emp in repo.All)
    
                    {
    
                        Console.WriteLine("{0} - {1}", emp.Id, emp.FullName);
    
                    }
    
                }            
    
            }
    
        }
    
    }
    This obviously simple approach. The recommended options are to make the Repository generic and handle the related entities. I will discuss about them later.
    Namoskar!!!
  • Wriju's BLOG

    Patterns and Practices : Dependency Injection with Unity–Just Released

    • 1 Comments

    Just Released!!!

    Patterns and Practices guidance on Dependency Injection with Unity

    clip_image001[4] 

     

    PDF

     

    EPUB

     

    MOBI

     

    HARD COPY

     

    Inside the guide:

    Foreword by Chris Tavares

    Chapter 1 – Introduction

    Chapter 2 – Dependency Injection

    Chapter 3 – Dependency Injection with Unity

    Chapter 4 – Interception

    Chapter 5 – Interception using Unity

    Chapter 6 – Extending Unity

    Tales from the Trenches: Using Unity

    Tales from the Trenches: One User's Story – Customizing Unity

    Tales from the Trenches: Using Unity in a Windows Store app

    Appendix  – Unity and Windows Store apps

     

     

     

    The guide is a primer on using dependency injection with Unity – a lightweight extensible dependency injection container built by the Microsoft patterns & practices team. It covers various styles of dependency injection and also additional capabilities of Unity container, such as object lifetime management, interception, and registration by convention. It also discusses the advanced topics of enhancing Unity with your custom extensions.

     

    The guide contains plenty of trade-off discussions and tips and tricks for managing your application cross-cutting concerns and making the most out of both dependency injection and Unity. These are accompanied by a real world example that will help you master the techniques. Keep in mind that Unity can be used in a wide range of application types such as desktop, web, services, and cloud.

     

    We encourage you to experiment with the sample code and think beyond the scenarios discussed in the guide.

    In addition, the guide includes the Tales from the Trenches – a collection of case studies that offer a different perspective through the eyes of developers working on the real world projects and sharing their experiences. These chapters make clear the range of scenarios in which you can use Unity, and also highlight its ease of use and flexibility.

     

    Whether you are a seasoned developer or just starting your development journey, we hope this guide will be worth your time studying it. We hope you discover that Unity container adds significant benefits to your applications and helps you to achieve the goals of maintainability, testability, flexibility, and extensibility in your own projects.

     

    Namoskar!!!
  • Wriju's BLOG

    Microsoft Patterns and Practices Guidance on Solution Development Fundamentals

    • 1 Comments

     

    • Enterprise Library. Enterprise Library is a collection of application blocks that address common cross-cutting concerns that developers face when developing applications. The latest version of Enterprise Library (version 6) was released in April 2013 and includes two new application blocks (Semantic Logging Application Block and Transient Fault Handling Application Block) and many improvements.

    The Enterprise Library 5.0 Integration Pack for Windows Azure is a collection of reusable application blocks that address common cross-cutting concerns in enterprise software development. The Microsoft Enterprise Library Integration Pack for Windows Azure is an extension to Microsoft Enterprise Library 5.0 that can be used with the Windows Azure technology platform. It includes the Autoscaling Application Block, Transient Fault Handling Application Block, blob configuration source, protected configuration provider, and learning materials.

    • Unity. Unity is a lightweight and extensible dependency injection container with support for interception. We recently released Unity 3.0 (also part of the latest version of Enterprise Library) which added a number of key features, including registration by convention and support for Windows Store apps. Unity provides a mature and stable foundation for building high-quality, flexible, pattern-based libraries and reference implementations. It facilitates loosely-coupled design and help improve testability.
    • Parallel Programming with Microsoft .NET This book describes patterns for parallel programming, with code examples, that use the new parallel programming support in the Microsoft® .NET Framework 4. This support is commonly referred to as the Parallel Extensions. You can use the patterns described in this book to improve your application's performance on multicore computers. Adopting the patterns in your code makes your application run faster today and also helps prepare for future hardware environments, which are expected to have an increasingly parallel computing architecture
    • Parallel Programming with Microsoft Visual C++ This book describes patterns for parallel programming, with code examples, that use the parallel programming support in the Microsoft® Visual C++. The Parallel Patterns Library (PPL) and the Asynchronous Agents Library introduce a new programming model for parallelism that significantly simplifies the job. Behind the scenes are sophisticated algorithms that dynamically distribute computations on multicore architectures. In addition, Microsoft® Visual Studio® 2010 development system includes debugging and analysis tools to support the new parallel programming model. You can use the patterns described in this book to improve your application's performance on multicore computers. Adopting the patterns in your code makes your application run faster today and also helps prepare for future hardware environments, which are expected to have an increasingly parallel computing architecture
    • CQRS Journey. This guidance is designed to help you get started with the Command & Query Responsibility Segregation and the Event Sourcing patterns. The guide is a journal that describes the experiences of a development team with no prior CQRS proficiency in building, deploying (to Windows Azure), and maintaining a sample real-world complex enterprise system to showcase various CQRS and ES concepts and techniques.
    • A Guide to Claims–based Identity and Access Control, 2nd Edition. This guide gives you enough information to evaluate claims-based identity as a possible option when you're planning a new application or making changes to an existing one. It is intended for any architect, developer, or information technology (IT) professional who designs, builds, or operates Web applications and services that require identity information about their users.
    • Testing for Continuous Delivery with Visual Studio 2012. This guide provides an end-to-end walkthrough of the testing scenarios supported by the Visual Studio 2012 infrastructure. It will help testers and developers use Team Foundation Server effectively as an application lifecycle management solution for testing and supporting products.
    • Microsoft Application Architecture Guide, 2nd Edition. This guide provides design-level guidance for the architecture and design of applications built on the .NET Framework.
    • Performance Testing Guidance for Web Applications. This guide shows you an end-to-end approach for implementing performance testing for your Web applications.

    Technical Articles

     

    Namoskar!!!

  • Wriju's BLOG

    Windows Phone Security : Encrypt and Decrypt

    • 0 Comments

    Often times you may want to secure the user data. However, there is one native API to support

    using System.Security.Cryptography;
    
    using System.Text;
    byte[] secretPassword; // to store it in a variable 
    
    private void btnEncrypt_Click(object sender, RoutedEventArgs e)
    
    {
    
        //Convert the text to byte[] 
    
        byte[] clearPassword = Encoding.UTF8.GetBytes(txtData.Text);
    
        //Encrypt
    
        secretPassword = ProtectedData.Protect(clearPassword, null);
    
        txtDecryptText.Text = "Encrypted...";
    
        txtData.Text = "";
    
    }
    
    private void btnDecrypt_Click(object sender, RoutedEventArgs e)
    
    {
    
        byte[] protectedPassowrd = secretPassword;
    
                
    
        //Decrypt the text
    
        byte[] unprotectPassword = ProtectedData.Unprotect(protectedPassowrd, null);
    
        txtDecryptText.Text = Encoding.UTF8.GetString(unprotectPassword, 0, unprotectPassword.Length);
    
        txtDecryptText.Text = "Decrypted...";
    
    }

    Namoskar!!!

  • Wriju's BLOG

    Windows Phone Storing string in Application Setting

    • 0 Comments

    For small key-value pair like storage we can use Application Setting and store the string. It is simple and easy to retrieve.

    // Storing the data in Application Settings
    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
        IsolatedStorageSettings.ApplicationSettings["TheName"] = txtName.Text;
        IsolatedStorageSettings.ApplicationSettings.Save(); //Important to call to Save it
    }
    
    //Getting the data from Application Settings
    private void btnRead_Click(object sender, RoutedEventArgs e)
    {
        if(IsolatedStorageSettings.ApplicationSettings.Contains("TheName"))
        {
            txtRead.Text = (string)IsolatedStorageSettings.ApplicationSettings["TheName"];
    
            //To remove 
            IsolatedStorageSettings.ApplicationSettings.Remove("TheName");
        }            
    }
      

    Namoskar!!!

  • Wriju's BLOG

    Windows Store Apps and SQLite for x86, x64 and ARM

    • 2 Comments

    There are bunch of resources on how to use SQLite. The one which I came across and liked is http://jesseliberty.com/2013/03/14/windows-8-storing-data-with-sqlite/. If you follow step by step you will be able to reach the desired output. 

     

    What is more challenging is the final package when you create for Windows Store. SQLite DLL compiles per platform. So you need to compile separately for x86, x64 and ARM. I took help from Sid who created Bumpy and used Bing Map DLL. He too had the same issue.

    After we had a discussion we did the below thing.

    Created one project lets say Project_x64 and selected platform x64 by going to the project property

    image

     

    After that I created one more project under the same solution let’s assume Project_x32. Here is the trick. Added all the code file as LINK from previous project

    image

    This time the platform is x86. I did similar thing for ARM.

    Benefit: Whenever I change code I do it in one file and all the other project has the immediate affect. After that we I have to create the package I do it per platform like below

    image

    That’s it!!! Your app is ready for all the 3 platforms and you need to upload all the packages.

    Namoskar!!!

  • Wriju's BLOG

    Windows Azure WebSites–Best Presentation

    • 0 Comments

    This one is the best http://channel9.msdn.com/Events/windowsazure/Learn-2012TechEd-EU/WebSites

    image

    Namoskar!!!

  • Wriju's BLOG

    Windows Phone Development Absolute Beginners Series

    • 2 Comments

    It is amazing series in Channel 9 demonstrating the Windows Phone Development step by step. Watch it here http://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners

    image

    Also the source code and instruction manual is available at

    Source Code: http://aka.ms/absbeginnerdevwp8
    PDF Version: http://aka.ms/absbeginnerdevwp8pdf

    Namoskar!!!

  • Wriju's BLOG

    Build your Windows Phone Application without writing Code

    • 0 Comments

    This is pretty cool. You do not need to be a developer to be able to publish an App to Windows Phone Store. More details visit http://apps.windowsstore.com/default.htm

    image

    Namoskar!!!

  • Wriju's BLOG

    Windows Phone Developer account at $19

    • 0 Comments

    This offer is valid till August 27. See for more http://dev.windowsphone.com/en-us/join

    image

    Namoskar!!!

Page 1 of 1 (10 items)