Using a DataProvider achitecture with CSLA.NET

Using a DataProvider achitecture with CSLA.NET

Old forum URL: forums.lhotka.net/forums/t/3037.aspx


Matthew Yost posted on Wednesday, June 13, 2007

I'm trying to use the CSLA.NET framework with my companies existing data provider architecture.  The architecture is that we have a data provider component that handles all database interaction and is configurable using a configuration file to switch the database and data provider it will use.  The problem I am having is that in the examples, all data access code was written in the Business Objects themselves and I can't do this.  What is the best way for me to be able to implement this framework w/o having the SQL in the BO?  Below is a code sample:

 

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Text;

    4 using Csla;

    5 using Csla.Data;

    6 

    7 namespace TPL

    8 {

    9     [Serializable]

   10     public class TPLAgency : BusinessBase<TPLAgency>

   11     {

   12         private int _intAgencyID;

   13         private string _strAgencyName;

   14         private string _strAgencyContact;

   15         private string _strAddressLine1;

   16         private string _strAddressLine2;

   29 

   30         public TPLAgency()

   31         {

   32             _intAgencyID = 0;

   33         }

   34 

   35         #region Properties

   36         /// <summary>

   37         /// Gets or sets the agency ID.

   38         /// </summary>

   39         /// <value>The agency ID.</value>

   40         [System.ComponentModel.DataObjectField(true,true)]

   41         public int AgencyID

   42         {

   43             get

   44             {

   45                 CanReadProperty("AgencyID", true);

   46                 return _intAgencyID;

   47             }

   48             set

   49             {

   50                 CanWriteProperty("AgencyID", true);

   51                 _intAgencyID = value;

   52             }

   53         }

   54 

   55         /// <summary>

   56         /// Gets or sets the name of the agency.

   57         /// </summary>

   58         /// <value>The name of the agency.</value>

   59         public string AgencyName

   60         {

   61             get

   62             {

   63                 CanReadProperty("AgencyName", true);

   64                 return _strAgencyName;

   65             }

   66             set

   67             {

   68                 CanWriteProperty("AgencyName", true);

   69                 _strAgencyName = value;

   70             }

   71         }

   72 

   73         /// <summary>

   74         /// Gets or sets the agency contact.

   75         /// </summary>

   76         /// <value>The agency contact.</value>

   77         public string AgencyContact

   78         {

   79             get

   80             {

   81                 CanReadProperty("AgencyContact", true);

   82                 return _strAgencyContact;

   83             }

   84             set

   85             {

   86                 CanWriteProperty("AgencyContact", true);

   87                 _strAgencyContact = value;

   88             }

   89         }

   90 

   91         /// <summary>

   92         /// Gets or sets the address line1.

   93         /// </summary>

   94         /// <value>The address line1.</value>

   95         public string AddressLine1

   96         {

   97             get

   98             {

   99                 CanReadProperty("AddressLine1", true);

  100                 return _strAddressLine1;

  101             }

  102             set

  103             {

  104                 CanWriteProperty("AddressLine1", true);

  105                 _strAddressLine1 = value;

  106             }

  107         }

  108 

  109         /// <summary>

  110         /// Gets or sets the address line2.

  111         /// </summary>

  112         /// <value>The address line2.</value>

  113         public string AddressLine2

  114         {

  115             get

  116             {

  117                 CanReadProperty("AddressLine2", true);

  118                 return _strAddressLine2;

  119             }

  120             set

  121             {

  122                 CanWriteProperty("AddressLine2", true);

  123                 _strAddressLine2 = value;

  124             }

  125         }

  324 

  325         protected override object GetIdValue()

  326         {

  327             return _intAgencyID;

  328         }

  329 

  330         #endregion

  370         #region Factory Methods

  371 

  372         public static TPLAgency NewAgency()

  373         {

  374           return DataPortal.Create<TPLAgency>();

  375         }

  376 

  377         public static TPLAgency GetAgency(int id)

  378         {

  379           return DataPortal.Fetch<TPLAgency>(new Criteria(id));

  380         }

  381 

  382         public static void DeleteAgency(int id)

  383         {

  384           DataPortal.Delete(new Criteria(id));

  385         }

  386 

  387         #endregion

  388         #region Data Access

  389 

  390         [Serializable()]

  391         private class Criteria

  392         {

  393             private int mId;

  394             public int Id

  395             {

  396                 get

  397                 { return mId; }

  398             }

  399             public Criteria(int id)

  400             { mId = id; }

  401         }

  402 

  403         [RunLocal()]

  404         protected override void DataPortal_Create()

  405         {

  406             AgencyID = 0;

  407         }

  408 

  409         private void DataPortal_Fetch(Criteria criteria)

  410         {

  411             TPLAgency a = CoreGlobal.DBInterface.GetAgency(criteria.Id);

  412             _intAgencyID = a.AgencyID;

  413             _strAgencyName = a.AgencyName;

  414             _strAgencyContact = a.AgencyContact;

  415             _strAddressLine1 = a.AddressLine1;

  416             _strAddressLine2 = a.AddressLine2;

  417             _strAddressLine3 = a.AddressLine3;

  418             _strAddressLine4 = a.AddressLine4;

  419             _strAddressLine5 = a.AddressLine5;

  420             _strTown = a.Town;

  421             _strProvince = a.Province;

  422             _strRegion = a.Region;

  423             _strTelephone = a.Telephone;

  424             _strFax = a.Fax;

  425             _strEmail = a.Email;

  426             _objCountry = a.Country;

  427         }

  428 

  429         protected override void DataPortal_Insert()

  430         {

  431             // TODO: insert object's data

  432         }

  433 

  434         protected override void DataPortal_Update()

  435         {

  436             // TODO: update object's data

  437         }

  438 

  439         protected override void DataPortal_DeleteSelf()

  440         {

  441             DataPortal_Delete(new Criteria(_intAgencyID));

  442         }

  443 

  444         private void DataPortal_Delete(Criteria criteria)

  445         {

  446             // TODO: delete object's data

  447         }

  448 

  449         #endregion

  450 

  451     }

  452 }

Brian Criswell replied on Wednesday, June 13, 2007

Your DataPortal_Fetch method is more or less correct.  Just replace the example's SqlDataProvider code with your companies data access code.  In general however, you will
want to keep the other method calls in the examples that are not directly related to the SqlDataProvider.

nermin replied on Thursday, June 14, 2007

I believe you are looking for "Deep Data" example (a solution). It is somewhere on lhotka.net - I downloaded it couple of months ago but I am having hard time finding it now.

Basically nothing is preventing you from calling your DataProvider objects from Csla serialization methods, and populating updating your Csla objects that way.


Nermin Dibek
Magenic Technologies

Nobody's more serious about Microsoft.

________________________________

From: Matthew Yost [mailto:cslanet@lhotka.net]
Sent: Wed 6/13/2007 11:05 AM
To: Nermin Dibek
Subject: [CSLA .NET] Using a DataProvider achitecture with CSLA.NET



I'm trying to use the CSLA.NET framework with my companies existing data provider architecture. The architecture is that we have a data provider component that handles all database interaction and is configurable using a configuration file to switch the database and data provider it will use. The problem I am having is that in the examples, all data access code was written in the Business Objects themselves and I can't do this. What is the best way for me to be able to implement this framework w/o having the SQL in the BO? Below is a code sample:



1 using System;

2 using System.Collections.Generic;

3 using System.Text;

4 using Csla;

5 using Csla.Data;

6

7 namespace TPL

8 {

9 [Serializable]

10 public class TPLAgency : BusinessBase

11 {

12 private int _intAgencyID;

13 private string _strAgencyName;

14 private string _strAgencyContact;

15 private string _strAddressLine1;

16 private string _strAddressLine2;

17 private string _strAddressLine3;

18 private string _strAddressLine4;

19 private string _strAddressLine5;

20 private string _strTown;

21 private string _strProvince;

22 private string _strRegion;

23 private string _strPostalCode;

24 private string _strCounty;

25 private string _strTelephone;

26 private string _strFax;

27 private string _strEmail;

28 private TPLCountry _objCountry = TPLCountry.NewDPLCountry();

29

30 public TPLAgency()

31 {

32 _intAgencyID = 0;

33 }

34

35 #region Properties

36 ///

37 /// Gets or sets the agency ID.

38 ///

39 /// The agency ID.

40 [System.ComponentModel.DataObjectField(true,true)]

41 public int AgencyID

42 {

43 get

44 {

45 CanReadProperty("AgencyID", true);

46 return _intAgencyID;

47 }

48 set

49 {

50 CanWriteProperty("AgencyID", true);

51 _intAgencyID = value;

52 }

53 }

54

55 ///

56 /// Gets or sets the name of the agency.

57 ///

58 /// The name of the agency.

59 public string AgencyName

60 {

61 get

62 {

63 CanReadProperty("AgencyName", true);

64 return _strAgencyName;

65 }

66 set

67 {

68 CanWriteProperty("AgencyName", true);

69 _strAgencyName = value;

70 }

71 }

72

73 ///

74 /// Gets or sets the agency contact.

75 ///

76 /// The agency contact.

77 public string AgencyContact

78 {

79 get

80 {

81 CanReadProperty("AgencyContact", true);

82 return _strAgencyContact;

83 }

84 set

85 {

86 CanWriteProperty("AgencyContact", true);

87 _strAgencyContact = value;

88 }

89 }

90

91 ///

92 /// Gets or sets the address line1.

93 ///

94 /// The address line1.

95 public string AddressLine1

96 {

97 get

98 {

99 CanReadProperty("AddressLine1", true);

100 return _strAddressLine1;

101 }

102 set

103 {

104 CanWriteProperty("AddressLine1", true);

105 _strAddressLine1 = value;

106 }

107 }

108

109 ///

110 /// Gets or sets the address line2.

111 ///

112 /// The address line2.

113 public string AddressLine2

114 {

115 get

116 {

117 CanReadProperty("AddressLine2", true);

118 return _strAddressLine2;

119 }

120 set

121 {

122 CanWriteProperty("AddressLine2", true);

123 _strAddressLine2 = value;

124 }

125 }

126

127 ///

128 /// Gets or sets the address line3.

129 ///

130 /// The address line3.

131 public string AddressLine3

132 {

133 get

134 {

135 CanReadProperty("AddressLine3", true);

136 return _strAddressLine3;

137 }

138 set

139 {

140 CanWriteProperty("AddressLine3", true);

141 _strAddressLine3 = value;

142 }

143 }

144

145 ///

146 /// Gets or sets the address line4.

147 ///

148 /// The address line4.

149 public string AddressLine4

150 {

151 get

152 {

153 CanReadProperty("AddressLine4", true);

154 return _strAddressLine4;

155 }

156 set

157 {

158 CanWriteProperty("AddressLine4", true);

159 _strAddressLine4 = value;

160 }

161 }

162

163 ///

164 /// Gets or sets the address line5.

165 ///

166 /// The address line5.

167 public string AddressLine5

168 {

169 get

170 {

171 CanReadProperty("AddressLine5", true);

172 return _strAddressLine5;

173 }

174 set

175 {

176 CanWriteProperty("AddressLine5", true);

177 _strAddressLine5 = value;

178 }

179 }

180

181 ///

182 /// Gets or sets the town.

183 ///

184 /// The town.

185 public string Town

186 {

187 get

188 {

189 CanReadProperty("Town", true);

190 return _strTown;

191 }

192 set

193 {

194 CanWriteProperty("Town", true);

195 _strTown = value;

196 }

197 }

198

199 ///

200 /// Gets or sets the province.

201 ///

202 /// The province.

203 public string Province

204 {

205 get

206 {

207 CanReadProperty("Province", true);

208 return _strProvince;

209 }

210 set

211 {

212 CanWriteProperty("Province", true);

213 _strProvince = value;

214 }

215 }

216

217 ///

218 /// Gets or sets the region.

219 ///

220 /// The region.

221 public string Region

222 {

223 get

224 {

225 CanReadProperty("Region", true);

226 return _strRegion;

227 }

228 set

229 {

230 CanWriteProperty("Region", true);

231 _strRegion = value;

232 }

233 }

234

235 ///

236 /// Gets or sets the postal code.

237 ///

238 /// The postal code.

239 public string PostalCode

240 {

241 get

242 {

243 CanReadProperty("PostalCode", true);

244 return _strPostalCode;

245 }

246 set

247 {

248 CanWriteProperty("PostalCode", true);

249 _strPostalCode = value;

250 }

251 }

252

253 public string Telephone

254 {

255 get

256 {

257 CanReadProperty("Telephone", true);

258 return _strTelephone;

259 }

260 set

261 {

262 CanWriteProperty("Telephone", true);

263 if (_strTelephone != value)

264 {

265 _strTelephone = value;

266 PropertyHasChanged();

267 }

268 }

269 }

270

271 public string Fax

272 {

273 get

274 {

275 CanReadProperty("Fax", true);

276 return _strFax;

277 }

278 set

279 {

280 CanWriteProperty("Fax", true);

281 if (_strFax != value)

282 {

283 _strFax = value;

284 PropertyHasChanged();

285 }

286 }

287 }

288

289 public string Email

290 {

291 get

292 {

293 CanReadProperty("Email",true);

294 return _strEmail;

295 }

296 set

297 {

298 CanWriteProperty("Email",true);

299 if (_strEmail != value)

300 {

301 _strEmail = value;

302 PropertyHasChanged();

303 }

304 }

305 }

306

307 ///

308 /// Gets or sets the country.

309 ///

310 /// The country.

311 public TPLCountry Country

312 {

313 get

314 {

315 CanReadProperty("Country", true);

316 return _objCountry;

317 }

318 set

319 {

320 CanWriteProperty("Country", true);

321 _objCountry = value;

322 }

323 }

324

325 protected override object GetIdValue()

326 {

327 return _intAgencyID;

328 }

329

330 #endregion

331

332 #region Validation Rules

333

334 protected override void AddBusinessRules()

335 {

336 // TODO: add validation rules

337 }

338

339 #endregion

340

341 #region Authorization Rules

342

343 protected override void AddAuthorizationRules()

344 {

345 // TODO: add authorization rules

346 }

347

348 public static bool CanAddObject()

349 {

350 return true;

351 }

352

353 public static bool CanGetObject()

354 {

355 return true;

356 }

357

358 public static bool CanDeleteObject()

359 {

360 return true;

361 }

362

363 public static bool CanEditObject()

364 {

365 return true;

366 }

367

368 #endregion

369

370 #region Factory Methods

371

372 public static TPLAgency NewAgency()

373 {

374 return DataPortal.Create();

375 }

376

377 public static TPLAgency GetAgency(int id)

378 {

379 return DataPortal.Fetch(new Criteria(id));

380 }

381

382 public static void DeleteAgency(int id)

383 {

384 DataPortal.Delete(new Criteria(id));

385 }

386

387 #endregion

388 #region Data Access

389

390 [Serializable()]

391 private class Criteria

392 {

393 private int mId;

394 public int Id

395 {

396 get

397 { return mId; }

398 }

399 public Criteria(int id)

400 { mId = id; }

401 }

402

403 [RunLocal()]

404 protected override void DataPortal_Create()

405 {

406 AgencyID = 0;

407 }

408

409 private void DataPortal_Fetch(Criteria criteria)

410 {

411 TPLAgency a = CoreGlobal.DBInterface.GetAgency(criteria.Id);

412 _intAgencyID = a.AgencyID;

413 _strAgencyName = a.AgencyName;

414 _strAgencyContact = a.AgencyContact;

415 _strAddressLine1 = a.AddressLine1;

416 _strAddressLine2 = a.AddressLine2;

417 _strAddressLine3 = a.AddressLine3;

418 _strAddressLine4 = a.AddressLine4;

419 _strAddressLine5 = a.AddressLine5;

420 _strTown = a.Town;

421 _strProvince = a.Province;

422 _strRegion = a.Region;

423 _strTelephone = a.Telephone;

424 _strFax = a.Fax;

425 _strEmail = a.Email;

426 _objCountry = a.Country;

427 }

428

429 protected override void DataPortal_Insert()

430 {

431 // TODO: insert object's data

432 }

433

434 protected override void DataPortal_Update()

435 {

436 // TODO: update object's data

437 }

438

439 protected override void DataPortal_DeleteSelf()

440 {

441 DataPortal_Delete(new Criteria(_intAgencyID));

442 }

443

444 private void DataPortal_Delete(Criteria criteria)

445 {

446 // TODO: delete object's data

447 }

448

449 #endregion

450

451 }

452 }



RockfordLhotka replied on Thursday, June 14, 2007

nermin:
I believe you are looking for "Deep Data" example (a solution). It is somewhere on lhotka.net - I downloaded it couple of months ago but I am having hard time finding it now.

I do have a search page on my site

http://www.lhotka.net/Search.aspx

Big Smile [:D]

Click here to get to the download page you are looking for.

harmar replied on Saturday, June 16, 2007

There's a very good video on dnrTV featuring rocky, where he talks about the deepdata project and gives answers to your questions.

http://dnrtv.com/default.aspx?showID=60

enjoy!

Copyright (c) Marimer LLC