| 1354 | '''MyDbContext.cs''' |
| 1355 | {{{ |
| 1356 | #!c# |
| 1357 | using System; |
| 1358 | using System.Collections.Generic; |
| 1359 | using System.Data.Entity; |
| 1360 | using System.Linq; |
| 1361 | using System.Text; |
| 1362 | using System.Threading.Tasks; |
| 1363 | |
| 1364 | namespace CoDBIS_NakedObjects |
| 1365 | { |
| 1366 | public class MyDbContext : DbContext |
| 1367 | { |
| 1368 | public MyDbContext(String name) : base(name) { } |
| 1369 | public MyDbContext() { } |
| 1370 | public DbSet<User> Users { get; set; } |
| 1371 | |
| 1372 | } |
| 1373 | } |
| 1374 | }}} |
| 1375 | |
| 1376 | '''UserRepository.cs''' |
| 1377 | {{{ |
| 1378 | #!c# |
| 1379 | using NakedObjects; |
| 1380 | using System; |
| 1381 | using System.Collections.Generic; |
| 1382 | using System.ComponentModel; |
| 1383 | using System.Linq; |
| 1384 | using System.Text; |
| 1385 | using System.Threading.Tasks; |
| 1386 | |
| 1387 | namespace CoDBIS_NakedObjects |
| 1388 | { |
| 1389 | |
| 1390 | [DisplayName("Users")] |
| 1391 | public class UserRepository |
| 1392 | { |
| 1393 | |
| 1394 | public IDomainObjectContainer Container { set; protected get; } |
| 1395 | |
| 1396 | public IQueryable<User> AllUsers() |
| 1397 | { |
| 1398 | return Container.Instances<User>(); |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | public User CreateNewUser() |
| 1403 | { |
| 1404 | User obj = Container.NewTransientInstance<User>(); |
| 1405 | //set up any parameters |
| 1406 | //Container.Persist(ref obj); |
| 1407 | return obj; |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | #region Find |
| 1412 | public User Find(string searchString) |
| 1413 | { |
| 1414 | var query = from obj in Container.Instances<User>() |
| 1415 | where obj.Name.ToUpper().Contains(searchString.ToUpper()) |
| 1416 | select obj; |
| 1417 | |
| 1418 | return query.FirstOrDefault(); |
| 1419 | //(If inheriting from AbsractFactoryAndRepository can use:) return SingleObjectWarnIfNoMatch(query); |
| 1420 | } |
| 1421 | #endregion |
| 1422 | |
| 1423 | |
| 1424 | } |
| 1425 | } |
| 1426 | }}} |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | '''ActivityRepository.cs''' |
| 1431 | {{{ |
| 1432 | #!c# |
| 1433 | using NakedObjects; |
| 1434 | using System; |
| 1435 | using System.Collections.Generic; |
| 1436 | using System.ComponentModel; |
| 1437 | using System.Linq; |
| 1438 | using System.Text; |
| 1439 | using System.Threading.Tasks; |
| 1440 | |
| 1441 | namespace CoDBIS_NakedObjects |
| 1442 | { |
| 1443 | [DisplayName("Activities")] |
| 1444 | public class ActivityRepository |
| 1445 | { |
| 1446 | public IDomainObjectContainer Container { set; protected get; } |
| 1447 | |
| 1448 | |
| 1449 | public IQueryable<Activity> AllActivitys() |
| 1450 | { |
| 1451 | return Container.Instances<Activity>(); |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | public Activity CreateNewActivity() |
| 1456 | { |
| 1457 | Activity obj = Container.NewTransientInstance<Activity>(); |
| 1458 | //set up any parameters |
| 1459 | //Container.Persist(ref obj); |
| 1460 | return obj; |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | #region Find |
| 1465 | public Activity Find(string name ) |
| 1466 | { |
| 1467 | var query = from obj in Container.Instances<Activity>() |
| 1468 | where obj.Name.ToUpper().Contains(name.ToUpper()) |
| 1469 | select obj; |
| 1470 | |
| 1471 | return query.FirstOrDefault(); |
| 1472 | //(If inheriting from AbsractFactoryAndRepository can use:) return SingleObjectWarnIfNoMatch(query); |
| 1473 | } |
| 1474 | #endregion |
| 1475 | |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | }}} |
| 1480 | |
| 1481 | '''RunWeb.cs''' |
| 1482 | {{{ |
| 1483 | #!c# |
| 1484 | using CoDBIS_NakedObjects; |
| 1485 | using NakedObjects.Boot; |
| 1486 | using NakedObjects.Core.Context; |
| 1487 | using NakedObjects.Core.NakedObjectsSystem; |
| 1488 | using NakedObjects.EntityObjectStore; |
| 1489 | using NakedObjects.Services; |
| 1490 | using NakedObjects.Web.Mvc; |
| 1491 | using NakedObjects.Web.Mvc.Helpers; |
| 1492 | using System.Data.Entity; |
| 1493 | using System.Data.Entity.Infrastructure; |
| 1494 | |
| 1495 | |
| 1496 | namespace CoDBIS_MVC.App_Start { |
| 1497 | public class RunWeb : RunMvc { |
| 1498 | protected override NakedObjectsContext Context { |
| 1499 | get { return HttpContextContext.CreateInstance(); } |
| 1500 | } |
| 1501 | |
| 1502 | protected override IServicesInstaller MenuServices { |
| 1503 | get { |
| 1504 | return new ServicesInstaller( new UserRepository(), new ActivityRepository()); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | protected override IServicesInstaller ContributedActions { |
| 1509 | get { return new ServicesInstaller(); } |
| 1510 | } |
| 1511 | |
| 1512 | protected override IServicesInstaller SystemServices { |
| 1513 | get { return new ServicesInstaller(new SimpleEncryptDecrypt()); } |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | // example functions that gets types for AssociateTypes below |
| 1518 | //private static Type[] AdventureWorksTypes() { |
| 1519 | // var allTypes = AppDomain.CurrentDomain.GetAssemblies().Single(a => a.GetName().Name == "AdventureWorksModel").GetTypes(); |
| 1520 | // return allTypes.Where(t => t.BaseType == typeof(AWDomainObject) && !t.IsAbstract).ToArray(); |
| 1521 | //} |
| 1522 | // |
| 1523 | //private static Type[] CodeFirstTypes() { |
| 1524 | // return new[] {typeof(Class1), typeof(Class2)}; |
| 1525 | //} |
| 1526 | |
| 1527 | protected override IObjectPersistorInstaller Persistor |
| 1528 | { |
| 1529 | get |
| 1530 | { |
| 1531 | Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); //For in-memory database |
| 1532 | Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDbContext>()); //Optional behaviour for CodeFirst |
| 1533 | var installer = new EntityPersistorInstaller(); |
| 1534 | |
| 1535 | //installer.UsingEdmxContext("Model").AssociateTypes(AdventureWorksTypes); // for Model/Database First |
| 1536 | installer.AddCodeFirstDbContextConstructor(() => new MyDbContext()); //For Code First |
| 1537 | |
| 1538 | return installer; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | public static void Run() { |
| 1543 | new RunWeb().Start(); |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | }}} |