2013년 8월 9일 금요일

DI컨테이너 사용방법(Ninject)

1. 프로젝트에 다음 클래스를 추가한다.-----------------------------
using Ninject;
using SportsStore.Domain.Abstract;
using System;
using System.Web.Mvc;

namespace SportsStore.WebUI.Infrastructure
{
 public class NinjectControllerFactory : DefaultControllerFactory
 {
  private IKernel ninjectKernel;

  public NinjectControllerFactory()
  {
   ninjectKernel = new StandardKernel();
   AddBindings();
  }
  protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
  {
   return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
  }
  private void AddBindings()
  {
   // 아래와 같이 추가 바인딩을 넣는다
   ninjectKernel.Bind<IProductRepository>().To<IProductRepository>();
  }
 }
}

2. Global.asax.cs의 Application_Start메서드에 다음 코드를 추가한다.
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());


Ninject사용에 필요한 사전준비는 끝났다.

2013년 8월 8일 목요일

SQLite3 Result Codes

#define SQLITE_OK           0   /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR        1   /* SQL error or missing database */
#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
#define SQLITE_PERM         3   /* Access permission denied */
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
#define SQLITE_BUSY         5   /* The database file is locked */
#define SQLITE_LOCKED       6   /* A table in the database is locked */
#define SQLITE_NOMEM        7   /* A malloc() failed */
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL        13   /* Insertion failed because database is full */
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE_EMPTY       16   /* Database is empty */
#define SQLITE_SCHEMA      17   /* The database schema changed */
#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
#define SQLITE_MISMATCH    20   /* Data type mismatch */
#define SQLITE_MISUSE      21   /* Library used incorrectly */
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE_AUTH        23   /* Authorization denied */
#define SQLITE_FORMAT      24   /* Auxiliary database format error */
#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB      26   /* File opened that is not a database file */
#define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
/* end-of-error-codes */

2013년 8월 6일 화요일

함수의 결과로 배열을 반환하는 방법

#include <stdlib.h>
int* mat_mult(int m[], int n);
int main(void)
{
    int ma[4]={1, 3, 5, 7};
    int *mb, i;
    for(i=0;i<4;i++)
        printf("ma[%d]=%2d ", i, ma[i]);
    printf("\n");
    mb=mat_mult(ma, 4);
    for(i=0;i<4;i++)
        printf("mb[%d]=%2d ", i, mb[i]);
    return 0;
}
int* mat_mult(int m[], int n)
{
    int *mc=(int*)malloc(n);
    for(int i=0;i<n;i++)
        mc[i]=m[i]*n;
    return mc;
}

저장프로시저와 함수(C#/LINQ)

저장프로시저를 이용한 다중테이블 입력 방법
예 : Customers와 CustomersInfo 두 테이블 있고 회원가입시
       Customers에는 UserName, Password, Email을 입력하고
       CustomerInfo에는 FullName과 Customers에 Primary Key인 CustomerID를 입력한다면
DB에 Stored Procedure 생성
@Username VARCHAR(10),
@Password CHAR(40),
@Email VARCHAR(100),
@Fullname NVARCHAR(10)
AS
DECLARE @CustomerID INT;
BEGIN TRY
    INSERT INTO [Customers] VALUES(@Username, @Password, @Email);
    SELECT @CustomerID = [CustomerID] FROM [Customers] WHERE [UserName] = @Username;
    INSERT INTO [CustomerInfo]([CustomerID], [FullName]) VALUES(@CustomerID, @Fullname);
END TRY
 
BEGIN CATCH
    RETURN ERROR_NUMBER()
END CATCH
GO


Entity설정
[Function(Name="dbo.InsertCust")]
public int InsertCust(
[Parameter(Name="Username",DbType="VarChar(10)")]string username,
[Parameter(Name="Password",DbType="Char(40)")]string password,
[Parameter(Name="Email",DbType="VarChar(100)")]string email,
[Parameter(Name="FullName",DbType="NVarChar(30)")]string fullname)
{
IExecuteResult result = this.ExecuteMethodCall(
this,
((MethodInfo)(MethodInfo.GetCurrentMethod())),
username, password, email, fullname);
return ((int)(result.ReturnValue));
}


사용방법
CustomersEntity entity = new CustomersEntity(conn);
int insert = entity.InsertCust("tomebody", "han78031", "tome@naver.com", "Tome kim");


SQLite3 내장함수

문자열 조작 함수


함수명 내용
lower(x) 문자열 x를 소문자로 변경한다.
upper(x) 문자열 x를 대문자로 변경한다.
length(x) 인자로 전달된 문자열 x의 길이를 반환한다.
trim(x, y) 문자열 x의 양 끝에서 y를 제거한 결과를 반환한다.
ltrim(x, y) 문자열 x의 왼쪽 끝에서 y를 제거한 결과를 반환한다.
rtrim(x, y) 문자열 x의 오른쪽 긑에서 y를 제거한 결과를 반환한다.
substr(x, y, z) 문자열 x의 y번째 위치부터 z개 만큼의 부분 문자열을 반환한다.
replace(x, y, z) 문자열 x중에서 y와 일치하는 문자열을 z로 교체한다.


수치 계산 함수


함수명 내용
max() 인자 중 최대값을 반환한다.
min() 인자 중 최소값을 반환한다.
abs(x) x의 절대값을 반환한다.
round(x, y) 인자 x를 소수점 y자리에서 반올림한 결과를 반환한다.
random() 임의의 정수를 반환한다.
randomblob(n) n바이트 크기의 임의의 BLOB데이터를 반환한다.
hex(x) x를 16진수 형태로 반환한다.
/// random()
SELECT random();
/// 203219203
집계 함수


함수명 내용
max() 해당 컬럼의 최대값을 반환
min() 해당 컬럼의 최소값을 반환
avg() avg([컬럼명])
sum(), total() 해당 컬럼의 합계를 반환
count() 해당 컬럼의 개수를 반환
group_concat() 해당 컬럼의 문자열을 붙여서 하나의 문자열을 반환