Introduction

A generic Entity Framework repository pattern implementation.

Code

https://github.com/jinweijie/EF.GenericRepository

Usage

Unzip Database\Database.7z, attach to LocalDB, or restore to your favoriate Sql Server instance and change the connection string in App.Config.
Open solution and run the application.

Base Repository

The base repository exists in Common\AbstractRepository.cs, it handles the basic CRUD operations, for example, methods like Find with pagination, Create, Update, Delete.

The Find method, which has many overloading, handles pagination, sorting, total count. You can use this kind of generic methods for all the entities.

It supports async call and also in the parent-child table scenario, you can pass the include parameter to do the join table.

Derived Repository

For certain business logic, you may need derived repository from the base repository to handle more business logic.

In the sample, you may find Repository\LogRepository.cs which is a repository for Log. In this example, i don’t have additional business logic, so it’s just look like this:

public class LogRepository : AbstractRepository<Log, int>
 {
     public LogRepository(EFContext context)
         : base(context)
     {
     }
 }

And the in the caller code, you can just use it like:

var repo = uow.GetLogRepository(); //get repo from UnitOfWork

var result = repo.Find( GetSpecification(),
                        pageIndex,
                        pageSize,
                        List<SortDescriptor> sortings,
                        l => l.Level); //join the Level table

  Unit of Work

To implement the transaction concept, multiple repository share a common EFContext. You may find the example in Common\EFUnitOfWork.cs. For simplicity, I don’t use and inject dependency, just to demonstrate the concept.

Specification Pattern

In Find method of the generic repository, it accept a ISpecification interface. You can define some Specification for the query to fix the criteria and you may have better control of the query, for example:

public class LogSearchSpecification : ISpecification<Log>
{
    public string LevelName { get; set; }
    public string Message { get; set; }
    public Expression<Func<Log, bool>> ToExpression()
    {
        return log => (log.Level.Name == LevelName || LevelName == "") &&
                      (log.Message.Contains(Message) || Message == "");
    }

    public bool IsSatisfiedBy(Log entity)
    {
        return (entity.Level.Name == LevelName || LevelName == "") &&
               (entity.Message.Contains(Message) || Message == "");
    }
}

Entity Generation

I prefer the database first approach because for complex system, I like to start from the database design and generate the POCO classes. Of course, you may use the code first approach, there’s no limitation.

In the Entity\EF.GenericRepository.Entity.edmx, you can do reverse engineering to generate class from database.

Logging

I like to check what sql the Entity Framework generates so that I can make sure there won’t be some performance issue for example in the for loop, query the database in each iteration.

In the Common\EFContext.cs, there’s a switch EnableTraceSql, it will populate every sql statement which entity framework sends to database. And I use NLog to store the sql statements in the Log table. Every time you hit the Search button in the WinForm example, you may find there are some logs inserted into Log table.

Sample UI to Test

After preparing the database, you can directly compile and run the sample application.

{ 0 comments }

Introduction

Sample for custom pagination using Dapper. Supporting sorting + criteria + pagination.

Source Code

https://github.com/jinweijie/Dapper.PagingSample

Usage

Unzip Database\Database.7z, attach to LocalDB, or restore to your favoriate Sql Server instance and change the connection string in App.Config.
Open solution and run the application.
Caller sample:

 Tuple<IEnumerable<Log>, int> Find(LogSearchCriteria criteria
            , int pageIndex
            , int pageSize
            , List<SortDescriptor> sortings);
  

Two Query Approaches

Since it is a custom paging, you need to get the current page records as well as the total record count. After SQL Server 2012, you can use Fetch/Offset statement to do it in a single query. Prior to SQL Server 2012, you need two queries to achieve this. So in my example, I demonstrate the both approach.

You can check the Fetch/Offset sample in Repository\LogRepository.cs FindWithOffsetFetch. Another one is also in the same class the method name is Find.

Supports Table Join

In the examples, you can find I used some inner join to demostrate to get some entity from multiple table from database.

Sample UI to Test

After preparing the database, you can directly compile and run the sample application.

{ 0 comments }

How to print Chinese characters in ZPL

by Weijie JIN

1. You need to have Chinese font on the printer, use Zebra font downloader application to install. 2. The is a sample code to print Chinese characters: ^XA ^CI28 ^A@N,50,50,E:SIMSUN.FNT ^FO50,110^FDChinese 中文字符^FS ^XZ Another format can be: ^XA ^CI28 ^CWA,E:SIMSUN.FNT ^FO240,30^AAN,36,36^FDChinese 中文字符^FS ^XZ Use CW command to change font, so that you need only to […]

Read the full article →

Hide Back Button of Modern UI for WPF

by Weijie JIN

I searched the web seems no answer for this topic, so i create a style to hide the button: <Style TargetType="{x:Type mui:ModernButton}"> <Style.Triggers> <Trigger Property="Command" Value="NavigationCommands.BrowseBack"> <Setter Property="Visibility" Value="Collapsed" /> </Trigger> </Style.Triggers> </Style> Hope it helps.

Read the full article →

Running Mantis Bug Tracker on MS Sql Server with Unicode Supported

by Weijie JIN

Mantis is a great issue tracking system. But you may encounter some difficulties when you try to run it on Microsoft sql server and with Unicode(like my scenario, Chinese). Here’s the solution: When you try to deploy the MantisBT using build-in install page, you may encounter errors and cannot continue, so I modified the sql, […]

Read the full article →

Pluralsight Continuous Play User Scripts

by Weijie JIN

By default, it will stop after each module when you studying on Pluralsight. If you want to save the mouse click to automatically view the next module. You can use the following methods. Install Greasemonkey addon for firefox: https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/ Install my user scripts: http://userscripts.org/scripts/show/485571 Now when you reach each end of module, the next module […]

Read the full article →

Convert Boot to VHD Hyper-V VM to VMWare Virtual Machine

by Weijie JIN

If you have Boot to VHD VM (which leverage all your hardware) and want to convert it to a VMWare virtual machine, please follow the steps: Download WinImage and install it. Convert the vhd disk to vmdk following the WinImage instruction. Create a new VMWare VM and using the converted disk. You cannot boot the […]

Read the full article →

Fix “Access is denied” issue while installing Sql Server 2008 R2

by Weijie JIN

While installing several machine with Sql Server 2008 R2, I encounter the same problem: “Access is denied”. Apparently this error is related to the account privilege. Here’s the steps to fix this issue: Log on to the computer as a user who has administrative credentials. Click Start, click Run, type Control admintools, and then click […]

Read the full article →

ASP.Net Application Development and Deployment Configuration Best Practices

by Weijie JIN

Do you have the time when you are deploying your application, first you compile, copy the files to the server, change the web.config on the server. Next time, you want to deploy again, compare the server’s configuration to the development one to figure out which ones should be changed? If you experienced the situation above, […]

Read the full article →

Fix Hyper-V Time Synchronization Issue

by Weijie JIN

We’re running Windows Server 2008 R2 for host and Windows Server 2008 R2 as a virtual machine using Hyper-V. For some reason, the time synchronization doesn’t work well. No matter we disable the hyper-v time synchronization option on the host side or enable it. After the gap is more than 5 seconds, some issues will […]

Read the full article →