Running against Northwind and StockDB

This commit is contained in:
2021-01-10 18:07:51 +01:00
parent dd530c02a9
commit c7254389e4
8 changed files with 159 additions and 4 deletions

View File

@ -2,5 +2,6 @@
<configuration> <configuration>
<connectionStrings> <connectionStrings>
<add name="cn" connectionString="Data Source=oemansv7win;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> <add name="cn" connectionString="Data Source=oemansv7win;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="StockDB" connectionString="Data Source=oemansv7win;Initial Catalog=StockDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings> </connectionStrings>
</configuration> </configuration>

View File

@ -31,7 +31,12 @@ namespace RepositoryPattern
{ {
this.dataGridView = new System.Windows.Forms.DataGridView(); this.dataGridView = new System.Windows.Forms.DataGridView();
this.lblTotalRecords = new System.Windows.Forms.Label(); this.lblTotalRecords = new System.Windows.Forms.Label();
this.gB1 = new System.Windows.Forms.GroupBox();
this.rdbStock = new System.Windows.Forms.RadioButton();
this.rdbNorth = new System.Windows.Forms.RadioButton();
this.btnReload = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.gB1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// dataGridView // dataGridView
@ -52,11 +57,55 @@ namespace RepositoryPattern
this.lblTotalRecords.TabIndex = 2; this.lblTotalRecords.TabIndex = 2;
this.lblTotalRecords.Text = "Total Records: ???"; this.lblTotalRecords.Text = "Total Records: ???";
// //
// gB1
//
this.gB1.Controls.Add(this.btnReload);
this.gB1.Controls.Add(this.rdbStock);
this.gB1.Controls.Add(this.rdbNorth);
this.gB1.Location = new System.Drawing.Point(220, 410);
this.gB1.Name = "gB1";
this.gB1.Size = new System.Drawing.Size(248, 35);
this.gB1.TabIndex = 3;
this.gB1.TabStop = false;
//
// rdbStock
//
this.rdbStock.AutoSize = true;
this.rdbStock.Location = new System.Drawing.Point(98, 9);
this.rdbStock.Name = "rdbStock";
this.rdbStock.Size = new System.Drawing.Size(54, 19);
this.rdbStock.TabIndex = 1;
this.rdbStock.Text = "Stock";
this.rdbStock.UseVisualStyleBackColor = true;
//
// rdbNorth
//
this.rdbNorth.AutoSize = true;
this.rdbNorth.Checked = true;
this.rdbNorth.Location = new System.Drawing.Point(7, 9);
this.rdbNorth.Name = "rdbNorth";
this.rdbNorth.Size = new System.Drawing.Size(84, 19);
this.rdbNorth.TabIndex = 0;
this.rdbNorth.TabStop = true;
this.rdbNorth.Text = "NorthWind";
this.rdbNorth.UseVisualStyleBackColor = true;
//
// btnReload
//
this.btnReload.Location = new System.Drawing.Point(158, 9);
this.btnReload.Name = "btnReload";
this.btnReload.Size = new System.Drawing.Size(75, 23);
this.btnReload.TabIndex = 2;
this.btnReload.Text = "Reload";
this.btnReload.UseVisualStyleBackColor = true;
this.btnReload.Click += new System.EventHandler(this.btnReload_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.gB1);
this.Controls.Add(this.lblTotalRecords); this.Controls.Add(this.lblTotalRecords);
this.Controls.Add(this.dataGridView); this.Controls.Add(this.dataGridView);
this.MinimizeBox = false; this.MinimizeBox = false;
@ -65,6 +114,8 @@ namespace RepositoryPattern
this.Text = "Repository Pattern"; this.Text = "Repository Pattern";
this.Load += new System.EventHandler(this.Form1_Load); this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.gB1.ResumeLayout(false);
this.gB1.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -74,6 +125,10 @@ namespace RepositoryPattern
private System.Windows.Forms.DataGridView dataGridView; private System.Windows.Forms.DataGridView dataGridView;
private System.Windows.Forms.Label lblTotalRecords; private System.Windows.Forms.Label lblTotalRecords;
private System.Windows.Forms.GroupBox gB1;
private System.Windows.Forms.RadioButton rdbStock;
private System.Windows.Forms.RadioButton rdbNorth;
private System.Windows.Forms.Button btnReload;
} }
} }

View File

@ -14,16 +14,35 @@ namespace RepositoryPattern
public partial class Form1 : Form public partial class Form1 : Form
{ {
IProductRepository _productRepository; IProductRepository _productRepository;
public Form1(IProductRepository productRepository) private readonly IStockMemberRepository _stockMemberRepository;
public Form1(IProductRepository productRepository,IStockMemberRepository stockMemberRepository)
{ {
InitializeComponent(); InitializeComponent();
_productRepository = productRepository; _productRepository = productRepository;
_stockMemberRepository = stockMemberRepository;
} }
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
dataGridView.DataSource = _productRepository.GetProducts(); ReloadData();
}
private void ReloadData()
{
if (rdbNorth.Checked)
{
dataGridView.DataSource = _productRepository.GetProducts(); }
else if(rdbStock.Checked)
{
dataGridView.DataSource = _stockMemberRepository.GetStocks();
}
lblTotalRecords.Text = $"Total records: {dataGridView.RowCount}"; lblTotalRecords.Text = $"Total records: {dataGridView.RowCount}";
} }
private void btnReload_Click(object sender, EventArgs e)
{
ReloadData();
}
} }
} }

View File

@ -22,7 +22,7 @@ namespace RepositoryPattern
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Container = Configure(); Container = Configure();
Application.Run(new Form1(Container.Resolve<IProductRepository>())); Application.Run(new Form1(Container.Resolve<IProductRepository>(),Container.Resolve<IStockMemberRepository>()));
} }
/// <summary> /// <summary>
/// Setting dependency injection /// Setting dependency injection
@ -32,6 +32,7 @@ namespace RepositoryPattern
{ {
var builder = new ContainerBuilder(); var builder = new ContainerBuilder();
builder.RegisterType<ProductRepository>().As<IProductRepository>(); builder.RegisterType<ProductRepository>().As<IProductRepository>();
builder.RegisterType<StockMemberRepository>().As<IStockMemberRepository>();
builder.RegisterType<Form1>(); builder.RegisterType<Form1>();
return builder.Build(); return builder.Build();
} }

View File

@ -0,0 +1,14 @@
using StockDomain;
using System;
using System.Collections.Generic;
namespace StockDal.Interface
{
public interface IStockMemberRepository
{
IEnumerable<StockMember> GetStocks();
bool Insert(StockMember stockMember);
bool Update(StockMember stockMember);
bool Delete(string stockMemberId);
}
}

View File

@ -11,4 +11,8 @@ namespace RepositoryPattern
{ {
public static string ConnectionString => ConfigurationManager.ConnectionStrings["cn"].ConnectionString; public static string ConnectionString => ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
} }
public class StockDBConnection
{
public static string ConnectionString => ConfigurationManager.ConnectionStrings["StockDB"].ConnectionString;
}
} }

View File

@ -0,0 +1,48 @@
using Dapper;
using RepositoryPattern;
using StockDal.Interface;
using StockDomain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace StockDal
{
public class StockMemberRepository : IStockMemberRepository
{
public bool Delete(string stockMemberId)
{
throw new NotImplementedException();
}
public bool Insert(StockMember stockMember)
{
throw new NotImplementedException();
}
public bool Update(StockMember stockMember)
{
throw new NotImplementedException();
}
IEnumerable<StockMember> IStockMemberRepository.GetStocks()
{
using System.Data.IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString);
if (db.State == ConnectionState.Closed)
db.Open();
return db.Query<StockMember>(@"SELECT Id
,StockId
,StockExtId
,BuyValue
,BuyDate
,ActValue
,ActDate
,SoldValue
,SoldDate
,Comment
,PostAmount
FROM dbo.StockMember", commandType: CommandType.Text);
}
}
}

View File

@ -5,7 +5,20 @@ using System.Text;
namespace StockDomain namespace StockDomain
{ {
class StockMember public class StockMember
{ {
public int Id { get; set; }
public string StockId { get; set; }
public string StockExtId { get; set; }
public decimal BuyValue { get; set; }
public DateTime BuyDate { get; set; }
public long BuytAmount { get; set; }
public decimal ActValue { get; set; }
public DateTime ActDate { get; set; }
public long ActAmount { get; set; }
public decimal SoldValue { get; set; }
public DateTime SoldDate { get; set; }
// public string PostId { get; set; }
public string Comment { get; set; }
} }
} }